blob: 9fdc9c41a678c638c098a63b61457710c7d99de9 [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 Lesinski926969b2016-04-28 17:31:12 -0700111 private static final int VERSION = 144 + (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
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700512 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700513
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700514 int mInitStepMode = 0;
515 int mCurStepMode = 0;
516 int mModStepMode = 0;
517
Dianne Hackborn260c5022014-04-29 11:23:16 -0700518 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700519 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800520 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
521 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700522 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700523
524 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700525 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800526 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
527 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
528
529 static final int MAX_DAILY_ITEMS = 10;
530
531 long mDailyStartTime = 0;
532 long mNextMinDailyDeadline = 0;
533 long mNextMaxDailyDeadline = 0;
534
535 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700538
Amith Yamasanif37447b2009-10-08 18:28:01 -0700539 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800540 private int mPhoneServiceStateRaw = -1;
541 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700542
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800543 private int mNumConnectivityChange;
544 private int mLoadedNumConnectivityChange;
545 private int mUnpluggedNumConnectivityChange;
546
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700547 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
548
Adam Lesinskie08af192015-03-25 16:42:59 -0700549 private PowerProfile mPowerProfile;
550
Evan Millarc64edde2009-04-18 12:26:32 -0700551 /*
552 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
553 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700554 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700555
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700556 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700557 return mKernelWakelockStats;
558 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700559
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700560 String mLastWakeupReason = null;
561 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700562 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700563
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700564 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700565 return mWakeupReasonStats;
566 }
567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800569 this(new SystemClocks());
570 }
571
572 public BatteryStatsImpl(Clocks clocks) {
573 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700574 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700575 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800576 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700577 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700578 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700579 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700580 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
Joe Onoratoabded112016-02-08 16:49:39 -0800583 private void init(Clocks clocks) {
584 mClocks = clocks;
585 mMobileNetworkStats = new NetworkStats[] {
586 new NetworkStats(mClocks.elapsedRealtime(), 50),
587 new NetworkStats(mClocks.elapsedRealtime(), 50),
588 new NetworkStats(mClocks.elapsedRealtime(), 50)
589 };
590 mWifiNetworkStats = new NetworkStats[] {
591 new NetworkStats(mClocks.elapsedRealtime(), 50),
592 new NetworkStats(mClocks.elapsedRealtime(), 50),
593 new NetworkStats(mClocks.elapsedRealtime(), 50)
594 };
595 }
596
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800597 public static interface TimeBaseObs {
598 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
599 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
600 }
601
Joe Onoratoabded112016-02-08 16:49:39 -0800602 // methods are protected not private to be VisibleForTesting
603 public static class TimeBase {
604 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800605
Joe Onoratoabded112016-02-08 16:49:39 -0800606 protected long mUptime;
607 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800608
Joe Onoratoabded112016-02-08 16:49:39 -0800609 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800610
Joe Onoratoabded112016-02-08 16:49:39 -0800611 protected long mPastUptime;
612 protected long mUptimeStart;
613 protected long mPastRealtime;
614 protected long mRealtimeStart;
615 protected long mUnpluggedUptime;
616 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800617
618 public void dump(PrintWriter pw, String prefix) {
619 StringBuilder sb = new StringBuilder(128);
620 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
621 sb.setLength(0);
622 sb.append(prefix);
623 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700624 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800625 pw.println(sb.toString());
626 sb.setLength(0);
627 sb.append(prefix);
628 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700629 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800630 pw.println(sb.toString());
631 sb.setLength(0);
632 sb.append(prefix);
633 sb.append("mPastUptime=");
634 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
635 formatTimeMs(sb, mUptimeStart / 1000);
636 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
637 pw.println(sb.toString());
638 sb.setLength(0);
639 sb.append(prefix);
640 sb.append("mPastRealtime=");
641 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
642 formatTimeMs(sb, mRealtimeStart / 1000);
643 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
644 pw.println(sb.toString());
645 }
646
647 public void add(TimeBaseObs observer) {
648 mObservers.add(observer);
649 }
650
651 public void remove(TimeBaseObs observer) {
652 if (!mObservers.remove(observer)) {
653 Slog.wtf(TAG, "Removed unknown observer: " + observer);
654 }
655 }
656
Joe Onoratoabded112016-02-08 16:49:39 -0800657 public boolean hasObserver(TimeBaseObs observer) {
658 return mObservers.contains(observer);
659 }
660
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800661 public void init(long uptime, long realtime) {
662 mRealtime = 0;
663 mUptime = 0;
664 mPastUptime = 0;
665 mPastRealtime = 0;
666 mUptimeStart = uptime;
667 mRealtimeStart = realtime;
668 mUnpluggedUptime = getUptime(mUptimeStart);
669 mUnpluggedRealtime = getRealtime(mRealtimeStart);
670 }
671
672 public void reset(long uptime, long realtime) {
673 if (!mRunning) {
674 mPastUptime = 0;
675 mPastRealtime = 0;
676 } else {
677 mUptimeStart = uptime;
678 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800679 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
680 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800681 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800682 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800683 mUnpluggedRealtime = getRealtime(realtime);
684 }
685 }
686
687 public long computeUptime(long curTime, int which) {
688 switch (which) {
689 case STATS_SINCE_CHARGED:
690 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800691 case STATS_CURRENT:
692 return getUptime(curTime);
693 case STATS_SINCE_UNPLUGGED:
694 return getUptime(curTime) - mUnpluggedUptime;
695 }
696 return 0;
697 }
698
699 public long computeRealtime(long curTime, int which) {
700 switch (which) {
701 case STATS_SINCE_CHARGED:
702 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800703 case STATS_CURRENT:
704 return getRealtime(curTime);
705 case STATS_SINCE_UNPLUGGED:
706 return getRealtime(curTime) - mUnpluggedRealtime;
707 }
708 return 0;
709 }
710
711 public long getUptime(long curTime) {
712 long time = mPastUptime;
713 if (mRunning) {
714 time += curTime - mUptimeStart;
715 }
716 return time;
717 }
718
719 public long getRealtime(long curTime) {
720 long time = mPastRealtime;
721 if (mRunning) {
722 time += curTime - mRealtimeStart;
723 }
724 return time;
725 }
726
727 public long getUptimeStart() {
728 return mUptimeStart;
729 }
730
731 public long getRealtimeStart() {
732 return mRealtimeStart;
733 }
734
735 public boolean isRunning() {
736 return mRunning;
737 }
738
739 public boolean setRunning(boolean running, long uptime, long realtime) {
740 if (mRunning != running) {
741 mRunning = running;
742 if (running) {
743 mUptimeStart = uptime;
744 mRealtimeStart = realtime;
745 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
746 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
747
748 for (int i = mObservers.size() - 1; i >= 0; i--) {
749 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
750 }
751 } else {
752 mPastUptime += uptime - mUptimeStart;
753 mPastRealtime += realtime - mRealtimeStart;
754
755 long batteryUptime = getUptime(uptime);
756 long batteryRealtime = getRealtime(realtime);
757
758 for (int i = mObservers.size() - 1; i >= 0; i--) {
759 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
760 }
761 }
762 return true;
763 }
764 return false;
765 }
766
767 public void readSummaryFromParcel(Parcel in) {
768 mUptime = in.readLong();
769 mRealtime = in.readLong();
770 }
771
772 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
773 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
774 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
775 }
776
777 public void readFromParcel(Parcel in) {
778 mRunning = false;
779 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800780 mPastUptime = in.readLong();
781 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700782 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800783 mPastRealtime = in.readLong();
784 mRealtimeStart = in.readLong();
785 mUnpluggedUptime = in.readLong();
786 mUnpluggedRealtime = in.readLong();
787 }
788
789 public void writeToParcel(Parcel out, long uptime, long realtime) {
790 final long runningUptime = getUptime(uptime);
791 final long runningRealtime = getRealtime(realtime);
792 out.writeLong(mUptime);
793 out.writeLong(runningUptime);
794 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700795 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800796 out.writeLong(runningRealtime);
797 out.writeLong(mRealtimeStart);
798 out.writeLong(mUnpluggedUptime);
799 out.writeLong(mUnpluggedRealtime);
800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700804 * State for keeping track of counting information.
805 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800806 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700807 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800808 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700809 int mLoadedCount;
810 int mLastCount;
811 int mUnpluggedCount;
812 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700813
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800814 Counter(TimeBase timeBase, Parcel in) {
815 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700816 mPluggedCount = in.readInt();
817 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700818 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700819 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700820 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800821 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700822 }
823
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800824 Counter(TimeBase timeBase) {
825 mTimeBase = timeBase;
826 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700827 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700828
Dianne Hackborn617f8772009-03-31 15:04:46 -0700829 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700830 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700831 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700832 out.writeInt(mUnpluggedCount);
833 }
834
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800835 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700836 mUnpluggedCount = mPluggedCount;
837 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700838 }
839
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800840 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700841 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700842 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700843
Dianne Hackborn617f8772009-03-31 15:04:46 -0700844 /**
845 * Writes a possibly null Counter to a Parcel.
846 *
847 * @param out the Parcel to be written to.
848 * @param counter a Counter, or null.
849 */
850 public static void writeCounterToParcel(Parcel out, Counter counter) {
851 if (counter == null) {
852 out.writeInt(0); // indicates null
853 return;
854 }
855 out.writeInt(1); // indicates non-null
856
857 counter.writeToParcel(out);
858 }
859
860 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700861 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700862 int val = mCount.get();
863 if (which == STATS_SINCE_UNPLUGGED) {
864 val -= mUnpluggedCount;
865 } else if (which != STATS_SINCE_CHARGED) {
866 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700867 }
868
869 return val;
870 }
871
872 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700873 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700874 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
875 + " mUnpluggedCount=" + mUnpluggedCount
876 + " mPluggedCount=" + mPluggedCount);
877 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700878
Christopher Tate4cee7252010-03-19 14:50:40 -0700879 void stepAtomic() {
880 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700881 }
882
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700883 /**
884 * Clear state of this counter.
885 */
886 void reset(boolean detachIfReset) {
887 mCount.set(0);
888 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
889 if (detachIfReset) {
890 detach();
891 }
892 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700893
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700894 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800895 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700896 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700897
Dianne Hackborn617f8772009-03-31 15:04:46 -0700898 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700899 int count = mCount.get();
900 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700901 }
902
903 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700904 mLoadedCount = in.readInt();
905 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700906 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700907 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700908 }
909 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700910
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700911 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800912 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700913 long mCount;
914 long mLoadedCount;
915 long mLastCount;
916 long mUnpluggedCount;
917 long mPluggedCount;
918
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800919 LongSamplingCounter(TimeBase timeBase, Parcel in) {
920 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700921 mPluggedCount = in.readLong();
922 mCount = mPluggedCount;
923 mLoadedCount = in.readLong();
924 mLastCount = 0;
925 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800926 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700927 }
928
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800929 LongSamplingCounter(TimeBase timeBase) {
930 mTimeBase = timeBase;
931 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700932 }
933
934 public void writeToParcel(Parcel out) {
935 out.writeLong(mCount);
936 out.writeLong(mLoadedCount);
937 out.writeLong(mUnpluggedCount);
938 }
939
940 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800941 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700942 mUnpluggedCount = mPluggedCount;
943 mCount = mPluggedCount;
944 }
945
946 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800947 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700948 mPluggedCount = mCount;
949 }
950
951 public long getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700952 long val = mCount;
953 if (which == STATS_SINCE_UNPLUGGED) {
954 val -= mUnpluggedCount;
955 } else if (which != STATS_SINCE_CHARGED) {
956 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700957 }
958
959 return val;
960 }
961
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700962 @Override
963 public void logState(Printer pw, String prefix) {
964 pw.println(prefix + "mCount=" + mCount
965 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
966 + " mUnpluggedCount=" + mUnpluggedCount
967 + " mPluggedCount=" + mPluggedCount);
968 }
969
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700970 void addCountLocked(long count) {
971 mCount += count;
972 }
973
974 /**
975 * Clear state of this counter.
976 */
977 void reset(boolean detachIfReset) {
978 mCount = 0;
979 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
980 if (detachIfReset) {
981 detach();
982 }
983 }
984
985 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800986 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700987 }
988
989 void writeSummaryFromParcelLocked(Parcel out) {
990 out.writeLong(mCount);
991 }
992
993 void readSummaryFromParcelLocked(Parcel in) {
994 mLoadedCount = in.readLong();
995 mCount = mLoadedCount;
996 mLastCount = 0;
997 mUnpluggedCount = mPluggedCount = mLoadedCount;
998 }
999 }
1000
Dianne Hackborn617f8772009-03-31 15:04:46 -07001001 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 * State for keeping track of timing information.
1003 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001004 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001005 protected final Clocks mClocks;
1006 protected final int mType;
1007 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001008
Joe Onoratoabded112016-02-08 16:49:39 -08001009 protected int mCount;
1010 protected int mLoadedCount;
1011 protected int mLastCount;
1012 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 // Times are in microseconds for better accuracy when dividing by the
1015 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 /**
1018 * The total time we have accumulated since the start of the original
1019 * boot, to the last time something interesting happened in the
1020 * current run.
1021 */
Joe Onoratoabded112016-02-08 16:49:39 -08001022 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 /**
1025 * The total time we loaded for the previous runs. Subtract this from
1026 * mTotalTime to find the time for the current run of the system.
1027 */
Joe Onoratoabded112016-02-08 16:49:39 -08001028 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 /**
1031 * The run time of the last run of the system, as loaded from the
1032 * saved data.
1033 */
Joe Onoratoabded112016-02-08 16:49:39 -08001034 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 /**
1037 * The value of mTotalTime when unplug() was last called. Subtract
1038 * this from mTotalTime to find the time since the last unplug from
1039 * power.
1040 */
Joe Onoratoabded112016-02-08 16:49:39 -08001041 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001042
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001043 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001044 * The total time this timer has been running until the latest mark has been set.
1045 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1046 */
Joe Onoratoabded112016-02-08 16:49:39 -08001047 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001048
1049 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001050 * Constructs from a parcel.
1051 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001052 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001053 * @param in
1054 */
Joe Onoratoabded112016-02-08 16:49:39 -08001055 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1056 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001058 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mCount = in.readInt();
1061 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001062 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 mUnpluggedCount = in.readInt();
1064 mTotalTime = in.readLong();
1065 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001066 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001068 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001069 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001070 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072
Joe Onoratoabded112016-02-08 16:49:39 -08001073 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1074 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001076 mTimeBase = timeBase;
1077 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
Evan Millarc64edde2009-04-18 12:26:32 -07001079
1080 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001081
Evan Millarc64edde2009-04-18 12:26:32 -07001082 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001083
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001084 /**
1085 * Clear state of this timer. Returns true if the timer is inactive
1086 * so can be completely dropped.
1087 */
Joe Onoratoabded112016-02-08 16:49:39 -08001088 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001089 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001090 mCount = mLoadedCount = mLastCount = 0;
1091 if (detachIfReset) {
1092 detach();
1093 }
1094 return true;
1095 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001096
Joe Onoratoabded112016-02-08 16:49:39 -08001097 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001098 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001099 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001100
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001101 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001102 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1103 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001104 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001107 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001110 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112
Adam Lesinskie08af192015-03-25 16:42:59 -07001113 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001114 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001116 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 + " old mUnpluggedTime=" + mUnpluggedTime
1118 + " old mUnpluggedCount=" + mUnpluggedCount);
1119 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001120 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001121 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (DEBUG && mType < 0) {
1123 Log.v(TAG, "unplug #" + mType
1124 + ": new mUnpluggedTime=" + mUnpluggedTime
1125 + " new mUnpluggedCount=" + mUnpluggedCount);
1126 }
1127 }
1128
Adam Lesinskie08af192015-03-25 16:42:59 -07001129 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001130 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001131 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001132 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001133 + " old mTotalTime=" + mTotalTime);
1134 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001135 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001136 mCount = computeCurrentCountLocked();
1137 if (DEBUG && mType < 0) {
1138 Log.v(TAG, "plug #" + mType
1139 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Writes a possibly null Timer to a Parcel.
1145 *
1146 * @param out the Parcel to be written to.
1147 * @param timer a Timer, or null.
1148 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001149 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 if (timer == null) {
1151 out.writeInt(0); // indicates null
1152 return;
1153 }
1154 out.writeInt(1); // indicates non-null
1155
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001156 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158
1159 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001160 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001161 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1162 if (which == STATS_SINCE_UNPLUGGED) {
1163 val -= mUnpluggedTime;
1164 } else if (which != STATS_SINCE_CHARGED) {
1165 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
1167
1168 return val;
1169 }
1170
1171 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001172 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001173 int val = computeCurrentCountLocked();
1174 if (which == STATS_SINCE_UNPLUGGED) {
1175 val -= mUnpluggedCount;
1176 } else if (which != STATS_SINCE_CHARGED) {
1177 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179
1180 return val;
1181 }
1182
Adam Lesinskie08af192015-03-25 16:42:59 -07001183 @Override
1184 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1185 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1186 return val - mTimeBeforeMark;
1187 }
1188
1189 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001190 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001191 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1193 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001194 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001196 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001198 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001199
1200
Joe Onoratoabded112016-02-08 16:49:39 -08001201 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001202 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1203 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001204 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001205 }
1206
Joe Onoratoabded112016-02-08 16:49:39 -08001207 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001208 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001209 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001210 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001211 mUnpluggedTime = mTotalTime;
1212 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001213 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001214 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001215
1216 // When reading the summary, we set the mark to be the latest information.
1217 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001218 }
1219 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001220
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001221 /**
1222 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1223 * method. The state of the timer according to its {@link TimeBase} will determine how much
1224 * of the value is recorded.
1225 *
1226 * If the value being recorded resets, {@link #endSample()} can be called in order to
1227 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1228 * between calls, the {@link #endSample()} is automatically called and the new value is
1229 * expected to increase monotonically from that point on.
1230 */
Joe Onoratoabded112016-02-08 16:49:39 -08001231 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001232
Evan Millarc64edde2009-04-18 12:26:32 -07001233 /**
1234 * The most recent reported count from /proc/wakelocks.
1235 */
1236 int mCurrentReportedCount;
1237
1238 /**
1239 * The reported count from /proc/wakelocks when unplug() was last
1240 * called.
1241 */
1242 int mUnpluggedReportedCount;
1243
1244 /**
1245 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001246 */
Evan Millarc64edde2009-04-18 12:26:32 -07001247 long mCurrentReportedTotalTime;
1248
1249
1250 /**
1251 * The reported total_time from /proc/wakelocks when unplug() was last
1252 * called.
1253 */
1254 long mUnpluggedReportedTotalTime;
1255
1256 /**
1257 * Whether we are currently in a discharge cycle.
1258 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001259 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001260
1261 /**
1262 * Whether we are currently recording reported values.
1263 */
1264 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001265
Evan Millarc64edde2009-04-18 12:26:32 -07001266 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001267 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001268 */
1269 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001270
Adam Lesinski98f0d462016-04-19 16:46:20 -07001271 @VisibleForTesting
1272 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001273 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001274 mCurrentReportedCount = in.readInt();
1275 mUnpluggedReportedCount = in.readInt();
1276 mCurrentReportedTotalTime = in.readLong();
1277 mUnpluggedReportedTotalTime = in.readLong();
1278 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001279 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001280 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001281
Adam Lesinski98f0d462016-04-19 16:46:20 -07001282 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001283 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001284 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001285 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001286 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001287 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001288
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001289 /**
1290 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1291 * be less than the values used for a previous invocation.
1292 */
1293 public void endSample() {
1294 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1295 mCount = computeCurrentCountLocked();
1296 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1297 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001298 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001299
Evan Millarc64edde2009-04-18 12:26:32 -07001300 public void setUpdateVersion(int version) {
1301 mUpdateVersion = version;
1302 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001303
Evan Millarc64edde2009-04-18 12:26:32 -07001304 public int getUpdateVersion() {
1305 return mUpdateVersion;
1306 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001307
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001308 /**
1309 * Updates the current recorded values. These are meant to be monotonically increasing
1310 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1311 *
1312 * If the values being recorded have been reset, the monotonically increasing requirement
1313 * will be broken. In this case, {@link #endSample()} is automatically called and
1314 * the total value of totalTime and count are recorded, starting a new monotonically
1315 * increasing sample.
1316 *
1317 * @param totalTime total time of sample in microseconds.
1318 * @param count total number of times the event being sampled occurred.
1319 */
1320 public void update(long totalTime, int count) {
1321 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001322 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001323 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001324 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001325 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001326
1327 mTrackingReportedValues = true;
1328
1329 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1330 endSample();
1331 }
1332
1333 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001334 mCurrentReportedCount = count;
1335 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001336
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001337 /**
1338 * Adds deltaTime and deltaCount to the current sample.
1339 *
1340 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1341 * @param deltaCount additional number of times the event being sampled occurred.
1342 */
1343 public void add(long deltaTime, int deltaCount) {
1344 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001345 }
1346
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001347 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001348 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1349 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001350 if (mTrackingReportedValues) {
1351 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1352 mUnpluggedReportedCount = mCurrentReportedCount;
1353 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001354 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001355 }
1356
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001357 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001358 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1359 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1360 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001361 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001362
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001363 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001364 public void logState(Printer pw, String prefix) {
1365 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001366 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001367 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1368 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1369 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1370 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001371
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001372 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001373 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001374 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001375 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1376 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001377
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001378 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001379 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001380 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001381 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1382 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001383
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001384 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001385 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1386 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001387 out.writeInt(mCurrentReportedCount);
1388 out.writeInt(mUnpluggedReportedCount);
1389 out.writeLong(mCurrentReportedTotalTime);
1390 out.writeLong(mUnpluggedReportedTotalTime);
1391 out.writeInt(mTrackingReportedValues ? 1 : 0);
1392 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001393
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001394 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001395 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001396 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001397 mTrackingReportedValues = false;
1398 mUnpluggedReportedTotalTime = 0;
1399 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001400 return true;
1401 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001402
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001403 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001404 public void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001405 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1406 out.writeLong(mCurrentReportedTotalTime);
1407 out.writeInt(mCurrentReportedCount);
1408 out.writeInt(mTrackingReportedValues ? 1 : 0);
1409 }
1410
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001411 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001412 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001413 super.readSummaryFromParcelLocked(in);
1414 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1415 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1416 mTrackingReportedValues = in.readInt() == 1;
1417 }
1418 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001419
Evan Millarc64edde2009-04-18 12:26:32 -07001420 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001421 * A timer that increments in batches. It does not run for durations, but just jumps
1422 * for a pre-determined amount.
1423 */
Joe Onoratoabded112016-02-08 16:49:39 -08001424 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001425 final Uid mUid;
1426
1427 /**
1428 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1429 */
1430 long mLastAddedTime;
1431
1432 /**
1433 * The last duration that we added to the timer. This is in microseconds.
1434 */
1435 long mLastAddedDuration;
1436
1437 /**
1438 * Whether we are currently in a discharge cycle.
1439 */
1440 boolean mInDischarge;
1441
Joe Onoratoabded112016-02-08 16:49:39 -08001442 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1443 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001444 mUid = uid;
1445 mLastAddedTime = in.readLong();
1446 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001447 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001448 }
1449
Joe Onoratoabded112016-02-08 16:49:39 -08001450 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1451 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001452 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001453 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001454 }
1455
1456 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001457 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1458 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001459 out.writeLong(mLastAddedTime);
1460 out.writeLong(mLastAddedDuration);
1461 }
1462
1463 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001464 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001465 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001466 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001467 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001468 }
1469
1470 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001471 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001472 recomputeLastDuration(elapsedRealtime, false);
1473 mInDischarge = true;
1474 // If we are still within the last added duration, then re-added whatever remains.
1475 if (mLastAddedTime == elapsedRealtime) {
1476 mTotalTime += mLastAddedDuration;
1477 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001478 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001479 }
1480
1481 @Override
1482 public void logState(Printer pw, String prefix) {
1483 super.logState(pw, prefix);
1484 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1485 + " mLastAddedDuration=" + mLastAddedDuration);
1486 }
1487
1488 private long computeOverage(long curTime) {
1489 if (mLastAddedTime > 0) {
1490 return mLastTime + mLastAddedDuration - curTime;
1491 }
1492 return 0;
1493 }
1494
1495 private void recomputeLastDuration(long curTime, boolean abort) {
1496 final long overage = computeOverage(curTime);
1497 if (overage > 0) {
1498 // Aborting before the duration ran out -- roll back the remaining
1499 // duration. Only do this if currently discharging; otherwise we didn't
1500 // actually add the time.
1501 if (mInDischarge) {
1502 mTotalTime -= overage;
1503 }
1504 if (abort) {
1505 mLastAddedTime = 0;
1506 } else {
1507 mLastAddedTime = curTime;
1508 mLastAddedDuration -= overage;
1509 }
1510 }
1511 }
1512
1513 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001514 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001515 recomputeLastDuration(now, true);
1516 mLastAddedTime = now;
1517 mLastAddedDuration = durationMillis * 1000;
1518 if (mInDischarge) {
1519 mTotalTime += mLastAddedDuration;
1520 mCount++;
1521 }
1522 }
1523
1524 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001525 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001526 recomputeLastDuration(now, true);
1527 }
1528
1529 @Override
1530 protected int computeCurrentCountLocked() {
1531 return mCount;
1532 }
1533
1534 @Override
1535 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001536 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001537 if (overage > 0) {
1538 return mTotalTime = overage;
1539 }
1540 return mTotalTime;
1541 }
1542
1543 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001544 public boolean reset(boolean detachIfReset) {
1545 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001546 recomputeLastDuration(now, true);
1547 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001548 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001549 return !stillActive;
1550 }
1551 }
1552
1553 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001554 * State for keeping track of timing information.
1555 */
Joe Onoratoabded112016-02-08 16:49:39 -08001556 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001557 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001558 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001559
Evan Millarc64edde2009-04-18 12:26:32 -07001560 int mNesting;
1561
Evan Millarc64edde2009-04-18 12:26:32 -07001562 /**
1563 * The last time at which we updated the timer. If mNesting is > 0,
1564 * subtract this from the current battery time to find the amount of
1565 * time we have been running since we last computed an update.
1566 */
1567 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001568
Evan Millarc64edde2009-04-18 12:26:32 -07001569 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001570 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001571 * was actually held for an interesting duration.
1572 */
1573 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001574
Amith Yamasanif37447b2009-10-08 18:28:01 -07001575 long mTimeout;
1576
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001577 /**
1578 * For partial wake locks, keep track of whether we are in the list
1579 * to consume CPU cycles.
1580 */
1581 boolean mInList;
1582
Joe Onoratoabded112016-02-08 16:49:39 -08001583 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001584 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001585 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001586 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001587 mTimerPool = timerPool;
1588 mUpdateTime = in.readLong();
1589 }
1590
Joe Onoratoabded112016-02-08 16:49:39 -08001591 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001592 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001593 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001594 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001595 mTimerPool = timerPool;
1596 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001597
Joe Onoratoabded112016-02-08 16:49:39 -08001598 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001599 mTimeout = timeout;
1600 }
1601
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001602 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1603 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001604 out.writeLong(mUpdateTime);
1605 }
1606
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001607 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001608 if (mNesting > 0) {
1609 if (DEBUG && mType < 0) {
1610 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1611 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001612 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1613 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001614 if (DEBUG && mType < 0) {
1615 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1616 }
1617 }
1618 }
1619
1620 public void logState(Printer pw, String prefix) {
1621 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001622 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 + " mAcquireTime=" + mAcquireTime);
1624 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001625
Joe Onoratoabded112016-02-08 16:49:39 -08001626 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001628 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001629 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 if (mTimerPool != null) {
1631 // Accumulate time to all currently active timers before adding
1632 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001633 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 // Add this timer to the active pool
1635 mTimerPool.add(this);
1636 }
1637 // Increment the count
1638 mCount++;
1639 mAcquireTime = mTotalTime;
1640 if (DEBUG && mType < 0) {
1641 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1642 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1643 + " mAcquireTime=" + mAcquireTime);
1644 }
1645 }
1646 }
1647
Joe Onoratoabded112016-02-08 16:49:39 -08001648 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001649 return mNesting > 0;
1650 }
1651
Joe Onoratoabded112016-02-08 16:49:39 -08001652 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 // Ignore attempt to stop a timer that isn't running
1654 if (mNesting == 0) {
1655 return;
1656 }
1657 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001658 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 if (mTimerPool != null) {
1660 // Accumulate time to all active counters, scaled by the total
1661 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001662 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 // Remove this timer from the active pool
1664 mTimerPool.remove(this);
1665 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 mNesting = 1;
1667 mTotalTime = computeRunTimeLocked(batteryRealtime);
1668 mNesting = 0;
1669 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 if (DEBUG && mType < 0) {
1672 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1673 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1674 + " mAcquireTime=" + mAcquireTime);
1675 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 if (mTotalTime == mAcquireTime) {
1678 // If there was no change in the time, then discard this
1679 // count. A somewhat cheezy strategy, but hey.
1680 mCount--;
1681 }
1682 }
1683 }
1684
Joe Onoratoabded112016-02-08 16:49:39 -08001685 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001686 if (mNesting > 0) {
1687 mNesting = 1;
1688 stopRunningLocked(elapsedRealtimeMs);
1689 }
1690 }
1691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 // Update the total time for all other running Timers with the same type as this Timer
1693 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001694 private static long refreshTimersLocked(long batteryRealtime,
1695 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001696 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 final int N = pool.size();
1698 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001699 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 long heldTime = batteryRealtime - t.mUpdateTime;
1701 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001702 final long myTime = heldTime / N;
1703 if (t == self) {
1704 selfTime = myTime;
1705 }
1706 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
1708 t.mUpdateTime = batteryRealtime;
1709 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001710 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
1712
Evan Millarc64edde2009-04-18 12:26:32 -07001713 @Override
1714 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001715 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1716 curBatteryRealtime = mUpdateTime + mTimeout;
1717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 return mTotalTime + (mNesting > 0
1719 ? (curBatteryRealtime - mUpdateTime)
1720 / (mTimerPool != null ? mTimerPool.size() : 1)
1721 : 0);
1722 }
1723
Evan Millarc64edde2009-04-18 12:26:32 -07001724 @Override
1725 protected int computeCurrentCountLocked() {
1726 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728
Adam Lesinskie08af192015-03-25 16:42:59 -07001729 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001730 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001731 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001732 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001733 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001734 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001735 }
1736 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001737 return canDetach;
1738 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001739
Adam Lesinskie08af192015-03-25 16:42:59 -07001740 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001741 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001742 super.detach();
1743 if (mTimerPool != null) {
1744 mTimerPool.remove(this);
1745 }
1746 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001747
Adam Lesinskie08af192015-03-25 16:42:59 -07001748 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001749 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001750 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 mNesting = 0;
1752 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001753
1754 /**
1755 * Set the mark so that we can query later for the total time the timer has
1756 * accumulated since this point. The timer can be running or not.
1757 *
1758 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1759 */
1760 public void setMark(long elapsedRealtimeMs) {
1761 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1762 if (mNesting > 0) {
1763 // We are running.
1764 if (mTimerPool != null) {
1765 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1766 } else {
1767 mTotalTime += batteryRealtime - mUpdateTime;
1768 mUpdateTime = batteryRealtime;
1769 }
1770 }
1771 mTimeBeforeMark = mTotalTime;
1772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001774
Dianne Hackbornd953c532014-08-16 18:17:38 -07001775 public abstract class OverflowArrayMap<T> {
1776 private static final String OVERFLOW_NAME = "*overflow*";
1777
1778 final ArrayMap<String, T> mMap = new ArrayMap<>();
1779 T mCurOverflow;
1780 ArrayMap<String, MutableInt> mActiveOverflow;
1781
1782 public OverflowArrayMap() {
1783 }
1784
1785 public ArrayMap<String, T> getMap() {
1786 return mMap;
1787 }
1788
1789 public void clear() {
1790 mMap.clear();
1791 mCurOverflow = null;
1792 mActiveOverflow = null;
1793 }
1794
1795 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07001796 if (name == null) {
1797 name = "";
1798 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001799 mMap.put(name, obj);
1800 if (OVERFLOW_NAME.equals(name)) {
1801 mCurOverflow = obj;
1802 }
1803 }
1804
1805 public void cleanup() {
1806 if (mActiveOverflow != null) {
1807 if (mActiveOverflow.size() == 0) {
1808 mActiveOverflow = null;
1809 }
1810 }
1811 if (mActiveOverflow == null) {
1812 // There is no currently active overflow, so we should no longer have
1813 // an overflow entry.
1814 if (mMap.containsKey(OVERFLOW_NAME)) {
1815 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1816 + mMap.get(OVERFLOW_NAME));
1817 mMap.remove(OVERFLOW_NAME);
1818 }
1819 mCurOverflow = null;
1820 } else {
1821 // There is currently active overflow, so we should still have an overflow entry.
1822 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1823 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1824 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1825 }
1826 }
1827 }
1828
1829 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001830 if (name == null) {
1831 name = "";
1832 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001833 T obj = mMap.get(name);
1834 if (obj != null) {
1835 return obj;
1836 }
1837
1838 // No object exists for the given name, but do we currently have it
1839 // running as part of the overflow?
1840 if (mActiveOverflow != null) {
1841 MutableInt over = mActiveOverflow.get(name);
1842 if (over != null) {
1843 // We are already actively counting this name in the overflow object.
1844 obj = mCurOverflow;
1845 if (obj == null) {
1846 // Shouldn't be here, but we'll try to recover.
1847 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1848 obj = mCurOverflow = instantiateObject();
1849 mMap.put(OVERFLOW_NAME, obj);
1850 }
1851 over.value++;
1852 return obj;
1853 }
1854 }
1855
1856 // No object exists for given name nor in the overflow; we need to make
1857 // a new one.
1858 final int N = mMap.size();
1859 if (N >= MAX_WAKELOCKS_PER_UID) {
1860 // Went over the limit on number of objects to track; this one goes
1861 // in to the overflow.
1862 obj = mCurOverflow;
1863 if (obj == null) {
1864 // Need to start overflow now...
1865 obj = mCurOverflow = instantiateObject();
1866 mMap.put(OVERFLOW_NAME, obj);
1867 }
1868 if (mActiveOverflow == null) {
1869 mActiveOverflow = new ArrayMap<>();
1870 }
1871 mActiveOverflow.put(name, new MutableInt(1));
1872 return obj;
1873 }
1874
1875 // Normal case where we just need to make a new object.
1876 obj = instantiateObject();
1877 mMap.put(name, obj);
1878 return obj;
1879 }
1880
1881 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001882 if (name == null) {
1883 name = "";
1884 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001885 T obj = mMap.get(name);
1886 if (obj != null) {
1887 return obj;
1888 }
1889
1890 // No object exists for the given name, but do we currently have it
1891 // running as part of the overflow?
1892 if (mActiveOverflow != null) {
1893 MutableInt over = mActiveOverflow.get(name);
1894 if (over != null) {
1895 // We are already actively counting this name in the overflow object.
1896 obj = mCurOverflow;
1897 if (obj != null) {
1898 over.value--;
1899 if (over.value <= 0) {
1900 mActiveOverflow.remove(name);
1901 }
1902 return obj;
1903 }
1904 }
1905 }
1906
1907 // Huh, they are stopping an active operation but we can't find one!
1908 // That's not good.
1909 Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
1910 + mMap.size() + " activeoverflow=" + mActiveOverflow
1911 + " curoverflow=" + mCurOverflow);
1912 return null;
1913 }
1914
1915 public abstract T instantiateObject();
1916 }
1917
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001918 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
1919 implements Parcelable {
1920 private final LongSamplingCounter mIdleTimeMillis;
1921 private final LongSamplingCounter mRxTimeMillis;
1922 private final LongSamplingCounter[] mTxTimeMillis;
1923 private final LongSamplingCounter mPowerDrainMaMs;
1924
1925 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
1926 mIdleTimeMillis = new LongSamplingCounter(timeBase);
1927 mRxTimeMillis = new LongSamplingCounter(timeBase);
1928 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1929 for (int i = 0; i < numTxStates; i++) {
1930 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
1931 }
1932 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
1933 }
1934
1935 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
1936 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
1937 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
1938 final int recordedTxStates = in.readInt();
1939 if (recordedTxStates != numTxStates) {
1940 throw new ParcelFormatException("inconsistent tx state lengths");
1941 }
1942
1943 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1944 for (int i = 0; i < numTxStates; i++) {
1945 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
1946 }
1947 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
1948 }
1949
1950 public void readSummaryFromParcel(Parcel in) {
1951 mIdleTimeMillis.readSummaryFromParcelLocked(in);
1952 mRxTimeMillis.readSummaryFromParcelLocked(in);
1953 final int recordedTxStates = in.readInt();
1954 if (recordedTxStates != mTxTimeMillis.length) {
1955 throw new ParcelFormatException("inconsistent tx state lengths");
1956 }
1957 for (LongSamplingCounter counter : mTxTimeMillis) {
1958 counter.readSummaryFromParcelLocked(in);
1959 }
1960 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
1961 }
1962
1963 @Override
1964 public int describeContents() {
1965 return 0;
1966 }
1967
1968 public void writeSummaryToParcel(Parcel dest) {
1969 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
1970 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
1971 dest.writeInt(mTxTimeMillis.length);
1972 for (LongSamplingCounter counter : mTxTimeMillis) {
1973 counter.writeSummaryFromParcelLocked(dest);
1974 }
1975 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
1976 }
1977
1978 @Override
1979 public void writeToParcel(Parcel dest, int flags) {
1980 mIdleTimeMillis.writeToParcel(dest);
1981 mRxTimeMillis.writeToParcel(dest);
1982 dest.writeInt(mTxTimeMillis.length);
1983 for (LongSamplingCounter counter : mTxTimeMillis) {
1984 counter.writeToParcel(dest);
1985 }
1986 mPowerDrainMaMs.writeToParcel(dest);
1987 }
1988
1989 public void reset(boolean detachIfReset) {
1990 mIdleTimeMillis.reset(detachIfReset);
1991 mRxTimeMillis.reset(detachIfReset);
1992 for (LongSamplingCounter counter : mTxTimeMillis) {
1993 counter.reset(detachIfReset);
1994 }
1995 mPowerDrainMaMs.reset(detachIfReset);
1996 }
1997
1998 public void detach() {
1999 mIdleTimeMillis.detach();
2000 mRxTimeMillis.detach();
2001 for (LongSamplingCounter counter : mTxTimeMillis) {
2002 counter.detach();
2003 }
2004 mPowerDrainMaMs.detach();
2005 }
2006
2007 /**
2008 * @return a LongSamplingCounter, measuring time spent in the idle state in
2009 * milliseconds.
2010 */
2011 @Override
2012 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002013 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002014 }
2015
2016 /**
2017 * @return a LongSamplingCounter, measuring time spent in the receive state in
2018 * milliseconds.
2019 */
2020 @Override
2021 public LongSamplingCounter getRxTimeCounter() {
2022 return mRxTimeMillis;
2023 }
2024
2025 /**
2026 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2027 * milliseconds.
2028 */
2029 @Override
2030 public LongSamplingCounter[] getTxTimeCounters() {
2031 return mTxTimeMillis;
2032 }
2033
2034 /**
2035 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2036 */
2037 @Override
2038 public LongSamplingCounter getPowerCounter() {
2039 return mPowerDrainMaMs;
2040 }
2041 }
2042
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002043 /*
2044 * Get the wakeup reason counter, and create a new one if one
2045 * doesn't already exist.
2046 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002047 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2048 SamplingTimer timer = mWakeupReasonStats.get(name);
2049 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002050 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002051 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002052 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002053 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002054 }
2055
Evan Millarc64edde2009-04-18 12:26:32 -07002056 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002057 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002058 * doesn't already exist.
2059 */
2060 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2061 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2062 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002063 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002064 mKernelWakelockStats.put(name, kwlt);
2065 }
2066 return kwlt;
2067 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002068
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002069 private int writeHistoryTag(HistoryTag tag) {
2070 Integer idxObj = mHistoryTagPool.get(tag);
2071 int idx;
2072 if (idxObj != null) {
2073 idx = idxObj;
2074 } else {
2075 idx = mNextHistoryTagIdx;
2076 HistoryTag key = new HistoryTag();
2077 key.setTo(tag);
2078 tag.poolIdx = idx;
2079 mHistoryTagPool.put(key, idx);
2080 mNextHistoryTagIdx++;
2081 mNumHistoryTagChars += key.string.length() + 1;
2082 }
2083 return idx;
2084 }
2085
2086 private void readHistoryTag(int index, HistoryTag tag) {
2087 tag.string = mReadHistoryStrings[index];
2088 tag.uid = mReadHistoryUids[index];
2089 tag.poolIdx = index;
2090 }
2091
Adam Lesinski926969b2016-04-28 17:31:12 -07002092 /*
2093 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2094
2095 There is always the first token, which may contain the delta time, or an indicator of
2096 the length of the time (int or long) following this token.
2097
2098 First token: always present,
2099 31 23 15 7 0
2100 â–ˆ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â–ˆ
2101
2102 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2103 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2104 delta time.
2105 A: battery level changed and an int follows with battery data.
2106 B: state changed and an int follows with state change data.
2107 C: state2 has changed and an int follows with state2 change data.
2108 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2109 E: event data has changed and an event struct follows.
2110 F: battery charge in coulombs has changed and an int with the charge follows.
2111 G: state flag denoting that the mobile radio was active.
2112 H: state flag denoting that the wifi radio was active.
2113 I: state flag denoting that a wifi scan occurred.
2114 J: state flag denoting that a wifi full lock was held.
2115 K: state flag denoting that the gps was on.
2116 L: state flag denoting that a wakelock was held.
2117 M: state flag denoting that the cpu was running.
2118
2119 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2120 with the time delta.
2121
2122 Battery level int: if A in the first token is set,
2123 31 23 15 7 0
2124 â–ˆ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â–ˆ
2125
2126 D: indicates that extra history details follow.
2127 V: the battery voltage.
2128 T: the battery temperature.
2129 L: the battery level (out of 100).
2130
2131 State change int: if B in the first token is set,
2132 31 23 15 7 0
2133 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2134
2135 A: wifi multicast was on.
2136 B: battery was plugged in.
2137 C: screen was on.
2138 D: phone was scanning for signal.
2139 E: audio was on.
2140 F: a sensor was active.
2141
2142 State2 change int: if C in the first token is set,
2143 31 23 15 7 0
2144 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2145
2146 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2147 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2148 C: a bluetooth scan was active.
2149 D: the camera was active.
2150 E: bluetooth was on.
2151 F: a phone call was active.
2152 G: the device was charging.
2153 H: 2 bits indicating the device-idle (doze) state: off, light, full
2154 I: the flashlight was on.
2155 J: wifi was on.
2156 K: wifi was running.
2157 L: video was playing.
2158 M: power save mode was on.
2159
2160 Wakelock/wakereason struct: if D in the first token is set,
2161 TODO(adamlesinski): describe wakelock/wakereason struct.
2162
2163 Event struct: if E in the first token is set,
2164 TODO(adamlesinski): describe the event struct.
2165
2166 History step details struct: if D in the battery level int is set,
2167 TODO(adamlesinski): describe the history step details struct.
2168
2169 Battery charge int: if F in the first token is set, an int representing the battery charge
2170 in coulombs follows.
2171 */
2172
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002173 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002174 static final int DELTA_TIME_MASK = 0x7ffff;
2175 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2176 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2177 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002178 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002179 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002180 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002181 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002182 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002183 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002184 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002185 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002186 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002187 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002188 // Flag in delta int: contains the battery charge count in uAh.
2189 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002190 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002191 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002192
2193 // These are the pieces of battery state that are packed in to the upper bits of
2194 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002195 // in STATE_BATTERY_MASK.
2196 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002197 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2198 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2199 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2200 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2201 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2202 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002203
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002204 // We use the low bit of the battery state int to indicate that we have full details
2205 // from a battery level change.
2206 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2207
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002208 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002209 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002210 dest.writeInt(DELTA_TIME_ABS);
2211 cur.writeToParcel(dest, 0);
2212 return;
2213 }
2214
2215 final long deltaTime = cur.time - last.time;
2216 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2217 final int lastStateInt = buildStateInt(last);
2218
2219 int deltaTimeToken;
2220 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2221 deltaTimeToken = DELTA_TIME_LONG;
2222 } else if (deltaTime >= DELTA_TIME_ABS) {
2223 deltaTimeToken = DELTA_TIME_INT;
2224 } else {
2225 deltaTimeToken = (int)deltaTime;
2226 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002227 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002228 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2229 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2230 final boolean computeStepDetails = includeStepDetails != 0
2231 || mLastHistoryStepDetails == null;
2232 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002233 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2234 if (batteryLevelIntChanged) {
2235 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2236 }
2237 final int stateInt = buildStateInt(cur);
2238 final boolean stateIntChanged = stateInt != lastStateInt;
2239 if (stateIntChanged) {
2240 firstToken |= DELTA_STATE_FLAG;
2241 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002242 final boolean state2IntChanged = cur.states2 != last.states2;
2243 if (state2IntChanged) {
2244 firstToken |= DELTA_STATE2_FLAG;
2245 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002246 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002247 firstToken |= DELTA_WAKELOCK_FLAG;
2248 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002249 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2250 firstToken |= DELTA_EVENT_FLAG;
2251 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002252
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002253 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2254 if (batteryChargeChanged) {
2255 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002256 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002257 dest.writeInt(firstToken);
2258 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2259 + " deltaTime=" + deltaTime);
2260
2261 if (deltaTimeToken >= DELTA_TIME_INT) {
2262 if (deltaTimeToken == DELTA_TIME_INT) {
2263 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2264 dest.writeInt((int)deltaTime);
2265 } else {
2266 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2267 dest.writeLong(deltaTime);
2268 }
2269 }
2270 if (batteryLevelIntChanged) {
2271 dest.writeInt(batteryLevelInt);
2272 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2273 + Integer.toHexString(batteryLevelInt)
2274 + " batteryLevel=" + cur.batteryLevel
2275 + " batteryTemp=" + cur.batteryTemperature
2276 + " batteryVolt=" + (int)cur.batteryVoltage);
2277 }
2278 if (stateIntChanged) {
2279 dest.writeInt(stateInt);
2280 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2281 + Integer.toHexString(stateInt)
2282 + " batteryStatus=" + cur.batteryStatus
2283 + " batteryHealth=" + cur.batteryHealth
2284 + " batteryPlugType=" + cur.batteryPlugType
2285 + " states=0x" + Integer.toHexString(cur.states));
2286 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002287 if (state2IntChanged) {
2288 dest.writeInt(cur.states2);
2289 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2290 + Integer.toHexString(cur.states2));
2291 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002292 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2293 int wakeLockIndex;
2294 int wakeReasonIndex;
2295 if (cur.wakelockTag != null) {
2296 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2297 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2298 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2299 } else {
2300 wakeLockIndex = 0xffff;
2301 }
2302 if (cur.wakeReasonTag != null) {
2303 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2304 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2305 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2306 } else {
2307 wakeReasonIndex = 0xffff;
2308 }
2309 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002310 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002311 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002312 int index = writeHistoryTag(cur.eventTag);
2313 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002314 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002315 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2316 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2317 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002318 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002319 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002320 if (mPlatformIdleStateCallback != null) {
2321 mCurHistoryStepDetails.statPlatformIdleState =
2322 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2323 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2324 mCurHistoryStepDetails.statPlatformIdleState);
2325 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002326 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2327 if (includeStepDetails != 0) {
2328 mCurHistoryStepDetails.writeToParcel(dest);
2329 }
2330 cur.stepDetails = mCurHistoryStepDetails;
2331 mLastHistoryStepDetails = mCurHistoryStepDetails;
2332 } else {
2333 cur.stepDetails = null;
2334 }
2335 if (mLastHistoryStepLevel < cur.batteryLevel) {
2336 mLastHistoryStepDetails = null;
2337 }
2338 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002339
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002340 if (batteryChargeChanged) {
2341 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2342 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002343 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002344 }
2345
2346 private int buildBatteryLevelInt(HistoryItem h) {
2347 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002348 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2349 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2350 }
2351
2352 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2353 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2354 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2355 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002356 }
2357
2358 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002359 int plugType = 0;
2360 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2361 plugType = 1;
2362 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2363 plugType = 2;
2364 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2365 plugType = 3;
2366 }
2367 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2368 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2369 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002370 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002371 }
2372
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002373 private void computeHistoryStepDetails(final HistoryStepDetails out,
2374 final HistoryStepDetails last) {
2375 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2376
2377 // Perform a CPU update right after we do this collection, so we have started
2378 // collecting good data for the next step.
2379 requestImmediateCpuUpdate();
2380
2381 if (last == null) {
2382 // We are not generating a delta, so all we need to do is reset the stats
2383 // we will later be doing a delta from.
2384 final int NU = mUidStats.size();
2385 for (int i=0; i<NU; i++) {
2386 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2387 uid.mLastStepUserTime = uid.mCurStepUserTime;
2388 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2389 }
2390 mLastStepCpuUserTime = mCurStepCpuUserTime;
2391 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2392 mLastStepStatUserTime = mCurStepStatUserTime;
2393 mLastStepStatSystemTime = mCurStepStatSystemTime;
2394 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2395 mLastStepStatIrqTime = mCurStepStatIrqTime;
2396 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2397 mLastStepStatIdleTime = mCurStepStatIdleTime;
2398 tmp.clear();
2399 return;
2400 }
2401 if (DEBUG) {
2402 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2403 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2404 + " irq=" + mLastStepStatIrqTime + " sirq="
2405 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2406 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2407 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2408 + " irq=" + mCurStepStatIrqTime + " sirq="
2409 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2410 }
2411 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2412 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2413 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2414 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2415 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2416 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2417 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2418 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2419 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2420 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2421 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2422 final int NU = mUidStats.size();
2423 for (int i=0; i<NU; i++) {
2424 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2425 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2426 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2427 final int totalTime = totalUTime + totalSTime;
2428 uid.mLastStepUserTime = uid.mCurStepUserTime;
2429 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2430 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2431 continue;
2432 }
2433 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2434 out.appCpuUid3 = uid.mUid;
2435 out.appCpuUTime3 = totalUTime;
2436 out.appCpuSTime3 = totalSTime;
2437 } else {
2438 out.appCpuUid3 = out.appCpuUid2;
2439 out.appCpuUTime3 = out.appCpuUTime2;
2440 out.appCpuSTime3 = out.appCpuSTime2;
2441 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2442 out.appCpuUid2 = uid.mUid;
2443 out.appCpuUTime2 = totalUTime;
2444 out.appCpuSTime2 = totalSTime;
2445 } else {
2446 out.appCpuUid2 = out.appCpuUid1;
2447 out.appCpuUTime2 = out.appCpuUTime1;
2448 out.appCpuSTime2 = out.appCpuSTime1;
2449 out.appCpuUid1 = uid.mUid;
2450 out.appCpuUTime1 = totalUTime;
2451 out.appCpuSTime1 = totalSTime;
2452 }
2453 }
2454 }
2455 mLastStepCpuUserTime = mCurStepCpuUserTime;
2456 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2457 mLastStepStatUserTime = mCurStepStatUserTime;
2458 mLastStepStatSystemTime = mCurStepStatSystemTime;
2459 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2460 mLastStepStatIrqTime = mCurStepStatIrqTime;
2461 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2462 mLastStepStatIdleTime = mCurStepStatIdleTime;
2463 }
2464
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002465 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2466 int firstToken = src.readInt();
2467 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002468 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002469 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002470 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2471 + " deltaTimeToken=" + deltaTimeToken);
2472
2473 if (deltaTimeToken < DELTA_TIME_ABS) {
2474 cur.time += deltaTimeToken;
2475 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2476 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002477 cur.numReadInts += 2;
2478 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002479 cur.readFromParcel(src);
2480 return;
2481 } else if (deltaTimeToken == DELTA_TIME_INT) {
2482 int delta = src.readInt();
2483 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002484 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002485 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2486 } else {
2487 long delta = src.readLong();
2488 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2489 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002490 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002491 }
2492
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002493 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002494 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002495 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002496 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002497 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002498 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2499 + Integer.toHexString(batteryLevelInt)
2500 + " batteryLevel=" + cur.batteryLevel
2501 + " batteryTemp=" + cur.batteryTemperature
2502 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002503 } else {
2504 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002505 }
2506
2507 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2508 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002509 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002510 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2511 & STATE_BATTERY_STATUS_MASK);
2512 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2513 & STATE_BATTERY_HEALTH_MASK);
2514 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2515 & STATE_BATTERY_PLUG_MASK);
2516 switch (cur.batteryPlugType) {
2517 case 1:
2518 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2519 break;
2520 case 2:
2521 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2522 break;
2523 case 3:
2524 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2525 break;
2526 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002527 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002528 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2529 + Integer.toHexString(stateInt)
2530 + " batteryStatus=" + cur.batteryStatus
2531 + " batteryHealth=" + cur.batteryHealth
2532 + " batteryPlugType=" + cur.batteryPlugType
2533 + " states=0x" + Integer.toHexString(cur.states));
2534 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002535 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002536 }
2537
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002538 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2539 cur.states2 = src.readInt();
2540 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2541 + Integer.toHexString(cur.states2));
2542 }
2543
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002544 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002545 int indexes = src.readInt();
2546 int wakeLockIndex = indexes&0xffff;
2547 int wakeReasonIndex = (indexes>>16)&0xffff;
2548 if (wakeLockIndex != 0xffff) {
2549 cur.wakelockTag = cur.localWakelockTag;
2550 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2551 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2552 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2553 } else {
2554 cur.wakelockTag = null;
2555 }
2556 if (wakeReasonIndex != 0xffff) {
2557 cur.wakeReasonTag = cur.localWakeReasonTag;
2558 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2559 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2560 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2561 } else {
2562 cur.wakeReasonTag = null;
2563 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002564 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002565 } else {
2566 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002567 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002568 }
2569
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002570 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002571 cur.eventTag = cur.localEventTag;
2572 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002573 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002574 final int index = ((codeAndIndex>>16)&0xffff);
2575 readHistoryTag(index, cur.eventTag);
2576 cur.numReadInts += 1;
2577 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2578 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2579 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002580 } else {
2581 cur.eventCode = HistoryItem.EVENT_NONE;
2582 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002583
2584 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2585 cur.stepDetails = mReadHistoryStepDetails;
2586 cur.stepDetails.readFromParcel(src);
2587 } else {
2588 cur.stepDetails = null;
2589 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002590
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002591 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2592 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002593 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002594 }
2595
Dianne Hackbornfc064132014-06-02 12:42:12 -07002596 @Override
2597 public void commitCurrentHistoryBatchLocked() {
2598 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2599 }
2600
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002601 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002602 if (!mHaveBatteryLevel || !mRecordingHistory) {
2603 return;
2604 }
2605
Dianne Hackborn40c87252014-03-19 16:55:40 -07002606 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002607 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2608 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002609 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002610 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002611 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2612 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002613 + Integer.toHexString(lastDiffStates) + " diff2="
2614 + Integer.toHexString(diffStates2) + " lastDiff2="
2615 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002616 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002617 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002618 && (diffStates2&lastDiffStates2) == 0
2619 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2620 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002621 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002622 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002623 || cur.eventCode == HistoryItem.EVENT_NONE)
2624 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2625 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2626 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2627 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2628 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2629 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002630 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002631 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002632 // as long as no bit has changed both between now and the last entry, as
2633 // well as the last entry and the one before it (so we capture any toggles).
2634 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002635 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2636 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2637 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002638 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002639 // If the last written history had a wakelock tag, we need to retain it.
2640 // Note that the condition above made sure that we aren't in a case where
2641 // both it and the current history item have a wakelock tag.
2642 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002643 cur.wakelockTag = cur.localWakelockTag;
2644 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002645 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002646 // If the last written history had a wake reason tag, we need to retain it.
2647 // Note that the condition above made sure that we aren't in a case where
2648 // both it and the current history item have a wakelock tag.
2649 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002650 cur.wakeReasonTag = cur.localWakeReasonTag;
2651 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002652 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002653 // If the last written history had an event, we need to retain it.
2654 // Note that the condition above made sure that we aren't in a case where
2655 // both it and the current history item have an event.
2656 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002657 cur.eventCode = mHistoryLastWritten.eventCode;
2658 cur.eventTag = cur.localEventTag;
2659 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002660 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002661 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002662 }
2663
2664 final int dataSize = mHistoryBuffer.dataSize();
2665 if (dataSize >= MAX_HISTORY_BUFFER) {
2666 if (!mHistoryOverflow) {
2667 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002668 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2669 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002670 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002671 }
2672
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002673 // After overflow, we allow various bit-wise states to settle to 0.
2674 boolean writeAnyway = false;
2675 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2676 & mActiveHistoryStates;
2677 if (mHistoryLastWritten.states != curStates) {
2678 // mActiveHistoryStates keeps track of which bits in .states are now being
2679 // forced to 0.
2680 int old = mActiveHistoryStates;
2681 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2682 writeAnyway |= old != mActiveHistoryStates;
2683 }
2684 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2685 & mActiveHistoryStates2;
2686 if (mHistoryLastWritten.states2 != curStates2) {
2687 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2688 // forced to 0.
2689 int old = mActiveHistoryStates2;
2690 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2691 writeAnyway |= old != mActiveHistoryStates2;
2692 }
2693
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002694 // Once we've reached the maximum number of items, we only
2695 // record changes to the battery level and the most interesting states.
2696 // Once we've reached the maximum maximum number of items, we only
2697 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002698 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002699 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002700 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002701 & HistoryItem.MOST_INTERESTING_STATES) == 0
2702 || ((mHistoryLastWritten.states2^cur.states2)
2703 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002704 return;
2705 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002706
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002707 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002708 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002709 }
2710
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002711 if (dataSize == 0) {
2712 // The history is currently empty; we need it to start with a time stamp.
2713 cur.currentTime = System.currentTimeMillis();
2714 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2715 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002716 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002717 }
2718
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002719 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2720 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002721 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002722 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002723 }
2724 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2725 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002726 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002727 mHistoryLastWritten.states &= mActiveHistoryStates;
2728 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002729 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002730 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002731 cur.wakelockTag = null;
2732 cur.wakeReasonTag = null;
2733 cur.eventCode = HistoryItem.EVENT_NONE;
2734 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002735 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2736 + " now " + mHistoryBuffer.dataPosition()
2737 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002738 }
2739
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002740 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002741 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002742
Dianne Hackborn40c87252014-03-19 16:55:40 -07002743 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002744 if (mTrackRunningHistoryElapsedRealtime != 0) {
2745 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2746 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2747 if (diffUptime < (diffElapsed-20)) {
2748 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2749 mHistoryAddTmp.setTo(mHistoryLastWritten);
2750 mHistoryAddTmp.wakelockTag = null;
2751 mHistoryAddTmp.wakeReasonTag = null;
2752 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2753 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2754 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2755 }
2756 }
2757 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2758 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2759 mTrackRunningHistoryUptime = uptimeMs;
2760 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2761 }
2762
2763 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2764 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002765
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002766 if (!USE_OLD_HISTORY) {
2767 return;
2768 }
2769
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002770 if (!mHaveBatteryLevel || !mRecordingHistory) {
2771 return;
2772 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002773
2774 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002775 // and no states have since the last recorded entry changed and
2776 // are now resetting back to their original value, then just collapse
2777 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002778 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002779 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002780 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2781 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002782 // If the current is the same as the one before, then we no
2783 // longer need the entry.
2784 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002785 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002786 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002787 mHistoryLastEnd.next = null;
2788 mHistoryEnd.next = mHistoryCache;
2789 mHistoryCache = mHistoryEnd;
2790 mHistoryEnd = mHistoryLastEnd;
2791 mHistoryLastEnd = null;
2792 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002793 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2794 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002795 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002796 }
2797 return;
2798 }
2799
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002800 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002801 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002802
2803 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2804 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002805 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002806 }
2807
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002808 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2809 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002810 // record changes to the battery level and the most interesting states.
2811 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002812 // record changes to the battery level.
2813 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002814 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002815 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002816 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002817 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002818 return;
2819 }
2820 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002821
Dianne Hackborn40c87252014-03-19 16:55:40 -07002822 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002823 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002824
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002825 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002826 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002827 mHistoryCur.eventCode = code;
2828 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2829 mHistoryCur.eventTag.string = name;
2830 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002831 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002832 }
2833
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002834 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002835 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002836 if (rec != null) {
2837 mHistoryCache = rec.next;
2838 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002839 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002840 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002841 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002842
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002843 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002844 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002845
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002846 void addHistoryRecordLocked(HistoryItem rec) {
2847 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002848 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002849 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002850 if (mHistoryEnd != null) {
2851 mHistoryEnd.next = rec;
2852 mHistoryEnd = rec;
2853 } else {
2854 mHistory = mHistoryEnd = rec;
2855 }
2856 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002857
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002858 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002859 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002860 if (USE_OLD_HISTORY) {
2861 if (mHistory != null) {
2862 mHistoryEnd.next = mHistoryCache;
2863 mHistoryCache = mHistory;
2864 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2865 }
2866 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002867 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002868
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002869 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002870 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002871 mTrackRunningHistoryElapsedRealtime = 0;
2872 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002873
2874 mHistoryBuffer.setDataSize(0);
2875 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002876 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002877 mHistoryLastLastWritten.clear();
2878 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002879 mHistoryTagPool.clear();
2880 mNextHistoryTagIdx = 0;
2881 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002882 mHistoryBufferLastPos = -1;
2883 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002884 mActiveHistoryStates = 0xffffffff;
2885 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002886 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002887
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002888 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2889 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002890 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002891
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002892 boolean unpluggedScreenOff = unplugged && screenOff;
2893 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2894 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07002895 if (DEBUG_ENERGY_CPU) {
2896 Slog.d(TAG, "Updating cpu time because screen is now " +
2897 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002898 }
Adam Lesinski72478f02015-06-17 15:39:43 -07002899 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002900 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
2902 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002903
Dianne Hackborn099bc622014-01-22 13:39:16 -08002904 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2905 mIsolatedUids.put(isolatedUid, appUid);
2906 }
2907
Adam Lesinski61db88f2015-07-01 15:05:07 -07002908 /**
2909 * Schedules a read of the latest cpu times before removing the isolated UID.
2910 * @see #removeIsolatedUidLocked(int)
2911 */
2912 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002913 int curUid = mIsolatedUids.get(isolatedUid, -1);
2914 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07002915 if (mExternalSync != null) {
2916 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
2917 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002918 }
2919 }
2920
Adam Lesinski61db88f2015-07-01 15:05:07 -07002921 /**
2922 * This should only be called after the cpu times have been read.
2923 * @see #scheduleRemoveIsolatedUidLocked(int, int)
2924 */
2925 public void removeIsolatedUidLocked(int isolatedUid) {
2926 mIsolatedUids.delete(isolatedUid);
2927 mKernelUidCpuTimeReader.removeUid(isolatedUid);
2928 }
2929
Dianne Hackborn099bc622014-01-22 13:39:16 -08002930 public int mapUid(int uid) {
2931 int isolated = mIsolatedUids.get(uid, -1);
2932 return isolated > 0 ? isolated : uid;
2933 }
2934
2935 public void noteEventLocked(int code, String name, int uid) {
2936 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002937 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2938 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002939 }
Joe Onoratoabded112016-02-08 16:49:39 -08002940 final long elapsedRealtime = mClocks.elapsedRealtime();
2941 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002942 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002943 }
2944
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002945 boolean ensureStartClockTime(final long currentTime) {
2946 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
2947 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
2948 // If the start clock time has changed by more than a year, then presumably
2949 // the previous time was completely bogus. So we are going to figure out a
2950 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08002951 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002952 return true;
2953 }
2954 return false;
2955 }
2956
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002957 public void noteCurrentTimeChangedLocked() {
2958 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08002959 final long elapsedRealtime = mClocks.elapsedRealtime();
2960 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002961 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002962 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002963 }
2964
Dianne Hackborn61659e52014-07-09 16:13:01 -07002965 public void noteProcessStartLocked(String name, int uid) {
2966 uid = mapUid(uid);
2967 if (isOnBattery()) {
2968 Uid u = getUidStatsLocked(uid);
2969 u.getProcessStatsLocked(name).incStartsLocked();
2970 }
2971 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2972 return;
2973 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002974 if (!mRecordAllHistory) {
2975 return;
2976 }
Joe Onoratoabded112016-02-08 16:49:39 -08002977 final long elapsedRealtime = mClocks.elapsedRealtime();
2978 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002979 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2980 }
2981
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002982 public void noteProcessCrashLocked(String name, int uid) {
2983 uid = mapUid(uid);
2984 if (isOnBattery()) {
2985 Uid u = getUidStatsLocked(uid);
2986 u.getProcessStatsLocked(name).incNumCrashesLocked();
2987 }
2988 }
2989
2990 public void noteProcessAnrLocked(String name, int uid) {
2991 uid = mapUid(uid);
2992 if (isOnBattery()) {
2993 Uid u = getUidStatsLocked(uid);
2994 u.getProcessStatsLocked(name).incNumAnrsLocked();
2995 }
2996 }
2997
Dianne Hackborna8d10942015-11-19 17:55:19 -08002998 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07002999 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003000 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003001 }
3002
3003 public void noteProcessFinishLocked(String name, int uid) {
3004 uid = mapUid(uid);
3005 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3006 return;
3007 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003008 if (!mRecordAllHistory) {
3009 return;
3010 }
Joe Onoratoabded112016-02-08 16:49:39 -08003011 final long elapsedRealtime = mClocks.elapsedRealtime();
3012 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003013 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003014 }
3015
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003016 public void noteSyncStartLocked(String name, int uid) {
3017 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003018 final long elapsedRealtime = mClocks.elapsedRealtime();
3019 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003020 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3021 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3022 return;
3023 }
3024 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3025 }
3026
3027 public void noteSyncFinishLocked(String name, int uid) {
3028 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003029 final long elapsedRealtime = mClocks.elapsedRealtime();
3030 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003031 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3032 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3033 return;
3034 }
3035 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3036 }
3037
3038 public void noteJobStartLocked(String name, int uid) {
3039 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003040 final long elapsedRealtime = mClocks.elapsedRealtime();
3041 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003042 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3043 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3044 return;
3045 }
3046 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3047 }
3048
3049 public void noteJobFinishLocked(String name, int uid) {
3050 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003051 final long elapsedRealtime = mClocks.elapsedRealtime();
3052 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003053 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3054 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3055 return;
3056 }
3057 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3058 }
3059
Dianne Hackborn1e383822015-04-10 14:02:33 -07003060 public void noteAlarmStartLocked(String name, int uid) {
3061 if (!mRecordAllHistory) {
3062 return;
3063 }
3064 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003065 final long elapsedRealtime = mClocks.elapsedRealtime();
3066 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003067 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3068 return;
3069 }
3070 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3071 }
3072
3073 public void noteAlarmFinishLocked(String name, int uid) {
3074 if (!mRecordAllHistory) {
3075 return;
3076 }
3077 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003078 final long elapsedRealtime = mClocks.elapsedRealtime();
3079 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003080 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3081 return;
3082 }
3083 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3084 }
3085
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003086 private void requestWakelockCpuUpdate() {
3087 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3088 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3089 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3090 }
3091 }
3092
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003093 private void requestImmediateCpuUpdate() {
3094 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3095 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3096 }
3097
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003098 public void setRecordAllHistoryLocked(boolean enabled) {
3099 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003100 if (!enabled) {
3101 // Clear out any existing state.
3102 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003103 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003104 // Record the currently running processes as stopping, now that we are no
3105 // longer tracking them.
3106 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3107 HistoryItem.EVENT_PROC);
3108 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003109 long mSecRealtime = mClocks.elapsedRealtime();
3110 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003111 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3112 SparseIntArray uids = ent.getValue();
3113 for (int j=0; j<uids.size(); j++) {
3114 addHistoryEventLocked(mSecRealtime, mSecUptime,
3115 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3116 }
3117 }
3118 }
3119 } else {
3120 // Record the currently running processes as starting, now that we are tracking them.
3121 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3122 HistoryItem.EVENT_PROC);
3123 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003124 long mSecRealtime = mClocks.elapsedRealtime();
3125 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003126 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3127 SparseIntArray uids = ent.getValue();
3128 for (int j=0; j<uids.size(); j++) {
3129 addHistoryEventLocked(mSecRealtime, mSecUptime,
3130 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3131 }
3132 }
3133 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003134 }
3135 }
3136
Dianne Hackborn9a755432014-05-15 17:05:22 -07003137 public void setNoAutoReset(boolean enabled) {
3138 mNoAutoReset = enabled;
3139 }
3140
3141 private String mInitialAcquireWakeName;
3142 private int mInitialAcquireWakeUid = -1;
3143
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003144 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003145 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003146 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003147 if (type == WAKE_TYPE_PARTIAL) {
3148 // Only care about partial wake locks, since full wake locks
3149 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003150 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003151 if (historyName == null) {
3152 historyName = name;
3153 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003154 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003155 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3156 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003157 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003158 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003159 }
3160 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003161 if (mWakeLockNesting == 0) {
3162 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3163 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3164 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003165 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003166 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003167 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003168 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003169 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003170 } else if (!mWakeLockImportant && !unimportantForLogging
3171 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003172 if (mHistoryLastWritten.wakelockTag != null) {
3173 // We'll try to update the last tag.
3174 mHistoryLastWritten.wakelockTag = null;
3175 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003176 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003177 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003178 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003179 }
3180 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003181 }
3182 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003183 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003184 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003185 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3186 // We only update the cpu time when a wake lock is acquired if the screen is off.
3187 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3188 if (DEBUG_ENERGY_CPU) {
3189 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3190 }
3191 requestWakelockCpuUpdate();
3192 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003193 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003194 }
3195 }
3196
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003197 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3198 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003199 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003200 if (type == WAKE_TYPE_PARTIAL) {
3201 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003202 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003203 if (historyName == null) {
3204 historyName = name;
3205 }
3206 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3207 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003208 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003209 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003210 }
3211 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003212 if (mWakeLockNesting == 0) {
3213 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3214 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3215 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003216 mInitialAcquireWakeName = null;
3217 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003218 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003219 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003220 }
3221 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003222 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3223 if (DEBUG_ENERGY_CPU) {
3224 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3225 }
3226 requestWakelockCpuUpdate();
3227 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003228 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003229 }
3230 }
3231
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003232 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3233 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003234 final long elapsedRealtime = mClocks.elapsedRealtime();
3235 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003236 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003237 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003238 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003239 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003240 }
3241 }
3242
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003243 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3244 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003245 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003246 final long elapsedRealtime = mClocks.elapsedRealtime();
3247 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003248 // For correct semantics, we start the need worksources first, so that we won't
3249 // make inappropriate history items as if all wake locks went away and new ones
3250 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003251 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003252 for (int i=0; i<NN; i++) {
3253 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003254 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003255 }
3256 final int NO = ws.size();
3257 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003258 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003259 }
3260 }
3261
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003262 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3263 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003264 final long elapsedRealtime = mClocks.elapsedRealtime();
3265 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003266 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003267 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003268 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003269 }
3270 }
3271
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003272 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3273 if (mLastWakeupReason != null) {
3274 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003275 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003276 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003277 mLastWakeupReason = null;
3278 }
3279 }
3280
3281 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003282 final long elapsedRealtime = mClocks.elapsedRealtime();
3283 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003284 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003285 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003286 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003287 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3288 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003289 mHistoryCur.wakeReasonTag.uid = 0;
3290 mLastWakeupReason = reason;
3291 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003292 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003293 }
3294
Adam Lesinski72478f02015-06-17 15:39:43 -07003295 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003296 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003297 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003298 }
3299
Adam Lesinski72478f02015-06-17 15:39:43 -07003300 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3301 int statSystemTime, int statIOWaitTime, int statIrqTime,
3302 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003303 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3304 + " user=" + statUserTime + " sys=" + statSystemTime
3305 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3306 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3307 mCurStepCpuUserTime += totalUTime;
3308 mCurStepCpuSystemTime += totalSTime;
3309 mCurStepStatUserTime += statUserTime;
3310 mCurStepStatSystemTime += statSystemTime;
3311 mCurStepStatIOWaitTime += statIOWaitTime;
3312 mCurStepStatIrqTime += statIrqTime;
3313 mCurStepStatSoftIrqTime += statSoftIrqTime;
3314 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003315 }
3316
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003317 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003318 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003319 Uid u = mUidStats.get(uid);
3320 if (u != null) {
3321 u.mPids.remove(pid);
3322 }
3323 }
3324
3325 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003326 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003327 Uid u = mUidStats.get(uid);
3328 if (u != null) {
3329 Uid.Pid p = u.mPids.get(pid);
3330 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003331 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003332 }
3333 }
3334 return 0;
3335 }
3336
3337 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003338 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003339 Uid u = mUidStats.get(uid);
3340 if (u != null) {
3341 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3342 }
3343 }
3344
Dianne Hackborn287952c2010-09-22 22:34:31 -07003345 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003346 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003347 Uid u = mUidStats.get(uid);
3348 if (u != null) {
3349 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3350 }
3351 }
3352
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003353 int mSensorNesting;
3354
3355 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003356 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003357 final long elapsedRealtime = mClocks.elapsedRealtime();
3358 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003359 if (mSensorNesting == 0) {
3360 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3361 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3362 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003363 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003364 }
3365 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003366 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003367 }
3368
3369 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003370 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003371 final long elapsedRealtime = mClocks.elapsedRealtime();
3372 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003373 mSensorNesting--;
3374 if (mSensorNesting == 0) {
3375 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3376 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3377 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003378 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003379 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003380 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003381 }
3382
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003383 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003384
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003385 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003386 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003387 final long elapsedRealtime = mClocks.elapsedRealtime();
3388 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003389 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003390 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003391 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3392 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003393 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003394 }
3395 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003396 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003398
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003399 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003400 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003401 final long elapsedRealtime = mClocks.elapsedRealtime();
3402 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003403 mGpsNesting--;
3404 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003405 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003406 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3407 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003408 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003409 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003410 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003412
Jeff Browne95c3cd2014-05-02 16:59:26 -07003413 public void noteScreenStateLocked(int state) {
3414 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003415 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003416 final int oldState = mScreenState;
3417 mScreenState = state;
3418 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3419 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003420
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003421 if (state != Display.STATE_UNKNOWN) {
3422 int stepState = state-1;
3423 if (stepState < 4) {
3424 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3425 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3426 } else {
3427 Slog.wtf(TAG, "Unexpected screen state: " + state);
3428 }
3429 }
3430
Jeff Browne95c3cd2014-05-02 16:59:26 -07003431 if (state == Display.STATE_ON) {
3432 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003433 final long elapsedRealtime = mClocks.elapsedRealtime();
3434 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003435 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3436 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3437 + Integer.toHexString(mHistoryCur.states));
3438 addHistoryRecordLocked(elapsedRealtime, uptime);
3439 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3440 if (mScreenBrightnessBin >= 0) {
3441 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3442 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003443
Jeff Browne95c3cd2014-05-02 16:59:26 -07003444 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003445 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003446
Jeff Browne95c3cd2014-05-02 16:59:26 -07003447 // Fake a wake lock, so we consider the device waked as long
3448 // as the screen is on.
3449 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3450 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003451
Jeff Browne95c3cd2014-05-02 16:59:26 -07003452 // Update discharge amounts.
3453 if (mOnBatteryInternal) {
3454 updateDischargeScreenLevelsLocked(false, true);
3455 }
3456 } else if (oldState == Display.STATE_ON) {
3457 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003458 final long elapsedRealtime = mClocks.elapsedRealtime();
3459 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003460 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3461 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3462 + Integer.toHexString(mHistoryCur.states));
3463 addHistoryRecordLocked(elapsedRealtime, uptime);
3464 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3465 if (mScreenBrightnessBin >= 0) {
3466 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3467 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003468
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003469 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003470 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003471
Jeff Browne95c3cd2014-05-02 16:59:26 -07003472 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003473 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003474
Jeff Browne95c3cd2014-05-02 16:59:26 -07003475 // Update discharge amounts.
3476 if (mOnBatteryInternal) {
3477 updateDischargeScreenLevelsLocked(true, false);
3478 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003479 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003480 }
3481 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003482
Dianne Hackborn617f8772009-03-31 15:04:46 -07003483 public void noteScreenBrightnessLocked(int brightness) {
3484 // Bin the brightness.
3485 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3486 if (bin < 0) bin = 0;
3487 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3488 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003489 final long elapsedRealtime = mClocks.elapsedRealtime();
3490 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003491 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3492 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003493 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3494 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003495 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003496 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003497 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003498 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003499 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003500 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003501 }
3502 mScreenBrightnessBin = bin;
3503 }
3504 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003505
Dianne Hackborn617f8772009-03-31 15:04:46 -07003506 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003507 if (mOnBatteryInternal) {
3508 uid = mapUid(uid);
3509 getUidStatsLocked(uid).noteUserActivityLocked(event);
3510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003512
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003513 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003514 final long elapsedRealtime = mClocks.elapsedRealtime();
3515 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003516 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3517 reason, reasonUid);
3518 }
3519
Jeff Browne95c3cd2014-05-02 16:59:26 -07003520 public void noteInteractiveLocked(boolean interactive) {
3521 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003522 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003523 mInteractive = interactive;
3524 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3525 if (interactive) {
3526 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3527 } else {
3528 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3529 }
3530 }
3531 }
3532
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003533 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003534 final long elapsedRealtime = mClocks.elapsedRealtime();
3535 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003536 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3537 extra, type);
3538 mNumConnectivityChange++;
3539 }
3540
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003541 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
Joe Onoratoabded112016-02-08 16:49:39 -08003542 final long elapsedRealtime = mClocks.elapsedRealtime();
3543 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003544 if (mMobileRadioPowerState != powerState) {
3545 long realElapsedRealtimeMs;
3546 final boolean active =
3547 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3548 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3549 if (active) {
Adam Lesinski9acfd812016-04-19 18:29:50 -07003550 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003551 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3552 } else {
3553 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003554 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003555 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3556 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3557 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003558 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003559 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3560 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3561 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003562 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003563 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3564 }
3565 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3566 + Integer.toHexString(mHistoryCur.states));
3567 addHistoryRecordLocked(elapsedRealtime, uptime);
3568 mMobileRadioPowerState = powerState;
3569 if (active) {
3570 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3571 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3572 } else {
3573 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003574 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003575 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003576 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003577 }
3578 }
3579
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003580 public void notePowerSaveMode(boolean enabled) {
3581 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003582 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3583 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3584 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08003585 final long elapsedRealtime = mClocks.elapsedRealtime();
3586 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003587 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003588 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003589 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3590 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003591 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003592 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003593 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003594 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3595 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003596 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003597 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003598 }
3599 addHistoryRecordLocked(elapsedRealtime, uptime);
3600 }
3601 }
3602
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003603 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003604 final long elapsedRealtime = mClocks.elapsedRealtime();
3605 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003606 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003607 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003608 // We don't go out of general idling mode until explicitly taken out of
3609 // device idle through going active or significant motion.
3610 nowIdling = true;
3611 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003612 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
3613 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
3614 // We don't go out of general light idling mode until explicitly taken out of
3615 // device idle through going active or significant motion.
3616 nowLightIdling = true;
3617 }
3618 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
3619 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3620 activeReason, activeUid);
3621 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003622 if (mDeviceIdling != nowIdling) {
3623 mDeviceIdling = nowIdling;
3624 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3625 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3626 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003627 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003628 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3629 } else {
3630 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3631 }
3632 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003633 if (mDeviceLightIdling != nowLightIdling) {
3634 mDeviceLightIdling = nowLightIdling;
3635 if (nowLightIdling) {
3636 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003637 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003638 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003639 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003640 }
3641 if (mDeviceIdleMode != mode) {
3642 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
3643 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
3644 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
3645 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003646 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003647 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
3648 mLastIdleTimeStart = elapsedRealtime;
3649 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
3650 if (lastDuration > mLongestLightIdleTime) {
3651 mLongestLightIdleTime = lastDuration;
3652 }
3653 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003654 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003655 if (lastDuration > mLongestFullIdleTime) {
3656 mLongestFullIdleTime = lastDuration;
3657 }
3658 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3659 }
3660 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3661 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003662 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003663 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
3664 }
3665 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003666 }
3667 }
3668
3669 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08003670 final long elapsedRealtime = mClocks.elapsedRealtime();
3671 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003672 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3673 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003674 PackageChange pc = new PackageChange();
3675 pc.mPackageName = pkgName;
3676 pc.mUpdate = true;
3677 pc.mVersionCode = versionCode;
3678 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003679 }
3680
3681 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08003682 final long elapsedRealtime = mClocks.elapsedRealtime();
3683 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003684 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3685 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003686 PackageChange pc = new PackageChange();
3687 pc.mPackageName = pkgName;
3688 pc.mUpdate = true;
3689 addPackageChange(pc);
3690 }
3691
3692 private void addPackageChange(PackageChange pc) {
3693 if (mDailyPackageChanges == null) {
3694 mDailyPackageChanges = new ArrayList<>();
3695 }
3696 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003697 }
3698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 public void notePhoneOnLocked() {
3700 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003701 final long elapsedRealtime = mClocks.elapsedRealtime();
3702 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003703 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003704 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3705 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003706 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003708 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 }
3710 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 public void notePhoneOffLocked() {
3713 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003714 final long elapsedRealtime = mClocks.elapsedRealtime();
3715 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003716 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003717 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3718 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003719 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003721 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003724
Dianne Hackborn3251b902014-06-20 14:40:53 -07003725 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08003726 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003727 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003728 if (i == except) {
3729 continue;
3730 }
3731 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003732 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003733 }
3734 }
3735 }
3736
Dianne Hackborne4a59512010-12-07 11:08:07 -08003737 private int fixPhoneServiceState(int state, int signalBin) {
3738 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3739 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3740 // to infer that we are scanning from other data.
3741 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003742 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003743 state = ServiceState.STATE_IN_SERVICE;
3744 }
3745 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003746
Dianne Hackborne4a59512010-12-07 11:08:07 -08003747 return state;
3748 }
3749
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003750 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003751 boolean scanning = false;
3752 boolean newHistory = false;
3753
3754 mPhoneServiceStateRaw = state;
3755 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003756 mPhoneSignalStrengthBinRaw = strengthBin;
3757
Joe Onoratoabded112016-02-08 16:49:39 -08003758 final long elapsedRealtime = mClocks.elapsedRealtime();
3759 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003760
3761 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3762 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3763 // to infer that we are scanning from other data.
3764 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003765 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003766 state = ServiceState.STATE_IN_SERVICE;
3767 }
3768 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003769
3770 // If the phone is powered off, stop all timers.
3771 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003772 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003773
Dianne Hackborne4a59512010-12-07 11:08:07 -08003774 // If we are in service, make sure the correct signal string timer is running.
3775 } else if (state == ServiceState.STATE_IN_SERVICE) {
3776 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003777
3778 // If we're out of service, we are in the lowest signal strength
3779 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003780 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003781 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003782 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003783 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003784 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003785 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003786 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3787 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003788 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003789 }
3790 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003791
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003792 if (!scanning) {
3793 // If we are no longer scanning, then stop the scanning timer.
3794 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3795 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3796 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3797 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003798 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003799 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003800 }
3801 }
3802
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003803 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003804 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3805 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003806 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003807 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003808 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003809 mPhoneServiceState = state;
3810 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003811
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003812 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003813 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003814 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003815 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003816 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003817 if (strengthBin >= 0) {
3818 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003819 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003820 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003821 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3822 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003823 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003824 + Integer.toHexString(mHistoryCur.states));
3825 newHistory = true;
3826 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003827 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003828 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003829 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003830 }
3831
3832 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003833 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003834 }
3835 }
3836
3837 /**
3838 * Telephony stack updates the phone state.
3839 * @param state phone state from ServiceState.getState()
3840 */
3841 public void notePhoneStateLocked(int state, int simState) {
3842 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003843 }
3844
Wink Savillee9b06d72009-05-18 21:47:50 -07003845 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003846 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003847 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003848 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003849 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003850
Dianne Hackborn627bba72009-03-24 22:32:56 -07003851 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3852 int bin = DATA_CONNECTION_NONE;
3853 if (hasData) {
3854 switch (dataType) {
3855 case TelephonyManager.NETWORK_TYPE_EDGE:
3856 bin = DATA_CONNECTION_EDGE;
3857 break;
3858 case TelephonyManager.NETWORK_TYPE_GPRS:
3859 bin = DATA_CONNECTION_GPRS;
3860 break;
3861 case TelephonyManager.NETWORK_TYPE_UMTS:
3862 bin = DATA_CONNECTION_UMTS;
3863 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003864 case TelephonyManager.NETWORK_TYPE_CDMA:
3865 bin = DATA_CONNECTION_CDMA;
3866 break;
3867 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3868 bin = DATA_CONNECTION_EVDO_0;
3869 break;
3870 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3871 bin = DATA_CONNECTION_EVDO_A;
3872 break;
3873 case TelephonyManager.NETWORK_TYPE_1xRTT:
3874 bin = DATA_CONNECTION_1xRTT;
3875 break;
3876 case TelephonyManager.NETWORK_TYPE_HSDPA:
3877 bin = DATA_CONNECTION_HSDPA;
3878 break;
3879 case TelephonyManager.NETWORK_TYPE_HSUPA:
3880 bin = DATA_CONNECTION_HSUPA;
3881 break;
3882 case TelephonyManager.NETWORK_TYPE_HSPA:
3883 bin = DATA_CONNECTION_HSPA;
3884 break;
3885 case TelephonyManager.NETWORK_TYPE_IDEN:
3886 bin = DATA_CONNECTION_IDEN;
3887 break;
3888 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3889 bin = DATA_CONNECTION_EVDO_B;
3890 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003891 case TelephonyManager.NETWORK_TYPE_LTE:
3892 bin = DATA_CONNECTION_LTE;
3893 break;
3894 case TelephonyManager.NETWORK_TYPE_EHRPD:
3895 bin = DATA_CONNECTION_EHRPD;
3896 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003897 case TelephonyManager.NETWORK_TYPE_HSPAP:
3898 bin = DATA_CONNECTION_HSPAP;
3899 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003900 default:
3901 bin = DATA_CONNECTION_OTHER;
3902 break;
3903 }
3904 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003905 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003906 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003907 final long elapsedRealtime = mClocks.elapsedRealtime();
3908 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003909 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3910 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003911 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3912 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003913 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003914 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003915 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003916 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003917 }
3918 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003919 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003920 }
3921 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003922
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003923 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003924 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003925 final long elapsedRealtime = mClocks.elapsedRealtime();
3926 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003927 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003928 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3929 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003930 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003931 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003932 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003933 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003934 }
3935 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003936
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003937 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08003938 final long elapsedRealtime = mClocks.elapsedRealtime();
3939 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003940 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003941 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003942 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3943 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003944 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003945 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003946 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003947 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003948 }
3949 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003950
3951 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003952 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003953 final long elapsedRealtime = mClocks.elapsedRealtime();
3954 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003955 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003956 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003957 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3958 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003959 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003960 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003961 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003962 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003963 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003964 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003965
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003966 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003967 if (mAudioOnNesting == 0) {
3968 return;
3969 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003970 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003971 final long elapsedRealtime = mClocks.elapsedRealtime();
3972 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003973 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003974 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003975 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3976 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003977 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003978 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003979 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003980 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003981 }
3982
3983 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003984 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003985 final long elapsedRealtime = mClocks.elapsedRealtime();
3986 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003987 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003988 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003989 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3990 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003991 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003992 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003993 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003994 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003995 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003996 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003997
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003998 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003999 if (mVideoOnNesting == 0) {
4000 return;
4001 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004002 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004003 final long elapsedRealtime = mClocks.elapsedRealtime();
4004 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004005 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004006 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004007 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4008 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004009 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004010 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004011 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004012 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004013 }
4014
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004015 public void noteResetAudioLocked() {
4016 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004017 final long elapsedRealtime = mClocks.elapsedRealtime();
4018 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004019 mAudioOnNesting = 0;
4020 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4021 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4022 + Integer.toHexString(mHistoryCur.states));
4023 addHistoryRecordLocked(elapsedRealtime, uptime);
4024 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4025 for (int i=0; i<mUidStats.size(); i++) {
4026 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4027 uid.noteResetAudioLocked(elapsedRealtime);
4028 }
4029 }
4030 }
4031
4032 public void noteResetVideoLocked() {
4033 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004034 final long elapsedRealtime = mClocks.elapsedRealtime();
4035 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004036 mAudioOnNesting = 0;
4037 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4038 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4039 + Integer.toHexString(mHistoryCur.states));
4040 addHistoryRecordLocked(elapsedRealtime, uptime);
4041 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4042 for (int i=0; i<mUidStats.size(); i++) {
4043 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4044 uid.noteResetVideoLocked(elapsedRealtime);
4045 }
4046 }
4047 }
4048
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004049 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004050 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004051 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004052 }
4053
4054 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004055 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004056 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004057 }
4058
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004059 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004060 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004061 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4062 }
4063
4064 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004065 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004066 getUidStatsLocked(uid).noteVibratorOffLocked();
4067 }
4068
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004069 public void noteFlashlightOnLocked(int uid) {
4070 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004071 final long elapsedRealtime = mClocks.elapsedRealtime();
4072 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004073 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004074 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4075 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004076 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004077 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004078 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4079 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004080 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4081 }
4082
4083 public void noteFlashlightOffLocked(int uid) {
4084 if (mFlashlightOnNesting == 0) {
4085 return;
4086 }
4087 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004088 final long elapsedRealtime = mClocks.elapsedRealtime();
4089 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004090 if (--mFlashlightOnNesting == 0) {
4091 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4092 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4093 + Integer.toHexString(mHistoryCur.states2));
4094 addHistoryRecordLocked(elapsedRealtime, uptime);
4095 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4096 }
4097 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4098 }
4099
4100 public void noteCameraOnLocked(int uid) {
4101 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004102 final long elapsedRealtime = mClocks.elapsedRealtime();
4103 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004104 if (mCameraOnNesting++ == 0) {
4105 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4106 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4107 + Integer.toHexString(mHistoryCur.states2));
4108 addHistoryRecordLocked(elapsedRealtime, uptime);
4109 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4110 }
4111 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4112 }
4113
4114 public void noteCameraOffLocked(int uid) {
4115 if (mCameraOnNesting == 0) {
4116 return;
4117 }
4118 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004119 final long elapsedRealtime = mClocks.elapsedRealtime();
4120 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004121 if (--mCameraOnNesting == 0) {
4122 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4123 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4124 + Integer.toHexString(mHistoryCur.states2));
4125 addHistoryRecordLocked(elapsedRealtime, uptime);
4126 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4127 }
4128 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4129 }
4130
4131 public void noteResetCameraLocked() {
4132 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004133 final long elapsedRealtime = mClocks.elapsedRealtime();
4134 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004135 mCameraOnNesting = 0;
4136 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4137 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4138 + Integer.toHexString(mHistoryCur.states2));
4139 addHistoryRecordLocked(elapsedRealtime, uptime);
4140 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4141 for (int i=0; i<mUidStats.size(); i++) {
4142 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4143 uid.noteResetCameraLocked(elapsedRealtime);
4144 }
4145 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004146 }
4147
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004148 public void noteResetFlashlightLocked() {
4149 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004150 final long elapsedRealtime = mClocks.elapsedRealtime();
4151 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004152 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004153 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4154 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004155 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004156 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004157 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4158 for (int i=0; i<mUidStats.size(); i++) {
4159 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4160 uid.noteResetFlashlightLocked(elapsedRealtime);
4161 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004162 }
4163 }
4164
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004165 private void noteBluetoothScanStartedLocked(int uid) {
4166 uid = mapUid(uid);
4167 final long elapsedRealtime = SystemClock.elapsedRealtime();
4168 final long uptime = SystemClock.uptimeMillis();
4169 if (mBluetoothScanNesting == 0) {
4170 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4171 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4172 + Integer.toHexString(mHistoryCur.states2));
4173 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004174 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004175 }
4176 mBluetoothScanNesting++;
4177 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4178 }
4179
4180 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4181 final int N = ws.size();
4182 for (int i = 0; i < N; i++) {
4183 noteBluetoothScanStartedLocked(ws.get(i));
4184 }
4185 }
4186
4187 private void noteBluetoothScanStoppedLocked(int uid) {
4188 uid = mapUid(uid);
4189 final long elapsedRealtime = SystemClock.elapsedRealtime();
4190 final long uptime = SystemClock.uptimeMillis();
4191 mBluetoothScanNesting--;
4192 if (mBluetoothScanNesting == 0) {
4193 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4194 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4195 + Integer.toHexString(mHistoryCur.states2));
4196 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004197 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004198 }
4199 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4200 }
4201
4202 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4203 final int N = ws.size();
4204 for (int i = 0; i < N; i++) {
4205 noteBluetoothScanStoppedLocked(ws.get(i));
4206 }
4207 }
4208
4209 public void noteResetBluetoothScanLocked() {
4210 if (mBluetoothScanNesting > 0) {
4211 final long elapsedRealtime = SystemClock.elapsedRealtime();
4212 final long uptime = SystemClock.uptimeMillis();
4213 mBluetoothScanNesting = 0;
4214 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4215 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4216 + Integer.toHexString(mHistoryCur.states2));
4217 addHistoryRecordLocked(elapsedRealtime, uptime);
4218 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4219 for (int i=0; i<mUidStats.size(); i++) {
4220 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4221 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4222 }
4223 }
4224 }
4225
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004226 public void noteWifiRadioPowerState(int powerState, long timestampNs) {
Joe Onoratoabded112016-02-08 16:49:39 -08004227 final long elapsedRealtime = mClocks.elapsedRealtime();
4228 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004229 if (mWifiRadioPowerState != powerState) {
4230 final boolean active =
4231 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4232 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4233 if (active) {
4234 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4235 } else {
4236 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4237 }
4238 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4239 + Integer.toHexString(mHistoryCur.states));
4240 addHistoryRecordLocked(elapsedRealtime, uptime);
4241 mWifiRadioPowerState = powerState;
4242 }
4243 }
4244
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004245 public void noteWifiRunningLocked(WorkSource ws) {
4246 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004247 final long elapsedRealtime = mClocks.elapsedRealtime();
4248 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004249 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004250 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4251 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004252 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004253 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004254 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004255 int N = ws.size();
4256 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004257 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004258 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004259 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004260 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004261 } else {
4262 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004263 }
4264 }
4265
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004266 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4267 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004268 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004269 int N = oldWs.size();
4270 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004271 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004272 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004273 }
4274 N = newWs.size();
4275 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004276 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004277 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004278 }
4279 } else {
4280 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4281 }
4282 }
4283
4284 public void noteWifiStoppedLocked(WorkSource ws) {
4285 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004286 final long elapsedRealtime = mClocks.elapsedRealtime();
4287 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004288 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004289 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4290 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004291 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004292 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004293 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004294 int N = ws.size();
4295 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004296 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004297 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004298 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004299 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004300 } else {
4301 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004302 }
4303 }
4304
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004305 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4306 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4307 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004308 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004309 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004310 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004311 }
4312 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004313 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004314 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004315 }
4316 }
4317
Dianne Hackborn3251b902014-06-20 14:40:53 -07004318 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4319 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4320 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004321 final long elapsedRealtime = mClocks.elapsedRealtime();
4322 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004323 if (mWifiSupplState >= 0) {
4324 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4325 }
4326 mWifiSupplState = supplState;
4327 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4328 mHistoryCur.states2 =
4329 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4330 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4331 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4332 + Integer.toHexString(mHistoryCur.states2));
4333 addHistoryRecordLocked(elapsedRealtime, uptime);
4334 }
4335 }
4336
4337 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004338 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004339 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4340 if (i == except) {
4341 continue;
4342 }
4343 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4344 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4345 }
4346 }
4347 }
4348
4349 public void noteWifiRssiChangedLocked(int newRssi) {
4350 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4351 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4352 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004353 final long elapsedRealtime = mClocks.elapsedRealtime();
4354 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004355 if (mWifiSignalStrengthBin >= 0) {
4356 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4357 elapsedRealtime);
4358 }
4359 if (strengthBin >= 0) {
4360 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4361 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4362 }
4363 mHistoryCur.states2 =
4364 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4365 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4366 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4367 + Integer.toHexString(mHistoryCur.states2));
4368 addHistoryRecordLocked(elapsedRealtime, uptime);
4369 } else {
4370 stopAllWifiSignalStrengthTimersLocked(-1);
4371 }
4372 mWifiSignalStrengthBin = strengthBin;
4373 }
4374 }
4375
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004376 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004377
The Android Open Source Project10592532009-03-18 17:39:46 -07004378 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004379 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004380 final long elapsedRealtime = mClocks.elapsedRealtime();
4381 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004382 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004383 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004384 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4385 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004386 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004387 }
4388 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004389 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004390 }
4391
4392 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004393 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004394 final long elapsedRealtime = mClocks.elapsedRealtime();
4395 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004396 mWifiFullLockNesting--;
4397 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004398 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004399 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4400 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004401 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004402 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004403 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004404 }
4405
Nick Pelly6ccaa542012-06-15 15:22:47 -07004406 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004407
Nick Pelly6ccaa542012-06-15 15:22:47 -07004408 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004409 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004410 final long elapsedRealtime = mClocks.elapsedRealtime();
4411 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004412 if (mWifiScanNesting == 0) {
4413 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4414 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004415 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004416 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004417 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004418 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004419 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004420 }
4421
Nick Pelly6ccaa542012-06-15 15:22:47 -07004422 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004423 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004424 final long elapsedRealtime = mClocks.elapsedRealtime();
4425 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004426 mWifiScanNesting--;
4427 if (mWifiScanNesting == 0) {
4428 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4429 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004430 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004431 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004432 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004433 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004434 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004435
Robert Greenwalta029ea12013-09-25 16:38:12 -07004436 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004437 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004438 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004439 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004440 }
4441
4442 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004443 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004444 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004445 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004446 }
4447
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004448 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004449
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004450 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004451 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004452 final long elapsedRealtime = mClocks.elapsedRealtime();
4453 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004454 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004455 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004456 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4457 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004458 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004459 }
4460 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004461 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004462 }
4463
4464 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004465 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004466 final long elapsedRealtime = mClocks.elapsedRealtime();
4467 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004468 mWifiMulticastNesting--;
4469 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004470 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004471 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4472 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004473 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004474 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004475 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004476 }
4477
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004478 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4479 int N = ws.size();
4480 for (int i=0; i<N; i++) {
4481 noteFullWifiLockAcquiredLocked(ws.get(i));
4482 }
4483 }
4484
4485 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4486 int N = ws.size();
4487 for (int i=0; i<N; i++) {
4488 noteFullWifiLockReleasedLocked(ws.get(i));
4489 }
4490 }
4491
Nick Pelly6ccaa542012-06-15 15:22:47 -07004492 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004493 int N = ws.size();
4494 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004495 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004496 }
4497 }
4498
Nick Pelly6ccaa542012-06-15 15:22:47 -07004499 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004500 int N = ws.size();
4501 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004502 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004503 }
4504 }
4505
Robert Greenwalta029ea12013-09-25 16:38:12 -07004506 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4507 int N = ws.size();
4508 for (int i=0; i<N; i++) {
4509 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4510 }
4511 }
4512
4513 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4514 int N = ws.size();
4515 for (int i=0; i<N; i++) {
4516 noteWifiBatchedScanStoppedLocked(ws.get(i));
4517 }
4518 }
4519
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004520 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4521 int N = ws.size();
4522 for (int i=0; i<N; i++) {
4523 noteWifiMulticastEnabledLocked(ws.get(i));
4524 }
4525 }
4526
4527 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4528 int N = ws.size();
4529 for (int i=0; i<N; i++) {
4530 noteWifiMulticastDisabledLocked(ws.get(i));
4531 }
4532 }
4533
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004534 private static String[] includeInStringArray(String[] array, String str) {
4535 if (ArrayUtils.indexOf(array, str) >= 0) {
4536 return array;
4537 }
4538 String[] newArray = new String[array.length+1];
4539 System.arraycopy(array, 0, newArray, 0, array.length);
4540 newArray[array.length] = str;
4541 return newArray;
4542 }
4543
4544 private static String[] excludeFromStringArray(String[] array, String str) {
4545 int index = ArrayUtils.indexOf(array, str);
4546 if (index >= 0) {
4547 String[] newArray = new String[array.length-1];
4548 if (index > 0) {
4549 System.arraycopy(array, 0, newArray, 0, index);
4550 }
4551 if (index < array.length-1) {
4552 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4553 }
4554 return newArray;
4555 }
4556 return array;
4557 }
4558
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004559 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004560 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004561 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004562 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4563 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004564 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004565 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4566 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004567 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004568 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004569 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4570 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004571 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004572 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4573 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004574 }
4575 }
4576
4577 public void noteNetworkStatsEnabledLocked() {
4578 // During device boot, qtaguid isn't enabled until after the inital
4579 // loading of battery stats. Now that they're enabled, take our initial
4580 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08004581 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004582 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004583 }
4584
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004585 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4586 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004588
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004589 @Override public int getScreenOnCount(int which) {
4590 return mScreenOnTimer.getCountLocked(which);
4591 }
4592
Dianne Hackborn617f8772009-03-31 15:04:46 -07004593 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004594 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004595 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004596 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004597 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004598
Jeff Browne95c3cd2014-05-02 16:59:26 -07004599 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4600 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004601 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004602
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004603 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4604 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004605 }
4606
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004607 @Override public int getPowerSaveModeEnabledCount(int which) {
4608 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4609 }
4610
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004611 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
4612 int which) {
4613 switch (mode) {
4614 case DEVICE_IDLE_MODE_LIGHT:
4615 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004616 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004617 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4618 }
4619 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004620 }
4621
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004622 @Override public int getDeviceIdleModeCount(int mode, int which) {
4623 switch (mode) {
4624 case DEVICE_IDLE_MODE_LIGHT:
4625 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004626 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004627 return mDeviceIdleModeFullTimer.getCountLocked(which);
4628 }
4629 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004630 }
4631
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004632 @Override public long getLongestDeviceIdleModeTime(int mode) {
4633 switch (mode) {
4634 case DEVICE_IDLE_MODE_LIGHT:
4635 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004636 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004637 return mLongestFullIdleTime;
4638 }
4639 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004640 }
4641
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004642 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
4643 switch (mode) {
4644 case DEVICE_IDLE_MODE_LIGHT:
4645 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004646 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004647 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4648 }
4649 return 0;
4650 }
4651
4652 @Override public int getDeviceIdlingCount(int mode, int which) {
4653 switch (mode) {
4654 case DEVICE_IDLE_MODE_LIGHT:
4655 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004656 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004657 return mDeviceIdlingTimer.getCountLocked(which);
4658 }
4659 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004660 }
4661
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004662 @Override public int getNumConnectivityChange(int which) {
4663 int val = mNumConnectivityChange;
4664 if (which == STATS_CURRENT) {
4665 val -= mLoadedNumConnectivityChange;
4666 } else if (which == STATS_SINCE_UNPLUGGED) {
4667 val -= mUnpluggedNumConnectivityChange;
4668 }
4669 return val;
4670 }
4671
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004672 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4673 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004675
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004676 @Override public int getPhoneOnCount(int which) {
4677 return mPhoneOnTimer.getCountLocked(which);
4678 }
4679
Dianne Hackborn627bba72009-03-24 22:32:56 -07004680 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004681 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004682 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004683 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004684 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004685
4686 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004687 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004688 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004689 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004690 }
4691
Catherine Liufb900812012-07-17 14:12:56 -05004692 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4693 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004694 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004695
Dianne Hackborn627bba72009-03-24 22:32:56 -07004696 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004697 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004698 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004699 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004700 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004701
Dianne Hackborn617f8772009-03-31 15:04:46 -07004702 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004703 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004704 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004705
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004706 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4707 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004708 }
4709
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004710 @Override public int getMobileRadioActiveCount(int which) {
4711 return mMobileRadioActiveTimer.getCountLocked(which);
4712 }
4713
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004714 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4715 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4716 }
4717
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004718 @Override public long getMobileRadioActiveUnknownTime(int which) {
4719 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4720 }
4721
4722 @Override public int getMobileRadioActiveUnknownCount(int which) {
4723 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4724 }
4725
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004726 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4727 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004728 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004729
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004730 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4731 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004732 }
4733
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004734 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004735 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004736 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004737 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004738 }
4739
4740 @Override public int getWifiStateCount(int wifiState, int which) {
4741 return mWifiStateTimer[wifiState].getCountLocked(which);
4742 }
4743
Dianne Hackborn3251b902014-06-20 14:40:53 -07004744 @Override public long getWifiSupplStateTime(int state,
4745 long elapsedRealtimeUs, int which) {
4746 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4747 elapsedRealtimeUs, which);
4748 }
4749
4750 @Override public int getWifiSupplStateCount(int state, int which) {
4751 return mWifiSupplStateTimer[state].getCountLocked(which);
4752 }
4753
4754 @Override public long getWifiSignalStrengthTime(int strengthBin,
4755 long elapsedRealtimeUs, int which) {
4756 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4757 elapsedRealtimeUs, which);
4758 }
4759
4760 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4761 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4762 }
4763
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004764 @Override
4765 public ControllerActivityCounter getBluetoothControllerActivity() {
4766 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004767 }
4768
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004769 @Override
4770 public ControllerActivityCounter getWifiControllerActivity() {
4771 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07004772 }
4773
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004774 @Override
4775 public ControllerActivityCounter getModemControllerActivity() {
4776 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004777 }
4778
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004779 @Override
4780 public boolean hasBluetoothActivityReporting() {
4781 return mHasBluetoothReporting;
4782 }
4783
4784 @Override
4785 public boolean hasWifiActivityReporting() {
4786 return mHasWifiReporting;
4787 }
4788
4789 @Override
4790 public boolean hasModemActivityReporting() {
4791 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07004792 }
4793
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004794 @Override
4795 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004796 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4797 }
4798
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004799 @Override
4800 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004801 return mFlashlightOnTimer.getCountLocked(which);
4802 }
4803
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004804 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004805 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4806 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4807 }
4808
4809 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004810 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
4811 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4812 }
4813
4814 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004815 public long getNetworkActivityBytes(int type, int which) {
4816 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4817 return mNetworkByteActivityCounters[type].getCountLocked(which);
4818 } else {
4819 return 0;
4820 }
4821 }
4822
4823 @Override
4824 public long getNetworkActivityPackets(int type, int which) {
4825 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4826 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004827 } else {
4828 return 0;
4829 }
4830 }
4831
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004832 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07004833 final long currentTime = System.currentTimeMillis();
4834 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08004835 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
4836 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004837 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004838 return mStartClockTime;
4839 }
4840
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004841 @Override public String getStartPlatformVersion() {
4842 return mStartPlatformVersion;
4843 }
4844
4845 @Override public String getEndPlatformVersion() {
4846 return mEndPlatformVersion;
4847 }
4848
4849 @Override public int getParcelVersion() {
4850 return VERSION;
4851 }
4852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 @Override public boolean getIsOnBattery() {
4854 return mOnBattery;
4855 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4858 return mUidStats;
4859 }
4860
4861 /**
4862 * The statistics associated with a particular uid.
4863 */
Joe Onoratoabded112016-02-08 16:49:39 -08004864 public static class Uid extends BatteryStats.Uid {
4865 /**
4866 * BatteryStatsImpl that we are associated with.
4867 */
4868 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004871
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004872 boolean mWifiRunning;
4873 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004874
The Android Open Source Project10592532009-03-18 17:39:46 -07004875 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07004876 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004877
Nick Pelly6ccaa542012-06-15 15:22:47 -07004878 boolean mWifiScanStarted;
4879 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004880
Dianne Hackborn61659e52014-07-09 16:13:01 -07004881 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07004882 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4883 StopwatchTimer[] mWifiBatchedScanTimer;
4884
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004885 boolean mWifiMulticastEnabled;
4886 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004887
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004888 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004889 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004890 StopwatchTimer mFlashlightTurnedOnTimer;
4891 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004892 StopwatchTimer mForegroundActivityTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004893 StopwatchTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004894
Dianne Hackborna8d10942015-11-19 17:55:19 -08004895 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004896 StopwatchTimer[] mProcessStateTimer;
4897
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004898 BatchTimer mVibratorOnTimer;
4899
Dianne Hackborn617f8772009-03-31 15:04:46 -07004900 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004901
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004902 LongSamplingCounter[] mNetworkByteActivityCounters;
4903 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004904 LongSamplingCounter mMobileRadioActiveTime;
4905 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004907 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07004908 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004909 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004910 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004911 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004912
4913 /**
4914 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004915 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004916 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004917 private ControllerActivityCounterImpl mBluetoothControllerActivity;
4918
4919 /**
4920 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
4921 * Can be null if the UID has had no such activity.
4922 */
4923 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004924
4925 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004926 * The CPU times we had at the last history details update.
4927 */
4928 long mLastStepUserTime;
4929 long mLastStepSystemTime;
4930 long mCurStepUserTime;
4931 long mCurStepSystemTime;
4932
Joe Onoratoabded112016-02-08 16:49:39 -08004933 LongSamplingCounter mUserCpuTime;
4934 LongSamplingCounter mSystemCpuTime;
4935 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07004936 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004937
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 * The statistics we have collected for this uid's wake locks.
4940 */
Joe Onoratoabded112016-02-08 16:49:39 -08004941 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942
4943 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004944 * The statistics we have collected for this uid's syncs.
4945 */
Joe Onoratoabded112016-02-08 16:49:39 -08004946 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004947
4948 /**
4949 * The statistics we have collected for this uid's jobs.
4950 */
Joe Onoratoabded112016-02-08 16:49:39 -08004951 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004952
4953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 * The statistics we have collected for this uid's sensor activations.
4955 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004956 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957
4958 /**
4959 * The statistics we have collected for this uid's processes.
4960 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004961 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962
4963 /**
4964 * The statistics we have collected for this uid's processes.
4965 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004966 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004967
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004968 /**
4969 * The transient wake stats we have collected for this uid's pids.
4970 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004971 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004972
Joe Onoratoabded112016-02-08 16:49:39 -08004973 public Uid(BatteryStatsImpl bsi, int uid) {
4974 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08004976
4977 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4978 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4979 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4980
4981 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>() {
4982 @Override public Wakelock instantiateObject() {
4983 return new Wakelock(mBsi, Uid.this);
4984 }
4985 };
4986 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
4987 @Override public StopwatchTimer instantiateObject() {
4988 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
4989 mBsi.mOnBatteryTimeBase);
4990 }
4991 };
4992 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
4993 @Override public StopwatchTimer instantiateObject() {
4994 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
4995 mBsi.mOnBatteryTimeBase);
4996 }
4997 };
4998
4999 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5000 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5001 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5002 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
5003 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_SCAN,
5004 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005005 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005006 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5007 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005008 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 }
5010
5011 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005012 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005013 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 }
5015
5016 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005017 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005018 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005019 }
5020
5021 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005022 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005023 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005024 }
5025
5026 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005027 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 return mSensorStats;
5029 }
5030
5031 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005032 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 return mProcessStats;
5034 }
5035
5036 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005037 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 return mPackageStats;
5039 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005040
5041 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 public int getUid() {
5043 return mUid;
5044 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005045
5046 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005047 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005048 if (!mWifiRunning) {
5049 mWifiRunning = true;
5050 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005051 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5052 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005053 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005054 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005055 }
5056 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005057
Dianne Hackborn617f8772009-03-31 15:04:46 -07005058 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005059 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005060 if (mWifiRunning) {
5061 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005062 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005063 }
5064 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005065
Dianne Hackborn617f8772009-03-31 15:04:46 -07005066 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005067 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005068 if (!mFullWifiLockOut) {
5069 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005070 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005071 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5072 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005073 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005074 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005075 }
5076 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005077
The Android Open Source Project10592532009-03-18 17:39:46 -07005078 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005079 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005080 if (mFullWifiLockOut) {
5081 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005082 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005083 }
5084 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005085
The Android Open Source Project10592532009-03-18 17:39:46 -07005086 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005087 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005088 if (!mWifiScanStarted) {
5089 mWifiScanStarted = true;
5090 if (mWifiScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005091 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5092 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005093 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005094 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005095 }
5096 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005097
The Android Open Source Project10592532009-03-18 17:39:46 -07005098 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005099 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005100 if (mWifiScanStarted) {
5101 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005102 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005103 }
5104 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005105
5106 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005107 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005108 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005109 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005110 csph = csph >> 3;
5111 bin++;
5112 }
5113
5114 if (mWifiBatchedScanBinStarted == bin) return;
5115
5116 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5117 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005118 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005119 }
5120 mWifiBatchedScanBinStarted = bin;
5121 if (mWifiBatchedScanTimer[bin] == null) {
5122 makeWifiBatchedScanBin(bin, null);
5123 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005124 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005125 }
5126
5127 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005128 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005129 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5130 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005131 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005132 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5133 }
5134 }
5135
5136 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005137 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005138 if (!mWifiMulticastEnabled) {
5139 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005140 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005141 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5142 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005143 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005144 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005145 }
5146 }
5147
5148 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005149 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005150 if (mWifiMulticastEnabled) {
5151 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005152 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005153 }
5154 }
5155
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005156 @Override
5157 public ControllerActivityCounter getWifiControllerActivity() {
5158 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005159 }
5160
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005161 @Override
5162 public ControllerActivityCounter getBluetoothControllerActivity() {
5163 return mBluetoothControllerActivity;
5164 }
5165
5166 @Override
5167 public ControllerActivityCounter getModemControllerActivity() {
5168 return mModemControllerActivity;
5169 }
5170
5171 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5172 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005173 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005174 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005175 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005176 return mWifiControllerActivity;
5177 }
5178
5179 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5180 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005181 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005182 NUM_BT_TX_LEVELS);
5183 }
5184 return mBluetoothControllerActivity;
5185 }
5186
5187 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5188 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005189 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005190 ModemActivityInfo.TX_POWER_LEVELS);
5191 }
5192 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005193 }
5194
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005195 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5196 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005197 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5198 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005199 }
5200 return mAudioTurnedOnTimer;
5201 }
5202
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005203 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005204 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5205 }
5206
5207 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5208 if (mAudioTurnedOnTimer != null) {
5209 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005210 }
5211 }
5212
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005213 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5214 if (mAudioTurnedOnTimer != null) {
5215 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005216 }
5217 }
5218
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005219 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5220 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005221 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5222 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005223 }
5224 return mVideoTurnedOnTimer;
5225 }
5226
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005227 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005228 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5229 }
5230
5231 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5232 if (mVideoTurnedOnTimer != null) {
5233 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005234 }
5235 }
5236
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005237 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5238 if (mVideoTurnedOnTimer != null) {
5239 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005240 }
5241 }
5242
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005243 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5244 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005245 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5246 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005247 }
5248 return mFlashlightTurnedOnTimer;
5249 }
5250
5251 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5252 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5253 }
5254
5255 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5256 if (mFlashlightTurnedOnTimer != null) {
5257 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5258 }
5259 }
5260
5261 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5262 if (mFlashlightTurnedOnTimer != null) {
5263 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5264 }
5265 }
5266
5267 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5268 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005269 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5270 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005271 }
5272 return mCameraTurnedOnTimer;
5273 }
5274
5275 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5276 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5277 }
5278
5279 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5280 if (mCameraTurnedOnTimer != null) {
5281 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5282 }
5283 }
5284
5285 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5286 if (mCameraTurnedOnTimer != null) {
5287 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5288 }
5289 }
5290
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005291 public StopwatchTimer createForegroundActivityTimerLocked() {
5292 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005293 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5294 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005295 }
5296 return mForegroundActivityTimer;
5297 }
5298
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005299 public StopwatchTimer createBluetoothScanTimerLocked() {
5300 if (mBluetoothScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005301 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5302 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005303 }
5304 return mBluetoothScanTimer;
5305 }
5306
5307 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5308 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5309 }
5310
5311 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5312 if (mBluetoothScanTimer != null) {
5313 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5314 }
5315 }
5316
5317 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5318 if (mBluetoothScanTimer != null) {
5319 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5320 }
5321 }
5322
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005323 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005324 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005325 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005326 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005327 }
5328
5329 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005330 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005331 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005332 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005333 }
5334 }
5335
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005336 public BatchTimer createVibratorOnTimerLocked() {
5337 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005338 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5339 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005340 }
5341 return mVibratorOnTimer;
5342 }
5343
5344 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005345 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005346 }
5347
5348 public void noteVibratorOffLocked() {
5349 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005350 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005351 }
5352 }
5353
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005354 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005355 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005356 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005357 return 0;
5358 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005359 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005360 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005361
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005362 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005363 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005364 if (mFullWifiLockTimer == null) {
5365 return 0;
5366 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005367 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005368 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005369
5370 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005371 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005372 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005373 return 0;
5374 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005375 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005376 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005377
5378 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005379 public int getWifiScanCount(int which) {
5380 if (mWifiScanTimer == null) {
5381 return 0;
5382 }
5383 return mWifiScanTimer.getCountLocked(which);
5384 }
5385
5386 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005387 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005388 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5389 if (mWifiBatchedScanTimer[csphBin] == null) {
5390 return 0;
5391 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005392 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005393 }
5394
5395 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005396 public int getWifiBatchedScanCount(int csphBin, int which) {
5397 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5398 if (mWifiBatchedScanTimer[csphBin] == null) {
5399 return 0;
5400 }
5401 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5402 }
5403
5404 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005405 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005406 if (mWifiMulticastTimer == null) {
5407 return 0;
5408 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005409 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005410 }
5411
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005412 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005413 public Timer getAudioTurnedOnTimer() {
5414 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005415 }
5416
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005417 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005418 public Timer getVideoTurnedOnTimer() {
5419 return mVideoTurnedOnTimer;
5420 }
5421
5422 @Override
5423 public Timer getFlashlightTurnedOnTimer() {
5424 return mFlashlightTurnedOnTimer;
5425 }
5426
5427 @Override
5428 public Timer getCameraTurnedOnTimer() {
5429 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005430 }
5431
Dianne Hackborn617f8772009-03-31 15:04:46 -07005432 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005433 public Timer getForegroundActivityTimer() {
5434 return mForegroundActivityTimer;
5435 }
5436
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005437 @Override
5438 public Timer getBluetoothScanTimer() {
5439 return mBluetoothScanTimer;
5440 }
5441
Dianne Hackborn61659e52014-07-09 16:13:01 -07005442 void makeProcessState(int i, Parcel in) {
5443 if (i < 0 || i >= NUM_PROCESS_STATE) return;
5444
5445 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005446 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5447 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005448 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005449 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5450 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005451 }
5452 }
5453
5454 @Override
5455 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
5456 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
5457 if (mProcessStateTimer[state] == null) {
5458 return 0;
5459 }
5460 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
5461 }
5462
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005463 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08005464 public Timer getProcessStateTimer(int state) {
5465 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
5466 return mProcessStateTimer[state];
5467 }
5468
5469 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005470 public Timer getVibratorOnTimer() {
5471 return mVibratorOnTimer;
5472 }
5473
5474 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005475 public void noteUserActivityLocked(int type) {
5476 if (mUserActivityCounters == null) {
5477 initUserActivityLocked();
5478 }
Jeff Browndf693de2012-07-27 12:03:38 -07005479 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5480 mUserActivityCounters[type].stepAtomic();
5481 } else {
5482 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5483 new Throwable());
5484 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005485 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005486
Dianne Hackborn617f8772009-03-31 15:04:46 -07005487 @Override
5488 public boolean hasUserActivity() {
5489 return mUserActivityCounters != null;
5490 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005491
Dianne Hackborn617f8772009-03-31 15:04:46 -07005492 @Override
5493 public int getUserActivityCount(int type, int which) {
5494 if (mUserActivityCounters == null) {
5495 return 0;
5496 }
Evan Millarc64edde2009-04-18 12:26:32 -07005497 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005498 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005499
Robert Greenwalta029ea12013-09-25 16:38:12 -07005500 void makeWifiBatchedScanBin(int i, Parcel in) {
5501 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5502
Joe Onoratoabded112016-02-08 16:49:39 -08005503 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005504 if (collected == null) {
5505 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005506 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005507 }
5508 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005509 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5510 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005511 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005512 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5513 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005514 }
5515 }
5516
5517
Dianne Hackborn617f8772009-03-31 15:04:46 -07005518 void initUserActivityLocked() {
5519 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5520 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005521 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005522 }
5523 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005524
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005525 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5526 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005527 initNetworkActivityLocked();
5528 }
5529 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005530 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5531 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005532 } else {
5533 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5534 new Throwable());
5535 }
5536 }
5537
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005538 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5539 if (mNetworkByteActivityCounters == null) {
5540 initNetworkActivityLocked();
5541 }
5542 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5543 mMobileRadioActiveCount.addCountLocked(1);
5544 }
5545
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005546 @Override
5547 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005548 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005549 }
5550
5551 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005552 public long getNetworkActivityBytes(int type, int which) {
5553 if (mNetworkByteActivityCounters != null && type >= 0
5554 && type < mNetworkByteActivityCounters.length) {
5555 return mNetworkByteActivityCounters[type].getCountLocked(which);
5556 } else {
5557 return 0;
5558 }
5559 }
5560
5561 @Override
5562 public long getNetworkActivityPackets(int type, int which) {
5563 if (mNetworkPacketActivityCounters != null && type >= 0
5564 && type < mNetworkPacketActivityCounters.length) {
5565 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005566 } else {
5567 return 0;
5568 }
5569 }
5570
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005571 @Override
5572 public long getMobileRadioActiveTime(int which) {
5573 return mMobileRadioActiveTime != null
5574 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5575 }
5576
5577 @Override
5578 public int getMobileRadioActiveCount(int which) {
5579 return mMobileRadioActiveCount != null
5580 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5581 }
5582
Adam Lesinskie08af192015-03-25 16:42:59 -07005583 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005584 public long getUserCpuTimeUs(int which) {
5585 return mUserCpuTime.getCountLocked(which);
5586 }
5587
5588 @Override
5589 public long getSystemCpuTimeUs(int which) {
5590 return mSystemCpuTime.getCountLocked(which);
5591 }
5592
5593 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005594 public long getCpuPowerMaUs(int which) {
5595 return mCpuPower.getCountLocked(which);
5596 }
5597
5598 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005599 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5600 if (mCpuClusterSpeed != null) {
5601 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5602 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5603 if (cpuSpeeds != null) {
5604 if (step >= 0 && step < cpuSpeeds.length) {
5605 final LongSamplingCounter c = cpuSpeeds[step];
5606 if (c != null) {
5607 return c.getCountLocked(which);
5608 }
5609 }
5610 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005611 }
5612 }
5613 return 0;
5614 }
5615
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005616 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005617 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5618 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005619 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005620 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5621 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005622 }
Joe Onoratoabded112016-02-08 16:49:39 -08005623 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5624 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005626
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005627 /**
5628 * Clear all stats for this uid. Returns true if the uid is completely
5629 * inactive so can be dropped.
5630 */
5631 boolean reset() {
5632 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005633
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005634 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005635 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005636 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005637 }
5638 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005639 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005640 active |= mFullWifiLockOut;
5641 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005642 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005643 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005644 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005645 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005646 if (mWifiBatchedScanTimer != null) {
5647 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5648 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005649 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005650 }
5651 }
5652 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5653 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005654 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005655 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005656 active |= mWifiMulticastEnabled;
5657 }
5658 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005659 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005660 }
5661 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005662 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005663 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005664 if (mFlashlightTurnedOnTimer != null) {
5665 active |= !mFlashlightTurnedOnTimer.reset(false);
5666 }
5667 if (mCameraTurnedOnTimer != null) {
5668 active |= !mCameraTurnedOnTimer.reset(false);
5669 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005670 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005671 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005672 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005673 if (mBluetoothScanTimer != null) {
5674 active |= !mBluetoothScanTimer.reset(false);
5675 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005676 if (mProcessStateTimer != null) {
5677 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5678 if (mProcessStateTimer[i] != null) {
5679 active |= !mProcessStateTimer[i].reset(false);
5680 }
5681 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005682 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005683 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005684 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005685 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005686 mVibratorOnTimer.detach();
5687 mVibratorOnTimer = null;
5688 } else {
5689 active = true;
5690 }
5691 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005692
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005693 if (mUserActivityCounters != null) {
5694 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5695 mUserActivityCounters[i].reset(false);
5696 }
5697 }
5698
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005699 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005700 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005701 mNetworkByteActivityCounters[i].reset(false);
5702 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005703 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005704 mMobileRadioActiveTime.reset(false);
5705 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005706 }
5707
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005708 if (mWifiControllerActivity != null) {
5709 mWifiControllerActivity.reset(false);
5710 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005711
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005712 if (mBluetoothControllerActivity != null) {
5713 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005714 }
5715
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005716 if (mModemControllerActivity != null) {
5717 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07005718 }
5719
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005720 mUserCpuTime.reset(false);
5721 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005722 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07005723
5724 if (mCpuClusterSpeed != null) {
5725 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
5726 if (speeds != null) {
5727 for (LongSamplingCounter speed : speeds) {
5728 if (speed != null) {
5729 speed.reset(false);
5730 }
5731 }
5732 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005733 }
5734 }
5735
Dianne Hackbornd953c532014-08-16 18:17:38 -07005736 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5737 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5738 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005739 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005740 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005741 } else {
5742 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005743 }
5744 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005745 mWakelockStats.cleanup();
5746 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5747 for (int is=syncStats.size()-1; is>=0; is--) {
5748 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005749 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005750 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005751 timer.detach();
5752 } else {
5753 active = true;
5754 }
5755 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005756 mSyncStats.cleanup();
5757 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5758 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5759 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005760 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005761 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005762 timer.detach();
5763 } else {
5764 active = true;
5765 }
5766 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005767 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005768 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5769 Sensor s = mSensorStats.valueAt(ise);
5770 if (s.reset()) {
5771 mSensorStats.removeAt(ise);
5772 } else {
5773 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005774 }
5775 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005776 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5777 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08005778 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005779 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005780 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005781 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005782 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005783 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005784 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005785 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005786 } else {
5787 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005788 }
5789 }
5790 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005791 if (mPackageStats.size() > 0) {
5792 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5793 while (it.hasNext()) {
5794 Map.Entry<String, Pkg> pkgEntry = it.next();
5795 Pkg p = pkgEntry.getValue();
5796 p.detach();
5797 if (p.mServiceStats.size() > 0) {
5798 Iterator<Map.Entry<String, Pkg.Serv>> it2
5799 = p.mServiceStats.entrySet().iterator();
5800 while (it2.hasNext()) {
5801 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5802 servEntry.getValue().detach();
5803 }
5804 }
5805 }
5806 mPackageStats.clear();
5807 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005808
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005809 mLastStepUserTime = mLastStepSystemTime = 0;
5810 mCurStepUserTime = mCurStepSystemTime = 0;
5811
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005812 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005813 if (mWifiRunningTimer != null) {
5814 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005815 }
5816 if (mFullWifiLockTimer != null) {
5817 mFullWifiLockTimer.detach();
5818 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005819 if (mWifiScanTimer != null) {
5820 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005821 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005822 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5823 if (mWifiBatchedScanTimer[i] != null) {
5824 mWifiBatchedScanTimer[i].detach();
5825 }
5826 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005827 if (mWifiMulticastTimer != null) {
5828 mWifiMulticastTimer.detach();
5829 }
5830 if (mAudioTurnedOnTimer != null) {
5831 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005832 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005833 }
5834 if (mVideoTurnedOnTimer != null) {
5835 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005836 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005837 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005838 if (mFlashlightTurnedOnTimer != null) {
5839 mFlashlightTurnedOnTimer.detach();
5840 mFlashlightTurnedOnTimer = null;
5841 }
5842 if (mCameraTurnedOnTimer != null) {
5843 mCameraTurnedOnTimer.detach();
5844 mCameraTurnedOnTimer = null;
5845 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005846 if (mForegroundActivityTimer != null) {
5847 mForegroundActivityTimer.detach();
5848 mForegroundActivityTimer = null;
5849 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005850 if (mBluetoothScanTimer != null) {
5851 mBluetoothScanTimer.detach();
5852 mBluetoothScanTimer = null;
5853 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005854 if (mUserActivityCounters != null) {
5855 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5856 mUserActivityCounters[i].detach();
5857 }
5858 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005859 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005860 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005861 mNetworkByteActivityCounters[i].detach();
5862 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005863 }
5864 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005865
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005866 if (mWifiControllerActivity != null) {
5867 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07005868 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005869
5870 if (mBluetoothControllerActivity != null) {
5871 mBluetoothControllerActivity.detach();
5872 }
5873
5874 if (mModemControllerActivity != null) {
5875 mModemControllerActivity.detach();
5876 }
5877
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005878 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005879
5880 mUserCpuTime.detach();
5881 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005882 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07005883
5884 if (mCpuClusterSpeed != null) {
5885 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5886 if (cpuSpeeds != null) {
5887 for (LongSamplingCounter c : cpuSpeeds) {
5888 if (c != null) {
5889 c.detach();
5890 }
5891 }
5892 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005893 }
5894 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005895 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005896
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005897 return !active;
5898 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005899
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005900 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005901 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5902 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005903 out.writeInt(NW);
5904 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005905 out.writeString(wakeStats.keyAt(iw));
5906 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005907 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 }
5909
Dianne Hackbornd953c532014-08-16 18:17:38 -07005910 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5911 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005912 out.writeInt(NS);
5913 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005914 out.writeString(syncStats.keyAt(is));
5915 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005916 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5917 }
5918
Dianne Hackbornd953c532014-08-16 18:17:38 -07005919 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5920 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005921 out.writeInt(NJ);
5922 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005923 out.writeString(jobStats.keyAt(ij));
5924 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005925 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5926 }
5927
Dianne Hackborn61659e52014-07-09 16:13:01 -07005928 int NSE = mSensorStats.size();
5929 out.writeInt(NSE);
5930 for (int ise=0; ise<NSE; ise++) {
5931 out.writeInt(mSensorStats.keyAt(ise));
5932 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005933 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 }
5935
Dianne Hackborn61659e52014-07-09 16:13:01 -07005936 int NP = mProcessStats.size();
5937 out.writeInt(NP);
5938 for (int ip=0; ip<NP; ip++) {
5939 out.writeString(mProcessStats.keyAt(ip));
5940 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 proc.writeToParcelLocked(out);
5942 }
5943
5944 out.writeInt(mPackageStats.size());
5945 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
5946 out.writeString(pkgEntry.getKey());
5947 Uid.Pkg pkg = pkgEntry.getValue();
5948 pkg.writeToParcelLocked(out);
5949 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005950
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005951 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005952 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005953 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005954 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005955 out.writeInt(0);
5956 }
5957 if (mFullWifiLockTimer != null) {
5958 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005959 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005960 } else {
5961 out.writeInt(0);
5962 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005963 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005964 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005965 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005966 } else {
5967 out.writeInt(0);
5968 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005969 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5970 if (mWifiBatchedScanTimer[i] != null) {
5971 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005972 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005973 } else {
5974 out.writeInt(0);
5975 }
5976 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005977 if (mWifiMulticastTimer != null) {
5978 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005979 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005980 } else {
5981 out.writeInt(0);
5982 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005983
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005984 if (mAudioTurnedOnTimer != null) {
5985 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005986 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005987 } else {
5988 out.writeInt(0);
5989 }
5990 if (mVideoTurnedOnTimer != null) {
5991 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005992 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005993 } else {
5994 out.writeInt(0);
5995 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005996 if (mFlashlightTurnedOnTimer != null) {
5997 out.writeInt(1);
5998 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5999 } else {
6000 out.writeInt(0);
6001 }
6002 if (mCameraTurnedOnTimer != null) {
6003 out.writeInt(1);
6004 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6005 } else {
6006 out.writeInt(0);
6007 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006008 if (mForegroundActivityTimer != null) {
6009 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006010 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006011 } else {
6012 out.writeInt(0);
6013 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006014 if (mBluetoothScanTimer != null) {
6015 out.writeInt(1);
6016 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6017 } else {
6018 out.writeInt(0);
6019 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006020 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6021 if (mProcessStateTimer[i] != null) {
6022 out.writeInt(1);
6023 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6024 } else {
6025 out.writeInt(0);
6026 }
6027 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006028 if (mVibratorOnTimer != null) {
6029 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006030 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006031 } else {
6032 out.writeInt(0);
6033 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006034 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006035 out.writeInt(1);
6036 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6037 mUserActivityCounters[i].writeToParcel(out);
6038 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006039 } else {
6040 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006041 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006042 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006043 out.writeInt(1);
6044 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006045 mNetworkByteActivityCounters[i].writeToParcel(out);
6046 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006047 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006048 mMobileRadioActiveTime.writeToParcel(out);
6049 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006050 } else {
6051 out.writeInt(0);
6052 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006053
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006054 if (mWifiControllerActivity != null) {
6055 out.writeInt(1);
6056 mWifiControllerActivity.writeToParcel(out, 0);
6057 } else {
6058 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006059 }
6060
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006061 if (mBluetoothControllerActivity != null) {
6062 out.writeInt(1);
6063 mBluetoothControllerActivity.writeToParcel(out, 0);
6064 } else {
6065 out.writeInt(0);
6066 }
6067
6068 if (mModemControllerActivity != null) {
6069 out.writeInt(1);
6070 mModemControllerActivity.writeToParcel(out, 0);
6071 } else {
6072 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006073 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006074
6075 mUserCpuTime.writeToParcel(out);
6076 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006077 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006078
Adam Lesinski6832f392015-09-05 18:05:40 -07006079 if (mCpuClusterSpeed != null) {
6080 out.writeInt(1);
6081 out.writeInt(mCpuClusterSpeed.length);
6082 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6083 if (cpuSpeeds != null) {
6084 out.writeInt(1);
6085 out.writeInt(cpuSpeeds.length);
6086 for (LongSamplingCounter c : cpuSpeeds) {
6087 if (c != null) {
6088 out.writeInt(1);
6089 c.writeToParcel(out);
6090 } else {
6091 out.writeInt(0);
6092 }
6093 }
6094 } else {
6095 out.writeInt(0);
6096 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006097 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006098 } else {
6099 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 }
6102
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006103 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 int numWakelocks = in.readInt();
6105 mWakelockStats.clear();
6106 for (int j = 0; j < numWakelocks; j++) {
6107 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006108 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006109 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006110 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 }
6112
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006113 int numSyncs = in.readInt();
6114 mSyncStats.clear();
6115 for (int j = 0; j < numSyncs; j++) {
6116 String syncName = in.readString();
6117 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006118 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006119 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006120 }
6121 }
6122
6123 int numJobs = in.readInt();
6124 mJobStats.clear();
6125 for (int j = 0; j < numJobs; j++) {
6126 String jobName = in.readString();
6127 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006128 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
6129 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006130 }
6131 }
6132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 int numSensors = in.readInt();
6134 mSensorStats.clear();
6135 for (int k = 0; k < numSensors; k++) {
6136 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006137 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
6138 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 mSensorStats.put(sensorNumber, sensor);
6140 }
6141
6142 int numProcs = in.readInt();
6143 mProcessStats.clear();
6144 for (int k = 0; k < numProcs; k++) {
6145 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006146 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006147 proc.readFromParcelLocked(in);
6148 mProcessStats.put(processName, proc);
6149 }
6150
6151 int numPkgs = in.readInt();
6152 mPackageStats.clear();
6153 for (int l = 0; l < numPkgs; l++) {
6154 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006155 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 pkg.readFromParcelLocked(in);
6157 mPackageStats.put(packageName, pkg);
6158 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006159
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006160 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006161 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006162 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6163 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006164 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006165 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006166 }
6167 mFullWifiLockOut = false;
6168 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006169 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6170 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006171 } else {
6172 mFullWifiLockTimer = null;
6173 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006174 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006175 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006176 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6177 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006178 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006179 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006180 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006181 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6182 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6183 if (in.readInt() != 0) {
6184 makeWifiBatchedScanBin(i, in);
6185 } else {
6186 mWifiBatchedScanTimer[i] = null;
6187 }
6188 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006189 mWifiMulticastEnabled = false;
6190 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006191 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6192 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006193 } else {
6194 mWifiMulticastTimer = null;
6195 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006196 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006197 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6198 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006199 } else {
6200 mAudioTurnedOnTimer = null;
6201 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006202 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006203 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6204 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006205 } else {
6206 mVideoTurnedOnTimer = null;
6207 }
6208 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006209 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6210 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006211 } else {
6212 mFlashlightTurnedOnTimer = null;
6213 }
6214 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006215 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6216 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006217 } else {
6218 mCameraTurnedOnTimer = null;
6219 }
6220 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006221 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6222 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006223 } else {
6224 mForegroundActivityTimer = null;
6225 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006226 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006227 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6228 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006229 } else {
6230 mBluetoothScanTimer = null;
6231 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006232 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006233 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6234 if (in.readInt() != 0) {
6235 makeProcessState(i, in);
6236 } else {
6237 mProcessStateTimer[i] = null;
6238 }
6239 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006240 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006241 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6242 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006243 } else {
6244 mVibratorOnTimer = null;
6245 }
6246 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006247 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6248 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006249 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006250 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006251 } else {
6252 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006253 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006254 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006255 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6256 mNetworkPacketActivityCounters
6257 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006258 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006259 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006260 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006261 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006262 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006263 }
Joe Onoratoabded112016-02-08 16:49:39 -08006264 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6265 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006266 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006267 mNetworkByteActivityCounters = null;
6268 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006269 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006270
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006271 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006272 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006273 NUM_WIFI_TX_LEVELS, in);
6274 } else {
6275 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006276 }
6277
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006278 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006279 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006280 NUM_BT_TX_LEVELS, in);
6281 } else {
6282 mBluetoothControllerActivity = null;
6283 }
6284
6285 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006286 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006287 ModemActivityInfo.TX_POWER_LEVELS, in);
6288 } else {
6289 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006290 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006291
Joe Onoratoabded112016-02-08 16:49:39 -08006292 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6293 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6294 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006295
Adam Lesinski6832f392015-09-05 18:05:40 -07006296 if (in.readInt() != 0) {
6297 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006298 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006299 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006300 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006301
6302 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6303 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6304 if (in.readInt() != 0) {
6305 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006306 if (mBsi.mPowerProfile != null &&
6307 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006308 throw new ParcelFormatException("Incompatible number of cpu speeds");
6309 }
6310
6311 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6312 mCpuClusterSpeed[cluster] = cpuSpeeds;
6313 for (int speed = 0; speed < numSpeeds; speed++) {
6314 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006315 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006316 }
6317 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006318 } else {
6319 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006320 }
6321 }
6322 } else {
6323 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 }
6326
6327 /**
6328 * The statistics associated with a particular wake lock.
6329 */
Joe Onoratoabded112016-02-08 16:49:39 -08006330 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6331 /**
6332 * BatteryStatsImpl that we are associated with.
6333 */
6334 protected BatteryStatsImpl mBsi;
6335
6336 /**
6337 * BatteryStatsImpl that we are associated with.
6338 */
6339 protected Uid mUid;
6340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 /**
6342 * How long (in ms) this uid has been keeping the device partially awake.
6343 */
Evan Millarc64edde2009-04-18 12:26:32 -07006344 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345
6346 /**
6347 * How long (in ms) this uid has been keeping the device fully awake.
6348 */
Evan Millarc64edde2009-04-18 12:26:32 -07006349 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350
6351 /**
6352 * How long (in ms) this uid has had a window keeping the device awake.
6353 */
Evan Millarc64edde2009-04-18 12:26:32 -07006354 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006355
6356 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006357 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006358 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006359 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006360
Joe Onoratoabded112016-02-08 16:49:39 -08006361 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6362 mBsi = bsi;
6363 mUid = uid;
6364 }
6365
Adam Lesinski9425fe22015-06-19 12:02:13 -07006366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6368 * proper timer pool from the given BatteryStatsImpl object.
6369 *
6370 * @param in the Parcel to be read from.
6371 * return a new Timer, or null.
6372 */
Evan Millarc64edde2009-04-18 12:26:32 -07006373 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006374 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 if (in.readInt() == 0) {
6376 return null;
6377 }
6378
Joe Onoratoabded112016-02-08 16:49:39 -08006379 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 }
6381
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006382 boolean reset() {
6383 boolean wlactive = false;
6384 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006385 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006386 }
6387 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006388 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006389 }
6390 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006391 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006392 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006393 if (mTimerDraw != null) {
6394 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006395 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006396 if (!wlactive) {
6397 if (mTimerFull != null) {
6398 mTimerFull.detach();
6399 mTimerFull = null;
6400 }
6401 if (mTimerPartial != null) {
6402 mTimerPartial.detach();
6403 mTimerPartial = null;
6404 }
6405 if (mTimerWindow != null) {
6406 mTimerWindow.detach();
6407 mTimerWindow = null;
6408 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006409 if (mTimerDraw != null) {
6410 mTimerDraw.detach();
6411 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006412 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006413 }
6414 return !wlactive;
6415 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006416
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006417 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006418 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006419 mBsi.mPartialTimers, screenOffTimeBase, in);
6420 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
6421 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
6422 mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 }
6424
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006425 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6426 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6427 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6428 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006429 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 }
6431
6432 @Override
6433 public Timer getWakeTime(int type) {
6434 switch (type) {
6435 case WAKE_TYPE_FULL: return mTimerFull;
6436 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6437 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006438 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 default: throw new IllegalArgumentException("type = " + type);
6440 }
6441 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006442
6443 public StopwatchTimer getStopwatchTimer(int type) {
6444 StopwatchTimer t;
6445 switch (type) {
6446 case WAKE_TYPE_PARTIAL:
6447 t = mTimerPartial;
6448 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006449 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
6450 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006451 mTimerPartial = t;
6452 }
6453 return t;
6454 case WAKE_TYPE_FULL:
6455 t = mTimerFull;
6456 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006457 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6458 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006459 mTimerFull = t;
6460 }
6461 return t;
6462 case WAKE_TYPE_WINDOW:
6463 t = mTimerWindow;
6464 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006465 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6466 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006467 mTimerWindow = t;
6468 }
6469 return t;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006470 case WAKE_TYPE_DRAW:
6471 t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006472 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006473 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6474 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006475 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006476 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006477 return t;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006478 default:
6479 throw new IllegalArgumentException("type=" + type);
6480 }
6481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 }
6483
Joe Onoratoabded112016-02-08 16:49:39 -08006484 public static class Sensor extends BatteryStats.Uid.Sensor {
6485 /**
6486 * BatteryStatsImpl that we are associated with.
6487 */
6488 protected BatteryStatsImpl mBsi;
6489
6490 /**
6491 * BatteryStatsImpl that we are associated with.
6492 */
6493 protected Uid mUid;
6494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006496 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006497
Joe Onoratoabded112016-02-08 16:49:39 -08006498 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6499 mBsi = bsi;
6500 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 mHandle = handle;
6502 }
6503
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006504 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 if (in.readInt() == 0) {
6506 return null;
6507 }
6508
Joe Onoratoabded112016-02-08 16:49:39 -08006509 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006511 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006512 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 }
Joe Onoratoabded112016-02-08 16:49:39 -08006514 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 }
6516
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006517 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006518 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006519 mTimer = null;
6520 return true;
6521 }
6522 return false;
6523 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006524
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006525 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6526 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 }
6528
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006529 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6530 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 }
6532
6533 @Override
6534 public Timer getSensorTime() {
6535 return mTimer;
6536 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006537
6538 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 public int getHandle() {
6540 return mHandle;
6541 }
6542 }
6543
6544 /**
6545 * The statistics associated with a particular process.
6546 */
Joe Onoratoabded112016-02-08 16:49:39 -08006547 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
6548 /**
6549 * BatteryStatsImpl that we are associated with.
6550 */
6551 protected BatteryStatsImpl mBsi;
6552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006554 * The name of this process.
6555 */
6556 final String mName;
6557
6558 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08006559 * Remains true until removed from the stats.
6560 */
6561 boolean mActive = true;
6562
6563 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006564 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 */
6566 long mUserTime;
6567
6568 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006569 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006570 */
6571 long mSystemTime;
6572
6573 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006574 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006575 */
6576 long mForegroundTime;
6577
6578 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006579 * Number of times the process has been started.
6580 */
6581 int mStarts;
6582
6583 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006584 * Number of times the process has crashed.
6585 */
6586 int mNumCrashes;
6587
6588 /**
6589 * Number of times the process has had an ANR.
6590 */
6591 int mNumAnrs;
6592
6593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 * The amount of user time loaded from a previous save.
6595 */
6596 long mLoadedUserTime;
6597
6598 /**
6599 * The amount of system time loaded from a previous save.
6600 */
6601 long mLoadedSystemTime;
6602
6603 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006604 * The amount of foreground time loaded from a previous save.
6605 */
6606 long mLoadedForegroundTime;
6607
6608 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006609 * The number of times the process has started from a previous save.
6610 */
6611 int mLoadedStarts;
6612
6613 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006614 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006616 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617
6618 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006619 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006621 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006622
6623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 * The amount of user time when last unplugged.
6625 */
6626 long mUnpluggedUserTime;
6627
6628 /**
6629 * The amount of system time when last unplugged.
6630 */
6631 long mUnpluggedSystemTime;
6632
6633 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006634 * The amount of foreground time since unplugged.
6635 */
6636 long mUnpluggedForegroundTime;
6637
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006638 /**
6639 * The number of times the process has started before unplugged.
6640 */
6641 int mUnpluggedStarts;
6642
Dianne Hackborn61659e52014-07-09 16:13:01 -07006643 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006644 * Number of times the process has crashed before unplugged.
6645 */
6646 int mUnpluggedNumCrashes;
6647
6648 /**
6649 * Number of times the process has had an ANR before unplugged.
6650 */
6651 int mUnpluggedNumAnrs;
6652
Dianne Hackborn287952c2010-09-22 22:34:31 -07006653 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006654
Joe Onoratoabded112016-02-08 16:49:39 -08006655 public Proc(BatteryStatsImpl bsi, String name) {
6656 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006657 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08006658 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006660
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006661 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 mUnpluggedUserTime = mUserTime;
6663 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006664 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006665 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006666 mUnpluggedNumCrashes = mNumCrashes;
6667 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006668 }
6669
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006670 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006672
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006673 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006674 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08006675 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006676 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006677
Dianne Hackborn287952c2010-09-22 22:34:31 -07006678 public int countExcessivePowers() {
6679 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006680 }
6681
Dianne Hackborn287952c2010-09-22 22:34:31 -07006682 public ExcessivePower getExcessivePower(int i) {
6683 if (mExcessivePower != null) {
6684 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006685 }
6686 return null;
6687 }
6688
6689 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006690 if (mExcessivePower == null) {
6691 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006692 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006693 ExcessivePower ew = new ExcessivePower();
6694 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006695 ew.overTime = overTime;
6696 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006697 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006698 }
6699
Dianne Hackborn287952c2010-09-22 22:34:31 -07006700 public void addExcessiveCpu(long overTime, long usedTime) {
6701 if (mExcessivePower == null) {
6702 mExcessivePower = new ArrayList<ExcessivePower>();
6703 }
6704 ExcessivePower ew = new ExcessivePower();
6705 ew.type = ExcessivePower.TYPE_CPU;
6706 ew.overTime = overTime;
6707 ew.usedTime = usedTime;
6708 mExcessivePower.add(ew);
6709 }
6710
6711 void writeExcessivePowerToParcelLocked(Parcel out) {
6712 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006713 out.writeInt(0);
6714 return;
6715 }
6716
Dianne Hackborn287952c2010-09-22 22:34:31 -07006717 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006718 out.writeInt(N);
6719 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006720 ExcessivePower ew = mExcessivePower.get(i);
6721 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006722 out.writeLong(ew.overTime);
6723 out.writeLong(ew.usedTime);
6724 }
6725 }
6726
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006727 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006728 final int N = in.readInt();
6729 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006730 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006731 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006732 }
6733
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006734 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006735 throw new ParcelFormatException(
6736 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006737 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006738
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006739 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006740 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006741 ExcessivePower ew = new ExcessivePower();
6742 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006743 ew.overTime = in.readLong();
6744 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006745 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006746 }
6747 }
6748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749 void writeToParcelLocked(Parcel out) {
6750 out.writeLong(mUserTime);
6751 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006752 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006754 out.writeInt(mNumCrashes);
6755 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 out.writeLong(mLoadedUserTime);
6757 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006758 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006760 out.writeInt(mLoadedNumCrashes);
6761 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 out.writeLong(mUnpluggedUserTime);
6763 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006764 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006766 out.writeInt(mUnpluggedNumCrashes);
6767 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006768 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 }
6770
6771 void readFromParcelLocked(Parcel in) {
6772 mUserTime = in.readLong();
6773 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006774 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006776 mNumCrashes = in.readInt();
6777 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 mLoadedUserTime = in.readLong();
6779 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006780 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006782 mLoadedNumCrashes = in.readInt();
6783 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006784 mUnpluggedUserTime = in.readLong();
6785 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006786 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006787 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006788 mUnpluggedNumCrashes = in.readInt();
6789 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006790 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 }
6792
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006793 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 mUserTime += utime;
6795 mSystemTime += stime;
6796 }
6797
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006798 public void addForegroundTimeLocked(long ttime) {
6799 mForegroundTime += ttime;
6800 }
6801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 public void incStartsLocked() {
6803 mStarts++;
6804 }
6805
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006806 public void incNumCrashesLocked() {
6807 mNumCrashes++;
6808 }
6809
6810 public void incNumAnrsLocked() {
6811 mNumAnrs++;
6812 }
6813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006815 public boolean isActive() {
6816 return mActive;
6817 }
6818
6819 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006821 long val = mUserTime;
6822 if (which == STATS_CURRENT) {
6823 val -= mLoadedUserTime;
6824 } else if (which == STATS_SINCE_UNPLUGGED) {
6825 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 }
6827 return val;
6828 }
6829
6830 @Override
6831 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006832 long val = mSystemTime;
6833 if (which == STATS_CURRENT) {
6834 val -= mLoadedSystemTime;
6835 } else if (which == STATS_SINCE_UNPLUGGED) {
6836 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 }
6838 return val;
6839 }
6840
6841 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006842 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006843 long val = mForegroundTime;
6844 if (which == STATS_CURRENT) {
6845 val -= mLoadedForegroundTime;
6846 } else if (which == STATS_SINCE_UNPLUGGED) {
6847 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006848 }
6849 return val;
6850 }
6851
6852 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006854 int val = mStarts;
6855 if (which == STATS_CURRENT) {
6856 val -= mLoadedStarts;
6857 } else if (which == STATS_SINCE_UNPLUGGED) {
6858 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 }
6860 return val;
6861 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006862
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006863 @Override
6864 public int getNumCrashes(int which) {
6865 int val = mNumCrashes;
6866 if (which == STATS_CURRENT) {
6867 val -= mLoadedNumCrashes;
6868 } else if (which == STATS_SINCE_UNPLUGGED) {
6869 val -= mUnpluggedNumCrashes;
6870 }
6871 return val;
6872 }
6873
6874 @Override
6875 public int getNumAnrs(int which) {
6876 int val = mNumAnrs;
6877 if (which == STATS_CURRENT) {
6878 val -= mLoadedNumAnrs;
6879 } else if (which == STATS_SINCE_UNPLUGGED) {
6880 val -= mUnpluggedNumAnrs;
6881 }
6882 return val;
6883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006884 }
6885
6886 /**
6887 * The statistics associated with a particular package.
6888 */
Joe Onoratoabded112016-02-08 16:49:39 -08006889 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
6890 /**
6891 * BatteryStatsImpl that we are associated with.
6892 */
6893 protected BatteryStatsImpl mBsi;
6894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006896 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006897 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006898 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899
6900 /**
6901 * The statics we have collected for this package's services.
6902 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006903 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904
Joe Onoratoabded112016-02-08 16:49:39 -08006905 public Pkg(BatteryStatsImpl bsi) {
6906 mBsi = bsi;
6907 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006908 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006909
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006910 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 }
6912
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006913 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006915
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006916 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08006917 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006918 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006921 int numWA = in.readInt();
6922 mWakeupAlarms.clear();
6923 for (int i=0; i<numWA; i++) {
6924 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006925 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927
6928 int numServs = in.readInt();
6929 mServiceStats.clear();
6930 for (int m = 0; m < numServs; m++) {
6931 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006932 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 mServiceStats.put(serviceName, serv);
6934
6935 serv.readFromParcelLocked(in);
6936 }
6937 }
6938
6939 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006940 int numWA = mWakeupAlarms.size();
6941 out.writeInt(numWA);
6942 for (int i=0; i<numWA; i++) {
6943 out.writeString(mWakeupAlarms.keyAt(i));
6944 mWakeupAlarms.valueAt(i).writeToParcel(out);
6945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006947 final int NS = mServiceStats.size();
6948 out.writeInt(NS);
6949 for (int i=0; i<NS; i++) {
6950 out.writeString(mServiceStats.keyAt(i));
6951 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 serv.writeToParcelLocked(out);
6953 }
6954 }
6955
6956 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006957 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
6958 return mWakeupAlarms;
6959 }
6960
6961 public void noteWakeupAlarmLocked(String tag) {
6962 Counter c = mWakeupAlarms.get(tag);
6963 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006964 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006965 mWakeupAlarms.put(tag, c);
6966 }
6967 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 }
6969
6970 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006971 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
6972 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 }
6974
6975 /**
6976 * The statistics associated with a particular service.
6977 */
Joe Onoratoabded112016-02-08 16:49:39 -08006978 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
6979 /**
6980 * BatteryStatsImpl that we are associated with.
6981 */
6982 protected BatteryStatsImpl mBsi;
6983
6984 /**
6985 * The android package in which this service resides.
6986 */
6987 protected Pkg mPkg;
6988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 /**
6990 * Total time (ms in battery uptime) the service has been left started.
6991 */
Joe Onoratoabded112016-02-08 16:49:39 -08006992 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993
6994 /**
6995 * If service has been started and not yet stopped, this is
6996 * when it was started.
6997 */
Joe Onoratoabded112016-02-08 16:49:39 -08006998 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999
7000 /**
7001 * True if we are currently running.
7002 */
Joe Onoratoabded112016-02-08 16:49:39 -08007003 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004
7005 /**
7006 * Total number of times startService() has been called.
7007 */
Joe Onoratoabded112016-02-08 16:49:39 -08007008 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009
7010 /**
7011 * Total time (ms in battery uptime) the service has been left launched.
7012 */
Joe Onoratoabded112016-02-08 16:49:39 -08007013 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014
7015 /**
7016 * If service has been launched and not yet exited, this is
7017 * when it was launched (ms in battery uptime).
7018 */
Joe Onoratoabded112016-02-08 16:49:39 -08007019 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007020
7021 /**
7022 * True if we are currently launched.
7023 */
Joe Onoratoabded112016-02-08 16:49:39 -08007024 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025
7026 /**
7027 * Total number times the service has been launched.
7028 */
Joe Onoratoabded112016-02-08 16:49:39 -08007029 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007030
7031 /**
7032 * The amount of time spent started loaded from a previous save
7033 * (ms in battery uptime).
7034 */
Joe Onoratoabded112016-02-08 16:49:39 -08007035 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036
7037 /**
7038 * The number of starts loaded from a previous save.
7039 */
Joe Onoratoabded112016-02-08 16:49:39 -08007040 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041
7042 /**
7043 * The number of launches loaded from a previous save.
7044 */
Joe Onoratoabded112016-02-08 16:49:39 -08007045 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046
7047 /**
7048 * The amount of time spent started as of the last run (ms
7049 * in battery uptime).
7050 */
Joe Onoratoabded112016-02-08 16:49:39 -08007051 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052
7053 /**
7054 * The number of starts as of the last run.
7055 */
Joe Onoratoabded112016-02-08 16:49:39 -08007056 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057
7058 /**
7059 * The number of launches as of the last run.
7060 */
Joe Onoratoabded112016-02-08 16:49:39 -08007061 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062
7063 /**
7064 * The amount of time spent started when last unplugged (ms
7065 * in battery uptime).
7066 */
Joe Onoratoabded112016-02-08 16:49:39 -08007067 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068
7069 /**
7070 * The number of starts when last unplugged.
7071 */
Joe Onoratoabded112016-02-08 16:49:39 -08007072 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073
7074 /**
7075 * The number of launches when last unplugged.
7076 */
Joe Onoratoabded112016-02-08 16:49:39 -08007077 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078
Joe Onoratoabded112016-02-08 16:49:39 -08007079 /**
7080 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7081 */
7082 public Serv(BatteryStatsImpl bsi) {
7083 mBsi = bsi;
7084 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007086
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007087 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7088 long baseRealtime) {
7089 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 mUnpluggedStarts = mStarts;
7091 mUnpluggedLaunches = mLaunches;
7092 }
7093
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007094 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7095 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007097
Joe Onoratoabded112016-02-08 16:49:39 -08007098 /**
7099 * Remove this Serv as a listener from the time base.
7100 */
7101 public void detach() {
7102 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007103 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007104
Joe Onoratoabded112016-02-08 16:49:39 -08007105 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 mStartTime = in.readLong();
7107 mRunningSince = in.readLong();
7108 mRunning = in.readInt() != 0;
7109 mStarts = in.readInt();
7110 mLaunchedTime = in.readLong();
7111 mLaunchedSince = in.readLong();
7112 mLaunched = in.readInt() != 0;
7113 mLaunches = in.readInt();
7114 mLoadedStartTime = in.readLong();
7115 mLoadedStarts = in.readInt();
7116 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007117 mLastStartTime = 0;
7118 mLastStarts = 0;
7119 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 mUnpluggedStartTime = in.readLong();
7121 mUnpluggedStarts = in.readInt();
7122 mUnpluggedLaunches = in.readInt();
7123 }
7124
Joe Onoratoabded112016-02-08 16:49:39 -08007125 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 out.writeLong(mStartTime);
7127 out.writeLong(mRunningSince);
7128 out.writeInt(mRunning ? 1 : 0);
7129 out.writeInt(mStarts);
7130 out.writeLong(mLaunchedTime);
7131 out.writeLong(mLaunchedSince);
7132 out.writeInt(mLaunched ? 1 : 0);
7133 out.writeInt(mLaunches);
7134 out.writeLong(mLoadedStartTime);
7135 out.writeInt(mLoadedStarts);
7136 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 out.writeLong(mUnpluggedStartTime);
7138 out.writeInt(mUnpluggedStarts);
7139 out.writeInt(mUnpluggedLaunches);
7140 }
7141
Joe Onoratoabded112016-02-08 16:49:39 -08007142 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 if (!mLaunched) return mLaunchedTime;
7144 return mLaunchedTime + batteryUptime - mLaunchedSince;
7145 }
7146
Joe Onoratoabded112016-02-08 16:49:39 -08007147 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148 if (!mRunning) return mStartTime;
7149 return mStartTime + batteryUptime - mRunningSince;
7150 }
7151
7152 public void startLaunchedLocked() {
7153 if (!mLaunched) {
7154 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007155 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 mLaunched = true;
7157 }
7158 }
7159
7160 public void stopLaunchedLocked() {
7161 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007162 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007163 if (time > 0) {
7164 mLaunchedTime += time;
7165 } else {
7166 mLaunches--;
7167 }
7168 mLaunched = false;
7169 }
7170 }
7171
7172 public void startRunningLocked() {
7173 if (!mRunning) {
7174 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007175 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 mRunning = true;
7177 }
7178 }
7179
7180 public void stopRunningLocked() {
7181 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007182 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 if (time > 0) {
7184 mStartTime += time;
7185 } else {
7186 mStarts--;
7187 }
7188 mRunning = false;
7189 }
7190 }
7191
7192 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007193 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 }
7195
7196 @Override
7197 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007198 int val = mLaunches;
7199 if (which == STATS_CURRENT) {
7200 val -= mLoadedLaunches;
7201 } else if (which == STATS_SINCE_UNPLUGGED) {
7202 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 return val;
7205 }
7206
7207 @Override
7208 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007209 long val = getStartTimeToNowLocked(now);
7210 if (which == STATS_CURRENT) {
7211 val -= mLoadedStartTime;
7212 } else if (which == STATS_SINCE_UNPLUGGED) {
7213 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007215 return val;
7216 }
7217
7218 @Override
7219 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007220 int val = mStarts;
7221 if (which == STATS_CURRENT) {
7222 val -= mLoadedStarts;
7223 } else if (which == STATS_SINCE_UNPLUGGED) {
7224 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 }
7226
7227 return val;
7228 }
7229 }
7230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007232 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007233 }
7234 }
7235
7236 /**
7237 * Retrieve the statistics object for a particular process, creating
7238 * if needed.
7239 */
7240 public Proc getProcessStatsLocked(String name) {
7241 Proc ps = mProcessStats.get(name);
7242 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007243 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 mProcessStats.put(name, ps);
7245 }
7246
7247 return ps;
7248 }
7249
Dianne Hackborna8d10942015-11-19 17:55:19 -08007250 public void updateUidProcessStateLocked(int procState) {
7251 int uidRunningState;
7252 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7253 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7254 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7255 uidRunningState = PROCESS_STATE_TOP;
7256 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7257 // Persistent and other foreground states go here.
7258 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7259 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7260 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7261 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7262 // Persistent and other foreground states go here.
7263 uidRunningState = PROCESS_STATE_FOREGROUND;
7264 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7265 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007266 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007267 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007268 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007269
Dianne Hackborna8d10942015-11-19 17:55:19 -08007270 if (mProcessState == uidRunningState) return;
7271
Joe Onoratoabded112016-02-08 16:49:39 -08007272 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007273
7274 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7275 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7276 }
7277 mProcessState = uidRunningState;
7278 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7279 if (mProcessStateTimer[uidRunningState] == null) {
7280 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007281 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007282 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007283 }
7284 }
7285
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007286 public SparseArray<? extends Pid> getPidStats() {
7287 return mPids;
7288 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007289
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007290 public Pid getPidStatsLocked(int pid) {
7291 Pid p = mPids.get(pid);
7292 if (p == null) {
7293 p = new Pid();
7294 mPids.put(pid, p);
7295 }
7296 return p;
7297 }
7298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007299 /**
7300 * Retrieve the statistics object for a particular service, creating
7301 * if needed.
7302 */
7303 public Pkg getPackageStatsLocked(String name) {
7304 Pkg ps = mPackageStats.get(name);
7305 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007306 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 mPackageStats.put(name, ps);
7308 }
7309
7310 return ps;
7311 }
7312
7313 /**
7314 * Retrieve the statistics object for a particular service, creating
7315 * if needed.
7316 */
7317 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7318 Pkg ps = getPackageStatsLocked(pkg);
7319 Pkg.Serv ss = ps.mServiceStats.get(serv);
7320 if (ss == null) {
7321 ss = ps.newServiceStatsLocked();
7322 ps.mServiceStats.put(serv, ss);
7323 }
7324
7325 return ss;
7326 }
7327
Dianne Hackbornd953c532014-08-16 18:17:38 -07007328 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7329 StopwatchTimer timer = mSyncStats.instantiateObject();
7330 timer.readSummaryFromParcelLocked(in);
7331 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007332 }
7333
Dianne Hackbornd953c532014-08-16 18:17:38 -07007334 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7335 StopwatchTimer timer = mJobStats.instantiateObject();
7336 timer.readSummaryFromParcelLocked(in);
7337 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007338 }
7339
Dianne Hackbornd953c532014-08-16 18:17:38 -07007340 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007341 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007342 mWakelockStats.add(wlName, wl);
7343 if (in.readInt() != 0) {
7344 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007345 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007346 if (in.readInt() != 0) {
7347 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7348 }
7349 if (in.readInt() != 0) {
7350 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007352 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007353 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007355 }
7356
Evan Millarc64edde2009-04-18 12:26:32 -07007357 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 Sensor se = mSensorStats.get(sensor);
7359 if (se == null) {
7360 if (!create) {
7361 return null;
7362 }
Joe Onoratoabded112016-02-08 16:49:39 -08007363 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007364 mSensorStats.put(sensor, se);
7365 }
Evan Millarc64edde2009-04-18 12:26:32 -07007366 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 if (t != null) {
7368 return t;
7369 }
Joe Onoratoabded112016-02-08 16:49:39 -08007370 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007372 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007373 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 }
Joe Onoratoabded112016-02-08 16:49:39 -08007375 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7376 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 se.mTimer = t;
7378 return t;
7379 }
7380
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007381 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007382 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007383 if (t != null) {
7384 t.startRunningLocked(elapsedRealtimeMs);
7385 }
7386 }
7387
7388 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007389 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007390 if (t != null) {
7391 t.stopRunningLocked(elapsedRealtimeMs);
7392 }
7393 }
7394
7395 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007396 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007397 if (t != null) {
7398 t.startRunningLocked(elapsedRealtimeMs);
7399 }
7400 }
7401
7402 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007403 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007404 if (t != null) {
7405 t.stopRunningLocked(elapsedRealtimeMs);
7406 }
7407 }
7408
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007409 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007410 Wakelock wl = mWakelockStats.startObject(name);
7411 if (wl != null) {
7412 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007413 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007414 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007415 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007416 if (p.mWakeNesting++ == 0) {
7417 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007418 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 }
7421
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007422 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007423 Wakelock wl = mWakelockStats.stopObject(name);
7424 if (wl != null) {
7425 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007427 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007428 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007429 if (p != null && p.mWakeNesting > 0) {
7430 if (p.mWakeNesting-- == 1) {
7431 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7432 p.mWakeStartMs = 0;
7433 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007434 }
7435 }
7436 }
7437
7438 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7439 Proc p = getProcessStatsLocked(proc);
7440 if (p != null) {
7441 p.addExcessiveWake(overTime, usedTime);
7442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007444
Dianne Hackborn287952c2010-09-22 22:34:31 -07007445 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7446 Proc p = getProcessStatsLocked(proc);
7447 if (p != null) {
7448 p.addExcessiveCpu(overTime, usedTime);
7449 }
7450 }
7451
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007452 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007453 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007455 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 }
7458
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007459 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007461 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007463 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007466
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007467 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007468 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007470 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007473
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007474 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007475 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007477 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 }
7480
7481 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007482 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007483 }
7484 }
7485
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007486 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007487 this(new SystemClocks(), systemDir, handler, externalSync, null);
7488 }
7489
7490 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
7491 PlatformIdleStateCallback cb) {
7492 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08007493 }
7494
7495 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007496 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08007497 init(clocks);
7498
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007499 if (systemDir != null) {
7500 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7501 new File(systemDir, "batterystats.bin.tmp"));
7502 } else {
7503 mFile = null;
7504 }
7505 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007506 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007507 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007508 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007510 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007511 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007512 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007513 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007514 }
Joe Onoratoabded112016-02-08 16:49:39 -08007515 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
7516 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
7517 mOnBatteryTimeBase);
7518 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
7519 mOnBatteryTimeBase);
7520 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
7521 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
7522 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
7523 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
7524 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
7525 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
7526 mOnBatteryTimeBase);
7527 }
7528 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
7529 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007530 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007531 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007532 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007533 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007534 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007535 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7536 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007537 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007538 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
7539 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7540 NUM_BT_TX_LEVELS);
7541 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7542 ModemActivityInfo.TX_POWER_LEVELS);
7543
Joe Onoratoabded112016-02-08 16:49:39 -08007544 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
7545 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
7546 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007547 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007548 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
7549 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08007550 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
7551 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007552 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007553 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07007554 mOnBatteryTimeBase);
7555 }
Joe Onoratoabded112016-02-08 16:49:39 -08007556 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7557 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
7558 mOnBatteryTimeBase);
7559 }
7560 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7561 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
7562 mOnBatteryTimeBase);
7563 }
7564 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
7565 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
7566 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
7567 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
7568 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007569 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007570 long uptime = mClocks.uptimeMillis() * 1000;
7571 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007572 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007573 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07007574 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007575 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007576 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07007577 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007578 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007579 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007580 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007581 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007582 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 }
7584
7585 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08007586 this(new SystemClocks(), p);
7587 }
7588
7589 public BatteryStatsImpl(Clocks clocks, Parcel p) {
7590 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007591 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007592 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007593 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007594 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007595 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007596 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007597 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007598 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 }
7600
Adam Lesinskie08af192015-03-25 16:42:59 -07007601 public void setPowerProfile(PowerProfile profile) {
7602 synchronized (this) {
7603 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07007604
7605 // We need to initialize the KernelCpuSpeedReaders to read from
7606 // the first cpu of each core. Once we have the PowerProfile, we have access to this
7607 // information.
7608 final int numClusters = mPowerProfile.getNumCpuClusters();
7609 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
7610 int firstCpuOfCluster = 0;
7611 for (int i = 0; i < numClusters; i++) {
7612 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
7613 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
7614 numSpeedSteps);
7615 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
7616 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007617 }
7618 }
7619
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007620 public void setCallback(BatteryCallback cb) {
7621 mCallback = cb;
7622 }
7623
Amith Yamasanif37447b2009-10-08 18:28:01 -07007624 public void setRadioScanningTimeout(long timeout) {
7625 if (mPhoneSignalScanningTimer != null) {
7626 mPhoneSignalScanningTimer.setTimeout(timeout);
7627 }
7628 }
7629
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007630 public void updateDailyDeadlineLocked() {
7631 // Get the current time.
7632 long currentTime = mDailyStartTime = System.currentTimeMillis();
7633 Calendar calDeadline = Calendar.getInstance();
7634 calDeadline.setTimeInMillis(currentTime);
7635
7636 // Move time up to the next day, ranging from 1am to 3pm.
7637 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
7638 calDeadline.set(Calendar.MILLISECOND, 0);
7639 calDeadline.set(Calendar.SECOND, 0);
7640 calDeadline.set(Calendar.MINUTE, 0);
7641 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
7642 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
7643 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
7644 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
7645 }
7646
7647 public void recordDailyStatsIfNeededLocked(boolean settled) {
7648 long currentTime = System.currentTimeMillis();
7649 if (currentTime >= mNextMaxDailyDeadline) {
7650 recordDailyStatsLocked();
7651 } else if (settled && currentTime >= mNextMinDailyDeadline) {
7652 recordDailyStatsLocked();
7653 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
7654 recordDailyStatsLocked();
7655 }
7656 }
7657
7658 public void recordDailyStatsLocked() {
7659 DailyItem item = new DailyItem();
7660 item.mStartTime = mDailyStartTime;
7661 item.mEndTime = System.currentTimeMillis();
7662 boolean hasData = false;
7663 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
7664 hasData = true;
7665 item.mDischargeSteps = new LevelStepTracker(
7666 mDailyDischargeStepTracker.mNumStepDurations,
7667 mDailyDischargeStepTracker.mStepDurations);
7668 }
7669 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
7670 hasData = true;
7671 item.mChargeSteps = new LevelStepTracker(
7672 mDailyChargeStepTracker.mNumStepDurations,
7673 mDailyChargeStepTracker.mStepDurations);
7674 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007675 if (mDailyPackageChanges != null) {
7676 hasData = true;
7677 item.mPackageChanges = mDailyPackageChanges;
7678 mDailyPackageChanges = null;
7679 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007680 mDailyDischargeStepTracker.init();
7681 mDailyChargeStepTracker.init();
7682 updateDailyDeadlineLocked();
7683
7684 if (hasData) {
7685 mDailyItems.add(item);
7686 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
7687 mDailyItems.remove(0);
7688 }
7689 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
7690 try {
7691 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007692 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007693 writeDailyItemsLocked(out);
7694 BackgroundThread.getHandler().post(new Runnable() {
7695 @Override
7696 public void run() {
7697 synchronized (mCheckinFile) {
7698 FileOutputStream stream = null;
7699 try {
7700 stream = mDailyFile.startWrite();
7701 memStream.writeTo(stream);
7702 stream.flush();
7703 FileUtils.sync(stream);
7704 stream.close();
7705 mDailyFile.finishWrite(stream);
7706 } catch (IOException e) {
7707 Slog.w("BatteryStats",
7708 "Error writing battery daily items", e);
7709 mDailyFile.failWrite(stream);
7710 }
7711 }
7712 }
7713 });
7714 } catch (IOException e) {
7715 }
7716 }
7717 }
7718
7719 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7720 StringBuilder sb = new StringBuilder(64);
7721 out.startDocument(null, true);
7722 out.startTag(null, "daily-items");
7723 for (int i=0; i<mDailyItems.size(); i++) {
7724 final DailyItem dit = mDailyItems.get(i);
7725 out.startTag(null, "item");
7726 out.attribute(null, "start", Long.toString(dit.mStartTime));
7727 out.attribute(null, "end", Long.toString(dit.mEndTime));
7728 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7729 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007730 if (dit.mPackageChanges != null) {
7731 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7732 PackageChange pc = dit.mPackageChanges.get(j);
7733 if (pc.mUpdate) {
7734 out.startTag(null, "upd");
7735 out.attribute(null, "pkg", pc.mPackageName);
7736 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7737 out.endTag(null, "upd");
7738 } else {
7739 out.startTag(null, "rem");
7740 out.attribute(null, "pkg", pc.mPackageName);
7741 out.endTag(null, "rem");
7742 }
7743 }
7744 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007745 out.endTag(null, "item");
7746 }
7747 out.endTag(null, "daily-items");
7748 out.endDocument();
7749 }
7750
7751 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7752 StringBuilder tmpBuilder) throws IOException {
7753 if (steps != null) {
7754 out.startTag(null, tag);
7755 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7756 for (int i=0; i<steps.mNumStepDurations; i++) {
7757 out.startTag(null, "s");
7758 tmpBuilder.setLength(0);
7759 steps.encodeEntryAt(i, tmpBuilder);
7760 out.attribute(null, "v", tmpBuilder.toString());
7761 out.endTag(null, "s");
7762 }
7763 out.endTag(null, tag);
7764 }
7765 }
7766
7767 public void readDailyStatsLocked() {
7768 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7769 mDailyItems.clear();
7770 FileInputStream stream;
7771 try {
7772 stream = mDailyFile.openRead();
7773 } catch (FileNotFoundException e) {
7774 return;
7775 }
7776 try {
7777 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007778 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007779 readDailyItemsLocked(parser);
7780 } catch (XmlPullParserException e) {
7781 } finally {
7782 try {
7783 stream.close();
7784 } catch (IOException e) {
7785 }
7786 }
7787 }
7788
7789 private void readDailyItemsLocked(XmlPullParser parser) {
7790 try {
7791 int type;
7792 while ((type = parser.next()) != XmlPullParser.START_TAG
7793 && type != XmlPullParser.END_DOCUMENT) {
7794 ;
7795 }
7796
7797 if (type != XmlPullParser.START_TAG) {
7798 throw new IllegalStateException("no start tag found");
7799 }
7800
7801 int outerDepth = parser.getDepth();
7802 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7803 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7804 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7805 continue;
7806 }
7807
7808 String tagName = parser.getName();
7809 if (tagName.equals("item")) {
7810 readDailyItemTagLocked(parser);
7811 } else {
7812 Slog.w(TAG, "Unknown element under <daily-items>: "
7813 + parser.getName());
7814 XmlUtils.skipCurrentTag(parser);
7815 }
7816 }
7817
7818 } catch (IllegalStateException e) {
7819 Slog.w(TAG, "Failed parsing daily " + e);
7820 } catch (NullPointerException e) {
7821 Slog.w(TAG, "Failed parsing daily " + e);
7822 } catch (NumberFormatException e) {
7823 Slog.w(TAG, "Failed parsing daily " + e);
7824 } catch (XmlPullParserException e) {
7825 Slog.w(TAG, "Failed parsing daily " + e);
7826 } catch (IOException e) {
7827 Slog.w(TAG, "Failed parsing daily " + e);
7828 } catch (IndexOutOfBoundsException e) {
7829 Slog.w(TAG, "Failed parsing daily " + e);
7830 }
7831 }
7832
7833 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7834 XmlPullParserException, IOException {
7835 DailyItem dit = new DailyItem();
7836 String attr = parser.getAttributeValue(null, "start");
7837 if (attr != null) {
7838 dit.mStartTime = Long.parseLong(attr);
7839 }
7840 attr = parser.getAttributeValue(null, "end");
7841 if (attr != null) {
7842 dit.mEndTime = Long.parseLong(attr);
7843 }
7844 int outerDepth = parser.getDepth();
7845 int type;
7846 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7847 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7848 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7849 continue;
7850 }
7851
7852 String tagName = parser.getName();
7853 if (tagName.equals("dis")) {
7854 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
7855 } else if (tagName.equals("chg")) {
7856 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007857 } else if (tagName.equals("upd")) {
7858 if (dit.mPackageChanges == null) {
7859 dit.mPackageChanges = new ArrayList<>();
7860 }
7861 PackageChange pc = new PackageChange();
7862 pc.mUpdate = true;
7863 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7864 String verStr = parser.getAttributeValue(null, "ver");
7865 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
7866 dit.mPackageChanges.add(pc);
7867 XmlUtils.skipCurrentTag(parser);
7868 } else if (tagName.equals("rem")) {
7869 if (dit.mPackageChanges == null) {
7870 dit.mPackageChanges = new ArrayList<>();
7871 }
7872 PackageChange pc = new PackageChange();
7873 pc.mUpdate = false;
7874 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7875 dit.mPackageChanges.add(pc);
7876 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007877 } else {
7878 Slog.w(TAG, "Unknown element under <item>: "
7879 + parser.getName());
7880 XmlUtils.skipCurrentTag(parser);
7881 }
7882 }
7883 mDailyItems.add(dit);
7884 }
7885
7886 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
7887 String tag)
7888 throws NumberFormatException, XmlPullParserException, IOException {
7889 final String numAttr = parser.getAttributeValue(null, "n");
7890 if (numAttr == null) {
7891 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
7892 XmlUtils.skipCurrentTag(parser);
7893 return;
7894 }
7895 final int num = Integer.parseInt(numAttr);
7896 LevelStepTracker steps = new LevelStepTracker(num);
7897 if (isCharge) {
7898 dit.mChargeSteps = steps;
7899 } else {
7900 dit.mDischargeSteps = steps;
7901 }
7902 int i = 0;
7903 int outerDepth = parser.getDepth();
7904 int type;
7905 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7906 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7907 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7908 continue;
7909 }
7910
7911 String tagName = parser.getName();
7912 if ("s".equals(tagName)) {
7913 if (i < num) {
7914 String valueAttr = parser.getAttributeValue(null, "v");
7915 if (valueAttr != null) {
7916 steps.decodeEntryAt(i, valueAttr);
7917 i++;
7918 }
7919 }
7920 } else {
7921 Slog.w(TAG, "Unknown element under <" + tag + ">: "
7922 + parser.getName());
7923 XmlUtils.skipCurrentTag(parser);
7924 }
7925 }
7926 steps.mNumStepDurations = i;
7927 }
7928
7929 @Override
7930 public DailyItem getDailyItemLocked(int daysAgo) {
7931 int index = mDailyItems.size()-1-daysAgo;
7932 return index >= 0 ? mDailyItems.get(index) : null;
7933 }
7934
7935 @Override
7936 public long getCurrentDailyStartTime() {
7937 return mDailyStartTime;
7938 }
7939
7940 @Override
7941 public long getNextMinDailyDeadline() {
7942 return mNextMinDailyDeadline;
7943 }
7944
7945 @Override
7946 public long getNextMaxDailyDeadline() {
7947 return mNextMaxDailyDeadline;
7948 }
7949
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007950 @Override
7951 public boolean startIteratingOldHistoryLocked() {
7952 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7953 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007954 if ((mHistoryIterator = mHistory) == null) {
7955 return false;
7956 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007957 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007958 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007959 mReadOverflow = false;
7960 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007961 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007962 }
7963
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007964 @Override
7965 public boolean getNextOldHistoryLocked(HistoryItem out) {
7966 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
7967 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007968 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007969 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007970 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007971 HistoryItem cur = mHistoryIterator;
7972 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007973 if (!mReadOverflow && !end) {
7974 Slog.w(TAG, "Old history ends before new history!");
7975 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007976 return false;
7977 }
7978 out.setTo(cur);
7979 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007980 if (!mReadOverflow) {
7981 if (end) {
7982 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007983 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07007984 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007985 pw.println("Histories differ!");
7986 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007987 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007988 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007989 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
7990 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07007991 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007992 }
7993 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007994 return true;
7995 }
7996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007998 public void finishIteratingOldHistoryLocked() {
7999 mIteratingHistory = false;
8000 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008001 mHistoryIterator = null;
8002 }
8003
8004 public int getHistoryTotalSize() {
8005 return MAX_HISTORY_BUFFER;
8006 }
8007
8008 public int getHistoryUsedSize() {
8009 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008010 }
8011
8012 @Override
8013 public boolean startIteratingHistoryLocked() {
8014 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8015 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008016 if (mHistoryBuffer.dataSize() <= 0) {
8017 return false;
8018 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008019 mHistoryBuffer.setDataPosition(0);
8020 mReadOverflow = false;
8021 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008022 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8023 mReadHistoryUids = new int[mHistoryTagPool.size()];
8024 mReadHistoryChars = 0;
8025 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8026 final HistoryTag tag = ent.getKey();
8027 final int idx = ent.getValue();
8028 mReadHistoryStrings[idx] = tag.string;
8029 mReadHistoryUids[idx] = tag.uid;
8030 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008031 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008032 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008033 }
8034
8035 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008036 public int getHistoryStringPoolSize() {
8037 return mReadHistoryStrings.length;
8038 }
8039
8040 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008041 public int getHistoryStringPoolBytes() {
8042 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8043 // Each string character is 2 bytes.
8044 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8045 }
8046
8047 @Override
8048 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008049 return mReadHistoryStrings[index];
8050 }
8051
8052 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008053 public int getHistoryTagPoolUid(int index) {
8054 return mReadHistoryUids[index];
8055 }
8056
8057 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008058 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008059 final int pos = mHistoryBuffer.dataPosition();
8060 if (pos == 0) {
8061 out.clear();
8062 }
8063 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008064 if (end) {
8065 return false;
8066 }
8067
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008068 final long lastRealtime = out.time;
8069 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008070 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008071 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8072 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008073 out.currentTime = lastWalltime + (out.time - lastRealtime);
8074 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008075 return true;
8076 }
8077
8078 @Override
8079 public void finishIteratingHistoryLocked() {
8080 mIteratingHistory = false;
8081 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008082 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008083 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008084
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008085 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008086 public long getHistoryBaseTime() {
8087 return mHistoryBaseTime;
8088 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008089
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008090 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091 public int getStartCount() {
8092 return mStartCount;
8093 }
8094
8095 public boolean isOnBattery() {
8096 return mOnBattery;
8097 }
8098
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008099 public boolean isCharging() {
8100 return mCharging;
8101 }
8102
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008103 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008104 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008105 }
8106
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008107 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008108 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008109 mOnBatteryTimeBase.init(uptime, realtime);
8110 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008111 mRealtime = 0;
8112 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008113 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008114 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008115 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008116
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008117 void initDischarge() {
8118 mLowDischargeAmountSinceCharge = 0;
8119 mHighDischargeAmountSinceCharge = 0;
8120 mDischargeAmountScreenOn = 0;
8121 mDischargeAmountScreenOnSinceCharge = 0;
8122 mDischargeAmountScreenOff = 0;
8123 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008124 mDischargeStepTracker.init();
8125 mChargeStepTracker.init();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008126 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008127
8128 public void resetAllStatsCmdLocked() {
8129 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008130 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008131 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008132 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008133 long realtime = mSecRealtime * 1000;
8134 mDischargeStartLevel = mHistoryCur.batteryLevel;
8135 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008136 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008137 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8138 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008139 mOnBatteryTimeBase.reset(uptime, realtime);
8140 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8141 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008142 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008143 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8144 mDischargeScreenOffUnplugLevel = 0;
8145 } else {
8146 mDischargeScreenOnUnplugLevel = 0;
8147 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8148 }
8149 mDischargeAmountScreenOn = 0;
8150 mDischargeAmountScreenOff = 0;
8151 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008152 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008153 }
8154
8155 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008156 mStartCount = 0;
Joe Onoratoabded112016-02-08 16:49:39 -08008157 initTimes(mClocks.uptimeMillis() * 1000, mClocks.elapsedRealtime() * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008158 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008159 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008160 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008161 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008162 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008163 mPowerSaveModeEnabledTimer.reset(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008164 mLongestLightIdleTime = 0;
8165 mLongestFullIdleTime = 0;
8166 mDeviceIdleModeLightTimer.reset(false);
8167 mDeviceIdleModeFullTimer.reset(false);
8168 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008169 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008170 mPhoneOnTimer.reset(false);
8171 mAudioOnTimer.reset(false);
8172 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008173 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008174 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008175 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008176 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008177 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008178 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008179 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008180 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008181 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008182 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008183 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008184 mNetworkByteActivityCounters[i].reset(false);
8185 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008186 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008187 mMobileRadioActiveTimer.reset(false);
8188 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008189 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008190 mMobileRadioActiveUnknownTime.reset(false);
8191 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008192 mWifiOnTimer.reset(false);
8193 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008194 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008195 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008196 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008197 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8198 mWifiSupplStateTimer[i].reset(false);
8199 }
8200 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8201 mWifiSignalStrengthsTimer[i].reset(false);
8202 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008203 mWifiActivity.reset(false);
8204 mBluetoothActivity.reset(false);
8205 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008206 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008207
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008208 for (int i=0; i<mUidStats.size(); i++) {
8209 if (mUidStats.valueAt(i).reset()) {
8210 mUidStats.remove(mUidStats.keyAt(i));
8211 i--;
8212 }
8213 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008214
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008215 if (mKernelWakelockStats.size() > 0) {
8216 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008217 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008218 }
8219 mKernelWakelockStats.clear();
8220 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008221
8222 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008223 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8224 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008225 }
8226 mWakeupReasonStats.clear();
8227 }
8228
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008229 mLastHistoryStepDetails = null;
8230 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8231 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8232 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8233 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8234 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8235 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8236 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8237 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8238 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8239 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8240
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008241 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008242
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008243 clearHistoryLocked();
8244 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008245
Dianne Hackborn40c87252014-03-19 16:55:40 -07008246 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008247 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008248 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8249 // Not recording process starts/stops.
8250 continue;
8251 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008252 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008253 if (active == null) {
8254 continue;
8255 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008256 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8257 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008258 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008259 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8260 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008261 }
8262 }
8263 }
8264 }
8265
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008266 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008267 if (oldScreenOn) {
8268 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8269 if (diff > 0) {
8270 mDischargeAmountScreenOn += diff;
8271 mDischargeAmountScreenOnSinceCharge += diff;
8272 }
8273 } else {
8274 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8275 if (diff > 0) {
8276 mDischargeAmountScreenOff += diff;
8277 mDischargeAmountScreenOffSinceCharge += diff;
8278 }
8279 }
8280 if (newScreenOn) {
8281 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8282 mDischargeScreenOffUnplugLevel = 0;
8283 } else {
8284 mDischargeScreenOnUnplugLevel = 0;
8285 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8286 }
8287 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008288
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008289 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008290 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008291 final boolean screenOn = mScreenState == Display.STATE_ON;
8292 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008293 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008294 }
8295
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008296 private String[] mMobileIfaces = EmptyArray.STRING;
8297 private String[] mWifiIfaces = EmptyArray.STRING;
8298
8299 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8300
8301 private static final int NETWORK_STATS_LAST = 0;
8302 private static final int NETWORK_STATS_NEXT = 1;
8303 private static final int NETWORK_STATS_DELTA = 2;
8304
Joe Onoratoabded112016-02-08 16:49:39 -08008305 private NetworkStats[] mMobileNetworkStats;
8306 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008307
8308 /**
8309 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8310 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8311 */
8312 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8313 NetworkStats[] networkStatsBuffer)
8314 throws IOException {
8315 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8316 false)) {
8317 return null;
8318 }
8319
8320 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8321 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8322 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8323 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8324 networkStatsBuffer[NETWORK_STATS_DELTA]);
8325 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8326 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8327 return networkStatsBuffer[NETWORK_STATS_DELTA];
8328 }
8329
8330 /**
8331 * Distribute WiFi energy info and network traffic to apps.
8332 * @param info The energy information from the WiFi controller.
8333 */
8334 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008335 if (DEBUG_ENERGY) {
8336 Slog.d(TAG, "Updating wifi stats");
8337 }
8338
Joe Onoratoabded112016-02-08 16:49:39 -08008339 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008340 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008341 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008342 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8343 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8344 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008345 } catch (IOException e) {
8346 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8347 return;
8348 }
8349
8350 if (!mOnBatteryInternal) {
8351 return;
8352 }
8353
Adam Lesinskie08af192015-03-25 16:42:59 -07008354 SparseLongArray rxPackets = new SparseLongArray();
8355 SparseLongArray txPackets = new SparseLongArray();
8356 long totalTxPackets = 0;
8357 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008358 if (delta != null) {
8359 final int size = delta.size();
8360 for (int i = 0; i < size; i++) {
8361 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8362
Adam Lesinskie08af192015-03-25 16:42:59 -07008363 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008364 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008365 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8366 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008367 }
8368
Adam Lesinski6cca4142016-02-25 18:23:02 -08008369 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8370 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008371 continue;
8372 }
8373
8374 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008375 if (entry.rxBytes != 0) {
8376 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8377 entry.rxPackets);
8378 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8379 entry.rxBytes);
8380 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8381 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008382
Adam Lesinskiba88e682015-12-08 12:06:55 -08008383 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008384
Adam Lesinskiba88e682015-12-08 12:06:55 -08008385 // Sum the total number of packets so that the Rx Power can
8386 // be evenly distributed amongst the apps.
8387 totalRxPackets += entry.rxPackets;
8388 }
8389
8390 if (entry.txBytes != 0) {
8391 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8392 entry.txPackets);
8393 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8394 entry.txBytes);
8395 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8396 entry.txPackets);
8397
8398 txPackets.put(u.getUid(), entry.txPackets);
8399
8400 // Sum the total number of packets so that the Tx Power can
8401 // be evenly distributed amongst the apps.
8402 totalTxPackets += entry.txPackets;
8403 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008404 }
8405 }
8406
8407 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008408 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008409
Adam Lesinskie08af192015-03-25 16:42:59 -07008410 // Measured in mAms
8411 final long txTimeMs = info.getControllerTxTimeMillis();
8412 final long rxTimeMs = info.getControllerRxTimeMillis();
8413 final long idleTimeMs = info.getControllerIdleTimeMillis();
8414 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8415
8416 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008417 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008418
8419 if (DEBUG_ENERGY) {
8420 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8421 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8422 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8423 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8424 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8425 }
8426
8427 long totalWifiLockTimeMs = 0;
8428 long totalScanTimeMs = 0;
8429
8430 // On the first pass, collect some totals so that we can normalize power
8431 // calculations if we need to.
8432 final int uidStatsSize = mUidStats.size();
8433 for (int i = 0; i < uidStatsSize; i++) {
8434 final Uid uid = mUidStats.valueAt(i);
8435
8436 // Sum the total scan power for all apps.
8437 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8438 elapsedRealtimeMs * 1000) / 1000;
8439
8440 // Sum the total time holding wifi lock for all apps.
8441 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8442 elapsedRealtimeMs * 1000) / 1000;
8443 }
8444
8445 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8446 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8447 + rxTimeMs + " ms). Normalizing scan time.");
8448 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008449 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8450 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8451 + txTimeMs + " ms). Normalizing scan time.");
8452 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008453
8454 // Actually assign and distribute power usage to apps.
8455 for (int i = 0; i < uidStatsSize; i++) {
8456 final Uid uid = mUidStats.valueAt(i);
8457
8458 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8459 elapsedRealtimeMs * 1000) / 1000;
8460 if (scanTimeSinceMarkMs > 0) {
8461 // Set the new mark so that next time we get new data since this point.
8462 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8463
Mitchell Willsf9016492015-07-29 17:47:44 -07008464 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8465 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8466
8467 // Our total scan time is more than the reported Tx/Rx time.
8468 // This is possible because the cost of a scan is approximate.
8469 // Let's normalize the result so that we evenly blame each app
8470 // scanning.
8471 //
8472 // This means that we may have apps that transmitted/received packets not be
8473 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008474 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008475 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8476 totalScanTimeMs;
8477 }
8478 if (totalScanTimeMs > txTimeMs) {
8479 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8480 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008481 }
8482
8483 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008484 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8485 + scanRxTimeSinceMarkMs + " ms Tx:"
8486 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008487 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008488
8489 ControllerActivityCounterImpl activityCounter =
8490 uid.getOrCreateWifiControllerActivityLocked();
8491 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
8492 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07008493 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
8494 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008495 }
8496
8497 // Distribute evenly the power consumed while Idle to each app holding a WiFi
8498 // lock.
8499 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8500 elapsedRealtimeMs * 1000) / 1000;
8501 if (wifiLockTimeSinceMarkMs > 0) {
8502 // Set the new mark so that next time we get new data since this point.
8503 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
8504
8505 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
8506 / totalWifiLockTimeMs;
8507 if (DEBUG_ENERGY) {
8508 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
8509 + myIdleTimeMs + " ms");
8510 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008511 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
8512 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008513 }
8514 }
8515
8516 if (DEBUG_ENERGY) {
8517 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07008518 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07008519 }
8520
Mitchell Willsf9016492015-07-29 17:47:44 -07008521 // Distribute the remaining Tx power appropriately between all apps that transmitted
8522 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07008523 for (int i = 0; i < txPackets.size(); i++) {
8524 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07008525 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07008526 if (DEBUG_ENERGY) {
8527 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
8528 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008529 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
8530 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008531 }
8532
8533 // Distribute the remaining Rx power appropriately between all apps that received
8534 // packets.
8535 for (int i = 0; i < rxPackets.size(); i++) {
8536 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
8537 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
8538 if (DEBUG_ENERGY) {
8539 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
8540 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008541 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
8542 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008543 }
8544
8545 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
8546
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008547 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008548 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
8549 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
8550 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07008551
Adam Lesinski8576cf92015-06-09 12:48:25 -07008552 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8553 final double opVolt = mPowerProfile.getAveragePower(
8554 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8555 if (opVolt != 0) {
8556 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008557 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07008558 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07008559 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008560 }
8561 }
8562
8563 /**
8564 * Distribute Cell radio energy info and network traffic to apps.
8565 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008566 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
8567 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008568 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008569 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008570 }
8571
Adam Lesinskie08af192015-03-25 16:42:59 -07008572 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008573 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008574 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
8575 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
8576 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008577 } catch (IOException e) {
8578 Slog.wtf(TAG, "Failed to get mobile network stats", e);
8579 return;
8580 }
8581
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008582 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008583 return;
8584 }
8585
Adam Lesinskie08af192015-03-25 16:42:59 -07008586 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
8587 elapsedRealtimeMs * 1000);
8588 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008589
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008590 long totalRxPackets = 0;
8591 long totalTxPackets = 0;
8592 if (delta != null) {
8593 final int size = delta.size();
8594 for (int i = 0; i < size; i++) {
8595 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08008596 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008597 continue;
8598 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008599
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008600 if (DEBUG_ENERGY) {
8601 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
8602 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8603 + " txPackets=" + entry.txPackets);
8604 }
8605
8606 totalRxPackets += entry.rxPackets;
8607 totalTxPackets += entry.txPackets;
8608
8609 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8610 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
8611 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
8612
8613 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8614 entry.rxBytes);
8615 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8616 entry.txBytes);
8617 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8618 entry.rxPackets);
8619 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8620 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008621 }
8622
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008623 // Now distribute proportional blame to the apps that did networking.
8624 long totalPackets = totalRxPackets + totalTxPackets;
8625 if (totalPackets > 0) {
8626 for (int i = 0; i < size; i++) {
8627 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8628 if (entry.rxPackets == 0 && entry.txPackets == 0) {
8629 continue;
8630 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008631
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008632 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8633
8634 // Distribute total radio active time in to this app.
8635 final long appPackets = entry.rxPackets + entry.txPackets;
8636 final long appRadioTime = (radioTime * appPackets) / totalPackets;
8637 u.noteMobileRadioActiveTimeLocked(appRadioTime);
8638
8639 // Remove this app from the totals, so that we don't lose any time
8640 // due to rounding.
8641 radioTime -= appRadioTime;
8642 totalPackets -= appPackets;
8643
8644 if (activityInfo != null) {
8645 ControllerActivityCounterImpl activityCounter =
8646 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08008647 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008648 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
8649 / totalRxPackets;
8650 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
8651 }
8652
Adam Lesinskia4268172016-01-29 12:13:54 -08008653 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008654 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8655 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
8656 txMs /= totalTxPackets;
8657 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
8658 }
8659 }
8660 }
8661 }
8662 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008663
8664 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008665 // Whoops, there is some radio time we can't blame on an app!
8666 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
8667 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008668 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008669 }
8670
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008671 if (activityInfo != null) {
8672 mHasModemReporting = true;
8673 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
8674 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
8675 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8676 mModemActivity.getTxTimeCounters()[lvl]
8677 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
8678 }
8679
8680 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8681 final double opVolt = mPowerProfile.getAveragePower(
8682 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8683 if (opVolt != 0) {
8684 // We store the power drain as mAms.
8685 mModemActivity.getPowerCounter().addCountLocked(
8686 (long) (activityInfo.getEnergyUsed() / opVolt));
8687 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008688 }
8689 }
8690
8691 /**
8692 * Distribute Bluetooth energy info and network traffic to apps.
8693 * @param info The energy information from the bluetooth controller.
8694 */
8695 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008696 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008697 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008698 }
8699
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008700 if (info == null || !mOnBatteryInternal) {
8701 return;
8702 }
Adam Lesinskie283d332015-04-16 12:29:25 -07008703
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008704 mHasBluetoothReporting = true;
8705
8706 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
8707 final long rxTimeMs = info.getControllerRxTimeMillis();
8708 final long txTimeMs = info.getControllerTxTimeMillis();
8709
8710 if (DEBUG_ENERGY) {
8711 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
8712 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8713 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8714 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
8715 }
8716
8717 long totalScanTimeMs = 0;
8718
8719 final int uidCount = mUidStats.size();
8720 for (int i = 0; i < uidCount; i++) {
8721 final Uid u = mUidStats.valueAt(i);
8722 if (u.mBluetoothScanTimer == null) {
8723 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07008724 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008725
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008726 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8727 elapsedRealtimeMs * 1000) / 1000;
8728 }
8729
8730 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
8731 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
8732
8733 if (DEBUG_ENERGY) {
8734 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
8735 + " TX=" + normalizeScanTxTime);
8736 }
8737
8738 long leftOverRxTimeMs = rxTimeMs;
8739 long leftOverTxTimeMs = txTimeMs;
8740
8741 for (int i = 0; i < uidCount; i++) {
8742 final Uid u = mUidStats.valueAt(i);
8743 if (u.mBluetoothScanTimer == null) {
8744 continue;
8745 }
8746
8747 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8748 elapsedRealtimeMs * 1000) / 1000;
8749 if (scanTimeSinceMarkMs > 0) {
8750 // Set the new mark so that next time we get new data since this point.
8751 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
8752
8753 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
8754 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
8755
8756 if (normalizeScanRxTime) {
8757 // Scan time is longer than the total rx time in the controller,
8758 // so distribute the scan time proportionately. This means regular traffic
8759 // will not blamed, but scans are more expensive anyways.
8760 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
8761 }
8762
8763 if (normalizeScanTxTime) {
8764 // Scan time is longer than the total tx time in the controller,
8765 // so distribute the scan time proportionately. This means regular traffic
8766 // will not blamed, but scans are more expensive anyways.
8767 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
8768 }
8769
8770 final ControllerActivityCounterImpl counter =
8771 u.getOrCreateBluetoothControllerActivityLocked();
8772 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
8773 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
8774
8775 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
8776 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
8777 }
8778 }
8779
8780 if (DEBUG_ENERGY) {
8781 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
8782 + " TX=" + leftOverTxTimeMs);
8783 }
8784
8785 //
8786 // Now distribute blame to apps that did bluetooth traffic.
8787 //
8788
8789 long totalTxBytes = 0;
8790 long totalRxBytes = 0;
8791
8792 final UidTraffic[] uidTraffic = info.getUidTraffic();
8793 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
8794 for (int i = 0; i < numUids; i++) {
8795 final UidTraffic traffic = uidTraffic[i];
8796
8797 // Add to the global counters.
8798 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
8799 traffic.getRxBytes());
8800 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
8801 traffic.getTxBytes());
8802
8803 // Add to the UID counters.
8804 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
8805 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
8806 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
8807
8808 // Calculate the total traffic.
8809 totalTxBytes += traffic.getTxBytes();
8810 totalRxBytes += traffic.getRxBytes();
8811 }
8812
8813 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
8814 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008815 for (int i = 0; i < numUids; i++) {
8816 final UidTraffic traffic = uidTraffic[i];
8817
Adam Lesinski50e47602015-12-04 17:04:54 -08008818 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008819 final ControllerActivityCounterImpl counter =
8820 u.getOrCreateBluetoothControllerActivityLocked();
8821
8822 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
8823 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
8824
8825 if (DEBUG_ENERGY) {
8826 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
8827 + " rx_time=" + timeRxMs);
8828 }
8829 counter.getRxTimeCounter().addCountLocked(timeRxMs);
8830 leftOverRxTimeMs -= timeRxMs;
8831 }
8832
8833 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
8834 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
8835
8836 if (DEBUG_ENERGY) {
8837 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
8838 + " tx_time=" + timeTxMs);
8839 }
8840
8841 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
8842 leftOverTxTimeMs -= timeTxMs;
8843 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008844 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008845 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008846
8847 mBluetoothActivity.getRxTimeCounter().addCountLocked(
8848 info.getControllerRxTimeMillis());
8849 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
8850 info.getControllerTxTimeMillis());
8851 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
8852 info.getControllerIdleTimeMillis());
8853
8854 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8855 final double opVolt = mPowerProfile.getAveragePower(
8856 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8857 if (opVolt != 0) {
8858 // We store the power drain as mAms.
8859 mBluetoothActivity.getPowerCounter().addCountLocked(
8860 (long) (info.getControllerEnergyUsed() / opVolt));
8861 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008862 }
8863
8864 /**
8865 * Read and distribute kernel wake lock use across apps.
8866 */
8867 public void updateKernelWakelocksLocked() {
8868 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
8869 mTmpWakelockStats);
8870 if (wakelockStats == null) {
8871 // Not crashing might make board bringup easier.
8872 Slog.w(TAG, "Couldn't get kernel wake lock stats");
8873 return;
8874 }
8875
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008876 // Record whether we've seen a non-zero time (for debugging b/22716723).
8877 boolean seenNonZeroTime = false;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008878 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
8879 String name = ent.getKey();
8880 KernelWakelockStats.Entry kws = ent.getValue();
8881
8882 SamplingTimer kwlt = mKernelWakelockStats.get(name);
8883 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07008884 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008885 mKernelWakelockStats.put(name, kwlt);
8886 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07008887 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008888 kwlt.setUpdateVersion(kws.mVersion);
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008889
Adam Lesinski757c6ea2016-04-21 09:55:41 -07008890 if (kws.mVersion != wakelockStats.kernelWakelockVersion) {
8891 seenNonZeroTime |= kws.mTotalTime > 0;
8892 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008893 }
8894
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008895 int numWakelocksSetStale = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008896 if (wakelockStats.size() != mKernelWakelockStats.size()) {
8897 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
8898 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8899 SamplingTimer st = ent.getValue();
8900 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07008901 st.endSample();
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008902 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008903 }
8904 }
8905 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008906
8907 if (!seenNonZeroTime) {
8908 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
8909 }
8910
8911 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
8912 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
8913 wakelockStats.kernelWakelockVersion);
8914 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008915 }
8916
Adam Lesinski72478f02015-06-17 15:39:43 -07008917 // We use an anonymous class to access these variables,
8918 // so they can't live on the stack or they'd have to be
8919 // final MutableLong objects (more allocations).
8920 // Used in updateCpuTimeLocked().
8921 long mTempTotalCpuUserTimeUs;
8922 long mTempTotalCpuSystemTimeUs;
8923
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008924 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07008925 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
8926 * and we are on battery with screen off, we give more of the cpu time to those apps holding
8927 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008928 */
Adam Lesinski72478f02015-06-17 15:39:43 -07008929 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008930 if (mPowerProfile == null) {
8931 return;
8932 }
8933
Adam Lesinski72478f02015-06-17 15:39:43 -07008934 if (DEBUG_ENERGY_CPU) {
8935 Slog.d(TAG, "!Cpu updating!");
8936 }
8937
8938 // Holding a wakelock costs more than just using the cpu.
8939 // Currently, we assign only half the cpu time to an app that is running but
8940 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
8941 // If no app is holding a wakelock, then the distribution is normal.
8942 final int wakelockWeight = 50;
8943
Adam Lesinski6832f392015-09-05 18:05:40 -07008944 // Read the time spent for each cluster at various cpu frequencies.
8945 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
8946 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
8947 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
8948 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008949
8950 int numWakelocks = 0;
8951
8952 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
8953 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
8954 final int numPartialTimers = mPartialTimers.size();
8955 if (mOnBatteryScreenOffTimeBase.isRunning()) {
8956 for (int i = 0; i < numPartialTimers; i++) {
8957 final StopwatchTimer timer = mPartialTimers.get(i);
8958 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8959 // Since the collection and blaming of wakelocks can be scheduled to run after
8960 // some delay, the mPartialTimers list may have new entries. We can't blame
8961 // the newly added timer for past cpu time, so we only consider timers that
8962 // were present for one round of collection. Once a timer has gone through
8963 // a round of collection, its mInList field is set to true.
8964 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008965 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008966 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008967 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008968
8969 final int numWakelocksF = numWakelocks;
8970 mTempTotalCpuUserTimeUs = 0;
8971 mTempTotalCpuSystemTimeUs = 0;
8972
8973 // Read the CPU data for each UID. This will internally generate a snapshot so next time
8974 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
8975 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08008976 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07008977 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
8978 new KernelUidCpuTimeReader.Callback() {
8979 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008980 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
8981 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07008982 final Uid u = getUidStatsLocked(mapUid(uid));
8983
8984 // Accumulate the total system and user time.
8985 mTempTotalCpuUserTimeUs += userTimeUs;
8986 mTempTotalCpuSystemTimeUs += systemTimeUs;
8987
8988 StringBuilder sb = null;
8989 if (DEBUG_ENERGY_CPU) {
8990 sb = new StringBuilder();
8991 sb.append(" got time for uid=").append(u.mUid).append(": u=");
8992 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8993 sb.append(" s=");
8994 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008995 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07008996 }
8997
8998 if (numWakelocksF > 0) {
8999 // We have wakelocks being held, so only give a portion of the
9000 // time to the process. The rest will be distributed among wakelock
9001 // holders.
9002 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9003 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9004 }
9005
9006 if (sb != null) {
9007 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9008 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9009 sb.append(" s=");
9010 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009011 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009012 Slog.d(TAG, sb.toString());
9013 }
9014
9015 u.mUserCpuTime.addCountLocked(userTimeUs);
9016 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009017 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009018
9019 // Add the cpu speeds to this UID. These are used as a ratio
9020 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009021 final int numClusters = mPowerProfile.getNumCpuClusters();
9022 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9023 numClusters) {
9024 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009025 }
9026
9027 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009028 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9029 cluster);
9030 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9031 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009032 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009033 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009034 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009035
9036 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9037 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9038 if (cpuSpeeds[speed] == null) {
9039 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9040 }
9041 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9042 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009043 }
9044 }
9045 });
9046
9047 if (DEBUG_ENERGY_CPU) {
Joe Onoratoabded112016-02-08 16:49:39 -08009048 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009049 " ms");
9050 }
9051
9052 if (mOnBatteryInternal && numWakelocks > 0) {
9053 // Distribute a portion of the total cpu time to wakelock holders.
9054 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9055 mTempTotalCpuSystemTimeUs =
9056 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9057
9058 for (int i = 0; i < numPartialTimers; i++) {
9059 final StopwatchTimer timer = mPartialTimers.get(i);
9060
9061 // The system does not share any blame, as it is usually holding the wakelock
9062 // on behalf of an app.
9063 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9064 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9065 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9066
9067 if (DEBUG_ENERGY_CPU) {
9068 StringBuilder sb = new StringBuilder();
9069 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9070 .append(": u=");
9071 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9072 sb.append(" s=");
9073 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9074 Slog.d(TAG, sb.toString());
9075 }
9076
9077 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9078 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9079
9080 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009081 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009082
9083 mTempTotalCpuUserTimeUs -= userTimeUs;
9084 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9085 numWakelocks--;
9086 }
9087 }
9088
9089 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9090 // Anything left over is given to the system.
9091 if (DEBUG_ENERGY_CPU) {
9092 StringBuilder sb = new StringBuilder();
9093 sb.append(" Distributing lost time to system: u=");
9094 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9095 sb.append(" s=");
9096 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9097 Slog.d(TAG, sb.toString());
9098 }
9099
9100 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9101 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9102 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9103
9104 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009105 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9106 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009107 }
9108 }
9109
9110 // See if there is a difference in wakelocks between this collection and the last
9111 // collection.
9112 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9113 // No difference, so each timer is now considered for the next collection.
9114 for (int i = 0; i < numPartialTimers; i++) {
9115 mPartialTimers.get(i).mInList = true;
9116 }
9117 } else {
9118 // The lists are different, meaning we added (or removed a timer) since the last
9119 // collection.
9120 final int numLastPartialTimers = mLastPartialTimers.size();
9121 for (int i = 0; i < numLastPartialTimers; i++) {
9122 mLastPartialTimers.get(i).mInList = false;
9123 }
9124 mLastPartialTimers.clear();
9125
9126 // Mark the current timers as gone through a collection.
9127 for (int i = 0; i < numPartialTimers; i++) {
9128 final StopwatchTimer timer = mPartialTimers.get(i);
9129 timer.mInList = true;
9130 mLastPartialTimers.add(timer);
9131 }
9132 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009133 }
9134
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009135 boolean setChargingLocked(boolean charging) {
9136 if (mCharging != charging) {
9137 mCharging = charging;
9138 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009139 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009140 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009141 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009142 }
9143 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9144 return true;
9145 }
9146 return false;
9147 }
9148
Dianne Hackborn40c87252014-03-19 16:55:40 -07009149 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
9150 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009151 boolean doWrite = false;
9152 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9153 m.arg1 = onBattery ? 1 : 0;
9154 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009155
Dianne Hackborn40c87252014-03-19 16:55:40 -07009156 final long uptime = mSecUptime * 1000;
9157 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009158 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009159 if (onBattery) {
9160 // We will reset our status if we are unplugging after the
9161 // battery was last full, or the level is at 100, or
9162 // we have gone through a significant charge (from a very low
9163 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009164 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009165 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009166 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009167 || (mDischargeCurrentLevel < 20 && level >= 80)
9168 || (getHighDischargeAmountSinceCharge() >= 200
9169 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009170 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009171 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009172 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9173 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009174 // Before we write, collect a snapshot of the final aggregated
9175 // stats to be reported in the next checkin. Only do this if we have
9176 // a sufficient amount of data to make it interesting.
9177 if (getLowDischargeAmountSinceCharge() >= 20) {
9178 final Parcel parcel = Parcel.obtain();
9179 writeSummaryToParcel(parcel, true);
9180 BackgroundThread.getHandler().post(new Runnable() {
9181 @Override public void run() {
9182 synchronized (mCheckinFile) {
9183 FileOutputStream stream = null;
9184 try {
9185 stream = mCheckinFile.startWrite();
9186 stream.write(parcel.marshall());
9187 stream.flush();
9188 FileUtils.sync(stream);
9189 stream.close();
9190 mCheckinFile.finishWrite(stream);
9191 } catch (IOException e) {
9192 Slog.w("BatteryStats",
9193 "Error writing checkin battery statistics", e);
9194 mCheckinFile.failWrite(stream);
9195 } finally {
9196 parcel.recycle();
9197 }
9198 }
9199 }
9200 });
9201 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009202 doWrite = true;
9203 resetAllStatsLocked();
9204 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009205 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009206 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009207 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009208 if (mCharging) {
9209 setChargingLocked(false);
9210 }
9211 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009212 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009213 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009214 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009215 mDischargeStepTracker.clearTime();
9216 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009217 mInitStepMode = mCurStepMode;
9218 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009219 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009220 mHistoryCur.batteryLevel = (byte)level;
9221 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9222 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9223 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009224 if (reset) {
9225 mRecordingHistory = true;
9226 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9227 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009228 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009229 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009230 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009231 mDischargeScreenOnUnplugLevel = level;
9232 mDischargeScreenOffUnplugLevel = 0;
9233 } else {
9234 mDischargeScreenOnUnplugLevel = 0;
9235 mDischargeScreenOffUnplugLevel = level;
9236 }
9237 mDischargeAmountScreenOn = 0;
9238 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009239 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009240 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009241 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009242 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009243 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009244 mHistoryCur.batteryLevel = (byte)level;
9245 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9246 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9247 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009248 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009249 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009250 if (level < mDischargeUnplugLevel) {
9251 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9252 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9253 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009254 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9255 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009256 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009257 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009258 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009259 mInitStepMode = mCurStepMode;
9260 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009261 }
9262 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9263 if (mFile != null) {
9264 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 }
9266 }
9267 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009268
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009269 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9270 boolean reset) {
9271 mRecordingHistory = true;
9272 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009273 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9274 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009275 mHistoryCur);
9276 mHistoryCur.currentTime = 0;
9277 if (reset) {
9278 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9279 }
9280 }
9281
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009282 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9283 final long uptimeMs) {
9284 if (mRecordingHistory) {
9285 mHistoryCur.currentTime = currentTime;
9286 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9287 mHistoryCur);
9288 mHistoryCur.currentTime = 0;
9289 }
9290 }
9291
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009292 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9293 if (mRecordingHistory) {
9294 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009295 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9296 mHistoryCur);
9297 mHistoryCur.currentTime = 0;
9298 }
9299 }
9300
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009301 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009302 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009303 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009304 }
9305 }
9306
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009307 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009308 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009309
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009310 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009311 int temp, int volt, int chargeUAh) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009312 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009313 final long uptime = mClocks.uptimeMillis();
9314 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009315 if (!mHaveBatteryLevel) {
9316 mHaveBatteryLevel = true;
9317 // We start out assuming that the device is plugged in (not
9318 // on battery). If our first report is now that we are indeed
9319 // plugged in, then twiddle our state to correctly reflect that
9320 // since we won't be going through the full setOnBattery().
9321 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009322 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009323 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009324 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009325 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009326 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009327 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009328 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009329 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009330 mHistoryCur.batteryStatus = (byte)status;
9331 mHistoryCur.batteryLevel = (byte)level;
9332 mMaxChargeStepLevel = mMinDischargeStepLevel =
9333 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009334 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009335 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9336 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9337 }
9338 int oldStatus = mHistoryCur.batteryStatus;
9339 if (onBattery) {
9340 mDischargeCurrentLevel = level;
9341 if (!mRecordingHistory) {
9342 mRecordingHistory = true;
9343 startRecordingHistory(elapsedRealtime, uptime, true);
9344 }
9345 } else if (level < 96) {
9346 if (!mRecordingHistory) {
9347 mRecordingHistory = true;
9348 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009349 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009350 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009351 mCurrentBatteryLevel = level;
9352 if (mDischargePlugLevel < 0) {
9353 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009354 }
Adam Lesinski926969b2016-04-28 17:31:12 -07009355
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009356 if (onBattery != mOnBattery) {
9357 mHistoryCur.batteryLevel = (byte)level;
9358 mHistoryCur.batteryStatus = (byte)status;
9359 mHistoryCur.batteryHealth = (byte)health;
9360 mHistoryCur.batteryPlugType = (byte)plugType;
9361 mHistoryCur.batteryTemperature = (short)temp;
9362 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009363 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009364 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
9365 } else {
9366 boolean changed = false;
9367 if (mHistoryCur.batteryLevel != level) {
9368 mHistoryCur.batteryLevel = (byte)level;
9369 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009370
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009371 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9372 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009373 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009374 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009375 if (mHistoryCur.batteryStatus != status) {
9376 mHistoryCur.batteryStatus = (byte)status;
9377 changed = true;
9378 }
9379 if (mHistoryCur.batteryHealth != health) {
9380 mHistoryCur.batteryHealth = (byte)health;
9381 changed = true;
9382 }
9383 if (mHistoryCur.batteryPlugType != plugType) {
9384 mHistoryCur.batteryPlugType = (byte)plugType;
9385 changed = true;
9386 }
9387 if (temp >= (mHistoryCur.batteryTemperature+10)
9388 || temp <= (mHistoryCur.batteryTemperature-10)) {
9389 mHistoryCur.batteryTemperature = (short)temp;
9390 changed = true;
9391 }
9392 if (volt > (mHistoryCur.batteryVoltage+20)
9393 || volt < (mHistoryCur.batteryVoltage-20)) {
9394 mHistoryCur.batteryVoltage = (char)volt;
9395 changed = true;
9396 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009397 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
9398 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
9399 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07009400 changed = true;
9401 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009402 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9403 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9404 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9405 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009406 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009407 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9408 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9409 modeBits, elapsedRealtime);
9410 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9411 modeBits, elapsedRealtime);
9412 mLastDischargeStepLevel = level;
9413 mMinDischargeStepLevel = level;
9414 mInitStepMode = mCurStepMode;
9415 mModStepMode = 0;
9416 }
9417 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009418 if (level >= 90) {
9419 // If the battery level is at least 90%, always consider the device to be
9420 // charging even if it happens to go down a level.
9421 changed |= setChargingLocked(true);
9422 mLastChargeStepLevel = level;
9423 } if (!mCharging) {
9424 if (mLastChargeStepLevel < level) {
9425 // We have not reporting that we are charging, but the level has now
9426 // gone up, so consider the state to be charging.
9427 changed |= setChargingLocked(true);
9428 mLastChargeStepLevel = level;
9429 }
9430 } else {
9431 if (mLastChargeStepLevel > level) {
9432 // We had reported that the device was charging, but here we are with
9433 // power connected and the level going down. Looks like the current
9434 // power supplied isn't enough, so consider the device to now be
9435 // discharging.
9436 changed |= setChargingLocked(false);
9437 mLastChargeStepLevel = level;
9438 }
9439 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009440 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9441 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9442 modeBits, elapsedRealtime);
9443 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9444 modeBits, elapsedRealtime);
9445 mLastChargeStepLevel = level;
9446 mMaxChargeStepLevel = level;
9447 mInitStepMode = mCurStepMode;
9448 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009449 }
9450 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009451 if (changed) {
9452 addHistoryRecordLocked(elapsedRealtime, uptime);
9453 }
Evan Millarc64edde2009-04-18 12:26:32 -07009454 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009455 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
9456 // We don't record history while we are plugged in and fully charged.
9457 // The next time we are unplugged, history will be cleared.
9458 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08009459 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009460 }
9461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 public long getAwakeTimeBattery() {
9463 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
9464 }
9465
9466 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -08009467 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009468 }
9469
9470 @Override
9471 public long computeUptime(long curTime, int which) {
9472 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009473 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009474 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009475 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 }
9477 return 0;
9478 }
9479
9480 @Override
9481 public long computeRealtime(long curTime, int which) {
9482 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009483 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009484 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009485 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486 }
9487 return 0;
9488 }
9489
9490 @Override
9491 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009492 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 }
9494
9495 @Override
9496 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009497 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 }
9499
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009500 @Override
9501 public long computeBatteryScreenOffUptime(long curTime, int which) {
9502 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
9503 }
9504
9505 @Override
9506 public long computeBatteryScreenOffRealtime(long curTime, int which) {
9507 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 }
9509
Dianne Hackborn260c5022014-04-29 11:23:16 -07009510 private long computeTimePerLevel(long[] steps, int numSteps) {
9511 // For now we'll do a simple average across all steps.
9512 if (numSteps <= 0) {
9513 return -1;
9514 }
9515 long total = 0;
9516 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009517 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009518 }
9519 return total / numSteps;
9520 /*
9521 long[] buckets = new long[numSteps];
9522 int numBuckets = 0;
9523 int numToAverage = 4;
9524 int i = 0;
9525 while (i < numSteps) {
9526 long totalTime = 0;
9527 int num = 0;
9528 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009529 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009530 num++;
9531 }
9532 buckets[numBuckets] = totalTime / num;
9533 numBuckets++;
9534 numToAverage *= 2;
9535 i += num;
9536 }
9537 if (numBuckets < 1) {
9538 return -1;
9539 }
9540 long averageTime = buckets[numBuckets-1];
9541 for (i=numBuckets-2; i>=0; i--) {
9542 averageTime = (averageTime + buckets[i]) / 2;
9543 }
9544 return averageTime;
9545 */
9546 }
9547
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009548 @Override
9549 public long computeBatteryTimeRemaining(long curTime) {
9550 if (!mOnBattery) {
9551 return -1;
9552 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009553 /* Simple implementation just looks at the average discharge per level across the
9554 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009555 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
9556 if (discharge < 2) {
9557 return -1;
9558 }
9559 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
9560 if (duration < 1000*1000) {
9561 return -1;
9562 }
9563 long usPerLevel = duration/discharge;
9564 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009565 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009566 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009567 return -1;
9568 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009569 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009570 if (msPerLevel <= 0) {
9571 return -1;
9572 }
9573 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009574 }
9575
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009576 @Override
9577 public LevelStepTracker getDischargeLevelStepTracker() {
9578 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009579 }
9580
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009581 @Override
9582 public LevelStepTracker getDailyDischargeLevelStepTracker() {
9583 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009584 }
9585
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009586 @Override
9587 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009588 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009589 // Not yet working.
9590 return -1;
9591 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009592 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009593 int curLevel = mCurrentBatteryLevel;
9594 int plugLevel = mDischargePlugLevel;
9595 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
9596 return -1;
9597 }
9598 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
9599 if (duration < 1000*1000) {
9600 return -1;
9601 }
9602 long usPerLevel = duration/(curLevel-plugLevel);
9603 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07009604 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009605 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009606 return -1;
9607 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009608 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009609 if (msPerLevel <= 0) {
9610 return -1;
9611 }
9612 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009613 }
9614
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009615 @Override
9616 public LevelStepTracker getChargeLevelStepTracker() {
9617 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009618 }
9619
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009620 @Override
9621 public LevelStepTracker getDailyChargeLevelStepTracker() {
9622 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009623 }
9624
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009625 @Override
9626 public ArrayList<PackageChange> getDailyPackageChanges() {
9627 return mDailyPackageChanges;
9628 }
9629
Joe Onoratoe1acd632016-02-23 13:25:10 -08009630 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009631 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 }
9633
9634 @Override
9635 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009636 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637 }
9638
9639 @Override
9640 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009641 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07009643
The Android Open Source Project10592532009-03-18 17:39:46 -07009644 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009645 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009646 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009647 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009648 }
9649 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009650
Evan Millar633a1742009-04-02 16:36:33 -07009651 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009652 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009653 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009654
The Android Open Source Project10592532009-03-18 17:39:46 -07009655 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009656 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009657 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009658 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009659 }
9660 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009661
Evan Millar633a1742009-04-02 16:36:33 -07009662 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009663 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009665
Amith Yamasanie43530a2009-08-21 13:11:37 -07009666 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009667 public int getLowDischargeAmountSinceCharge() {
9668 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009669 int val = mLowDischargeAmountSinceCharge;
9670 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9671 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
9672 }
9673 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009674 }
9675 }
9676
9677 @Override
9678 public int getHighDischargeAmountSinceCharge() {
9679 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009680 int val = mHighDischargeAmountSinceCharge;
9681 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9682 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
9683 }
9684 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009685 }
9686 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009687
9688 @Override
9689 public int getDischargeAmount(int which) {
9690 int dischargeAmount = which == STATS_SINCE_CHARGED
9691 ? getHighDischargeAmountSinceCharge()
9692 : (getDischargeStartLevel() - getDischargeCurrentLevel());
9693 if (dischargeAmount < 0) {
9694 dischargeAmount = 0;
9695 }
9696 return dischargeAmount;
9697 }
9698
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009699 public int getDischargeAmountScreenOn() {
9700 synchronized(this) {
9701 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009702 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009703 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9704 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9705 }
9706 return val;
9707 }
9708 }
9709
9710 public int getDischargeAmountScreenOnSinceCharge() {
9711 synchronized(this) {
9712 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009713 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009714 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9715 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9716 }
9717 return val;
9718 }
9719 }
9720
9721 public int getDischargeAmountScreenOff() {
9722 synchronized(this) {
9723 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009724 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009725 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9726 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9727 }
9728 return val;
9729 }
9730 }
9731
9732 public int getDischargeAmountScreenOffSinceCharge() {
9733 synchronized(this) {
9734 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009735 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009736 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9737 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9738 }
9739 return val;
9740 }
9741 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009743 /**
9744 * Retrieve the statistics object for a particular uid, creating if needed.
9745 */
9746 public Uid getUidStatsLocked(int uid) {
9747 Uid u = mUidStats.get(uid);
9748 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009749 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750 mUidStats.put(uid, u);
9751 }
9752 return u;
9753 }
9754
9755 /**
9756 * Remove the statistics object for a particular uid.
9757 */
9758 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07009759 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 mUidStats.remove(uid);
9761 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07009762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009763 /**
9764 * Retrieve the statistics object for a particular process, creating
9765 * if needed.
9766 */
9767 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009768 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 Uid u = getUidStatsLocked(uid);
9770 return u.getProcessStatsLocked(name);
9771 }
9772
9773 /**
9774 * Retrieve the statistics object for a particular process, creating
9775 * if needed.
9776 */
9777 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009778 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 Uid u = getUidStatsLocked(uid);
9780 return u.getPackageStatsLocked(pkg);
9781 }
9782
9783 /**
9784 * Retrieve the statistics object for a particular service, creating
9785 * if needed.
9786 */
9787 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009788 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009789 Uid u = getUidStatsLocked(uid);
9790 return u.getServiceStatsLocked(pkg, name);
9791 }
9792
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009793 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009794 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009795 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009796 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009797 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009798
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009799 Parcel mPendingWrite = null;
9800 final ReentrantLock mWriteLock = new ReentrantLock();
9801
9802 public void writeAsyncLocked() {
9803 writeLocked(false);
9804 }
9805
9806 public void writeSyncLocked() {
9807 writeLocked(true);
9808 }
9809
9810 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009811 if (mFile == null) {
9812 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 return;
9814 }
9815
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009816 if (mShuttingDown) {
9817 return;
9818 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009819
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009820 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009821 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -08009822 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009823
9824 if (mPendingWrite != null) {
9825 mPendingWrite.recycle();
9826 }
9827 mPendingWrite = out;
9828
9829 if (sync) {
9830 commitPendingDataToDisk();
9831 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009832 BackgroundThread.getHandler().post(new Runnable() {
9833 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009834 commitPendingDataToDisk();
9835 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009836 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009837 }
9838 }
9839
9840 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009841 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009842 synchronized (this) {
9843 next = mPendingWrite;
9844 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009845 if (next == null) {
9846 return;
9847 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009848
9849 mWriteLock.lock();
9850 }
9851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009853 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009854 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07009856 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009857 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009858 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009860 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009861 mFile.rollback();
9862 } finally {
9863 next.recycle();
9864 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07009865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 }
9867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009869 if (mDailyFile != null) {
9870 readDailyStatsLocked();
9871 }
9872
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009873 if (mFile == null) {
9874 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 return;
9876 }
9877
9878 mUidStats.clear();
9879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009881 File file = mFile.chooseForRead();
9882 if (!file.exists()) {
9883 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009885 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009887 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 Parcel in = Parcel.obtain();
9889 in.unmarshall(raw, 0, raw.length);
9890 in.setDataPosition(0);
9891 stream.close();
9892
9893 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08009894 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009895 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009896 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009898
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009899 mEndPlatformVersion = Build.ID;
9900
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009901 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009902 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -08009903 final long elapsedRealtime = mClocks.elapsedRealtime();
9904 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009905 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009906 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009907 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009908 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
9909 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009910 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009911
9912 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 }
9914
9915 public int describeContents() {
9916 return 0;
9917 }
9918
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009919 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -07009920 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009921
9922 mHistoryBuffer.setDataSize(0);
9923 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009924 mHistoryTagPool.clear();
9925 mNextHistoryTagIdx = 0;
9926 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009927
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009928 int numTags = in.readInt();
9929 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009930 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009931 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009932 if (str == null) {
9933 throw new ParcelFormatException("null history tag string");
9934 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009935 int uid = in.readInt();
9936 HistoryTag tag = new HistoryTag();
9937 tag.string = str;
9938 tag.uid = uid;
9939 tag.poolIdx = idx;
9940 mHistoryTagPool.put(tag, idx);
9941 if (idx >= mNextHistoryTagIdx) {
9942 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009943 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009944 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009945 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009946
9947 int bufSize = in.readInt();
9948 int curPos = in.dataPosition();
9949 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009950 throw new ParcelFormatException("File corrupt: history data buffer too large " +
9951 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009952 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009953 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
9954 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009955 } else {
9956 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
9957 + " bytes at " + curPos);
9958 mHistoryBuffer.appendFrom(in, curPos, bufSize);
9959 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009960 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009961
Dianne Hackbornae384452011-06-28 12:33:48 -07009962 if (andOldHistory) {
9963 readOldHistory(in);
9964 }
9965
9966 if (DEBUG_HISTORY) {
9967 StringBuilder sb = new StringBuilder(128);
9968 sb.append("****************** OLD mHistoryBaseTime: ");
9969 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9970 Slog.i(TAG, sb.toString());
9971 }
9972 mHistoryBaseTime = historyBaseTime;
9973 if (DEBUG_HISTORY) {
9974 StringBuilder sb = new StringBuilder(128);
9975 sb.append("****************** NEW mHistoryBaseTime: ");
9976 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9977 Slog.i(TAG, sb.toString());
9978 }
9979
9980 // We are just arbitrarily going to insert 1 minute from the sample of
9981 // the last run until samples in this run.
9982 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08009983 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009984 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07009985 if (DEBUG_HISTORY) {
9986 StringBuilder sb = new StringBuilder(128);
9987 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
9988 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9989 Slog.i(TAG, sb.toString());
9990 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07009991 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009992 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009993
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009994 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009995 if (!USE_OLD_HISTORY) {
9996 return;
9997 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009998 mHistory = mHistoryEnd = mHistoryCache = null;
9999 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010000 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010001 HistoryItem rec = new HistoryItem(time, in);
10002 addHistoryRecordLocked(rec);
10003 }
10004 }
10005
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010006 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010007 if (DEBUG_HISTORY) {
10008 StringBuilder sb = new StringBuilder(128);
10009 sb.append("****************** WRITING mHistoryBaseTime: ");
10010 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010011 sb.append(" mLastHistoryElapsedRealtime: ");
10012 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010013 Slog.i(TAG, sb.toString());
10014 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010015 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010016 if (!inclData) {
10017 out.writeInt(0);
10018 out.writeInt(0);
10019 return;
10020 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010021 out.writeInt(mHistoryTagPool.size());
10022 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10023 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010024 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010025 out.writeString(tag.string);
10026 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010027 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010028 out.writeInt(mHistoryBuffer.dataSize());
10029 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10030 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10031 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010032
10033 if (andOldHistory) {
10034 writeOldHistory(out);
10035 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010036 }
10037
10038 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010039 if (!USE_OLD_HISTORY) {
10040 return;
10041 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010042 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010043 while (rec != null) {
10044 if (rec.time >= 0) rec.writeToParcel(out, 0);
10045 rec = rec.next;
10046 }
10047 out.writeLong(-1);
10048 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010049
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010050 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051 final int version = in.readInt();
10052 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010053 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 + ", expected " + VERSION + "; erasing old stats");
10055 return;
10056 }
10057
Dianne Hackbornae384452011-06-28 12:33:48 -070010058 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010061 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010063 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010064 mStartPlatformVersion = in.readString();
10065 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010066 mOnBatteryTimeBase.readSummaryFromParcel(in);
10067 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010068 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010069 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010070 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010071 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010072 mLowDischargeAmountSinceCharge = in.readInt();
10073 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010074 mDischargeAmountScreenOnSinceCharge = in.readInt();
10075 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010076 mDischargeStepTracker.readFromParcel(in);
10077 mChargeStepTracker.readFromParcel(in);
10078 mDailyDischargeStepTracker.readFromParcel(in);
10079 mDailyChargeStepTracker.readFromParcel(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010080 int NPKG = in.readInt();
10081 if (NPKG > 0) {
10082 mDailyPackageChanges = new ArrayList<>(NPKG);
10083 while (NPKG > 0) {
10084 NPKG--;
10085 PackageChange pc = new PackageChange();
10086 pc.mPackageName = in.readString();
10087 pc.mUpdate = in.readInt() != 0;
10088 pc.mVersionCode = in.readInt();
10089 mDailyPackageChanges.add(pc);
10090 }
10091 } else {
10092 mDailyPackageChanges = null;
10093 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010094 mDailyStartTime = in.readLong();
10095 mNextMinDailyDeadline = in.readLong();
10096 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010099
Jeff Browne95c3cd2014-05-02 16:59:26 -070010100 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010102 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10103 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10104 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010105 mInteractive = false;
10106 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010108 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010109 mLongestLightIdleTime = in.readLong();
10110 mLongestFullIdleTime = in.readLong();
10111 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10112 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10113 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010114 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010115 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010116 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010117 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10118 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010119 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010120 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10121 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10122 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010123 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010124 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10125 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010126 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010127 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010128 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010129 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010130 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010131 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10132 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010133 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010134 mWifiOn = false;
10135 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010136 mGlobalWifiRunning = false;
10137 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010138 for (int i=0; i<NUM_WIFI_STATES; i++) {
10139 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10140 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010141 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10142 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10143 }
10144 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10145 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10146 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010147 mWifiActivity.readSummaryFromParcel(in);
10148 mBluetoothActivity.readSummaryFromParcel(in);
10149 mModemActivity.readSummaryFromParcel(in);
10150 mHasWifiReporting = in.readInt() != 0;
10151 mHasBluetoothReporting = in.readInt() != 0;
10152 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010153
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010154 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010155 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010156 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010157 mCameraOnNesting = 0;
10158 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010159 mBluetoothScanNesting = 0;
10160 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161
Evan Millarc64edde2009-04-18 12:26:32 -070010162 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010163 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010164 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010165 }
Evan Millarc64edde2009-04-18 12:26:32 -070010166 for (int ikw = 0; ikw < NKW; ikw++) {
10167 if (in.readInt() != 0) {
10168 String kwltName = in.readString();
10169 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10170 }
10171 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010172
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010173 int NWR = in.readInt();
10174 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010175 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010176 }
10177 for (int iwr = 0; iwr < NWR; iwr++) {
10178 if (in.readInt() != 0) {
10179 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010180 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010181 }
10182 }
10183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010184 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010185 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010186 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 for (int iu = 0; iu < NU; iu++) {
10189 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010190 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 mUidStats.put(uid, u);
10192
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010193 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010194 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010195 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010196 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010197 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010198 if (in.readInt() != 0) {
10199 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10200 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010201 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010202 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010203 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010204 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010205 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10206 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10207 if (in.readInt() != 0) {
10208 u.makeWifiBatchedScanBin(i, null);
10209 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10210 }
10211 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010212 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010213 if (in.readInt() != 0) {
10214 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10215 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010216 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010217 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010218 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010219 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010220 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10221 }
10222 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010223 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10224 }
10225 if (in.readInt() != 0) {
10226 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10227 }
10228 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010229 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10230 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010231 if (in.readInt() != 0) {
10232 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10233 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010234 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010235 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10236 if (in.readInt() != 0) {
10237 u.makeProcessState(i, null);
10238 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10239 }
10240 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010241 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010242 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010243 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010244
Dianne Hackborn617f8772009-03-31 15:04:46 -070010245 if (in.readInt() != 0) {
10246 if (u.mUserActivityCounters == null) {
10247 u.initUserActivityLocked();
10248 }
10249 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10250 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10251 }
10252 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010253
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010254 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010255 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010256 u.initNetworkActivityLocked();
10257 }
10258 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010259 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10260 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010261 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010262 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10263 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010264 }
10265
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010266 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10267 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010268 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010269
Adam Lesinski6832f392015-09-05 18:05:40 -070010270 if (in.readInt() != 0) {
10271 final int numClusters = in.readInt();
10272 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10273 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010274 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010275
10276 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10277 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010278 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010279 final int NSB = in.readInt();
10280 if (mPowerProfile != null &&
10281 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10282 throw new ParcelFormatException("File corrupt: too many speed bins " +
10283 NSB);
10284 }
10285
Adam Lesinski6832f392015-09-05 18:05:40 -070010286 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10287 for (int speed = 0; speed < NSB; speed++) {
10288 if (in.readInt() != 0) {
10289 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10290 mOnBatteryTimeBase);
10291 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10292 }
10293 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010294 } else {
10295 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010296 }
10297 }
10298 } else {
10299 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010300 }
10301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010303 if (NW > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010304 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 for (int iw = 0; iw < NW; iw++) {
10307 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010308 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 }
10310
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010311 int NS = in.readInt();
10312 if (NS > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010313 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010314 }
10315 for (int is = 0; is < NS; is++) {
10316 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010317 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010318 }
10319
10320 int NJ = in.readInt();
10321 if (NJ > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010322 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010323 }
10324 for (int ij = 0; ij < NJ; ij++) {
10325 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010326 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010327 }
10328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010330 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010331 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 for (int is = 0; is < NP; is++) {
10334 int seNumber = in.readInt();
10335 if (in.readInt() != 0) {
10336 u.getSensorTimerLocked(seNumber, true)
10337 .readSummaryFromParcelLocked(in);
10338 }
10339 }
10340
10341 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010342 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010343 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 for (int ip = 0; ip < NP; ip++) {
10346 String procName = in.readString();
10347 Uid.Proc p = u.getProcessStatsLocked(procName);
10348 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010349 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010350 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010352 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10353 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010354 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 }
10356
10357 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010358 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010359 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 for (int ip = 0; ip < NP; ip++) {
10362 String pkgName = in.readString();
10363 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010364 final int NWA = in.readInt();
10365 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010366 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010367 }
10368 p.mWakeupAlarms.clear();
10369 for (int iwa=0; iwa<NWA; iwa++) {
10370 String tag = in.readString();
10371 Counter c = new Counter(mOnBatteryTimeBase);
10372 c.readSummaryFromParcelLocked(in);
10373 p.mWakeupAlarms.put(tag, c);
10374 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010375 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010376 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010377 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 for (int is = 0; is < NS; is++) {
10380 String servName = in.readString();
10381 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10382 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010384 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 }
10386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 }
10388 }
10389
10390 /**
10391 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10392 * disk. This format does not allow a lossless round-trip.
10393 *
10394 * @param out the Parcel to be written to.
10395 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010396 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010397 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010398
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010399 // Pull the clock time. This may update the time and make a new history entry
10400 // if we had originally pulled a time before the RTC was set.
10401 long startClockTime = getStartClockTime();
10402
Joe Onoratoabded112016-02-08 16:49:39 -080010403 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10404 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405
10406 out.writeInt(VERSION);
10407
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010408 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010411 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010412 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010413 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010414 out.writeString(mStartPlatformVersion);
10415 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010416 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
10417 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010418 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010419 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010420 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010421 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -080010422 out.writeInt(getLowDischargeAmountSinceCharge());
10423 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010424 out.writeInt(getDischargeAmountScreenOnSinceCharge());
10425 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010426 mDischargeStepTracker.writeToParcel(out);
10427 mChargeStepTracker.writeToParcel(out);
10428 mDailyDischargeStepTracker.writeToParcel(out);
10429 mDailyChargeStepTracker.writeToParcel(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010430 if (mDailyPackageChanges != null) {
10431 final int NPKG = mDailyPackageChanges.size();
10432 out.writeInt(NPKG);
10433 for (int i=0; i<NPKG; i++) {
10434 PackageChange pc = mDailyPackageChanges.get(i);
10435 out.writeString(pc.mPackageName);
10436 out.writeInt(pc.mUpdate ? 1 : 0);
10437 out.writeInt(pc.mVersionCode);
10438 }
10439 } else {
10440 out.writeInt(0);
10441 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010442 out.writeLong(mDailyStartTime);
10443 out.writeLong(mNextMinDailyDeadline);
10444 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010445
10446 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010447 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010448 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010449 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010450 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010451 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010452 out.writeLong(mLongestLightIdleTime);
10453 out.writeLong(mLongestFullIdleTime);
10454 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10455 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10456 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010457 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010458 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080010459 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010460 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010461 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010462 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010463 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010464 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010465 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010466 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010467 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10468 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010469 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010470 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10471 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010472 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010473 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
10474 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010475 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10476 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010477 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010478 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010479 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010480 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10481 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10482 }
10483 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10484 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10485 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010486 mWifiActivity.writeSummaryToParcel(out);
10487 mBluetoothActivity.writeSummaryToParcel(out);
10488 mModemActivity.writeSummaryToParcel(out);
10489 out.writeInt(mHasWifiReporting ? 1 : 0);
10490 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10491 out.writeInt(mHasModemReporting ? 1 : 0);
10492
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010493 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010494 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010495 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010496 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497
Evan Millarc64edde2009-04-18 12:26:32 -070010498 out.writeInt(mKernelWakelockStats.size());
10499 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10500 Timer kwlt = ent.getValue();
10501 if (kwlt != null) {
10502 out.writeInt(1);
10503 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010504 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10505 } else {
10506 out.writeInt(0);
10507 }
10508 }
10509
10510 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010511 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10512 SamplingTimer timer = ent.getValue();
10513 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010514 out.writeInt(1);
10515 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010516 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070010517 } else {
10518 out.writeInt(0);
10519 }
10520 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 final int NU = mUidStats.size();
10523 out.writeInt(NU);
10524 for (int iu = 0; iu < NU; iu++) {
10525 out.writeInt(mUidStats.keyAt(iu));
10526 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010527
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010528 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010529 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010530 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010531 } else {
10532 out.writeInt(0);
10533 }
10534 if (u.mFullWifiLockTimer != null) {
10535 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010536 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010537 } else {
10538 out.writeInt(0);
10539 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010540 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010541 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010542 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010543 } else {
10544 out.writeInt(0);
10545 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010546 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10547 if (u.mWifiBatchedScanTimer[i] != null) {
10548 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010549 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070010550 } else {
10551 out.writeInt(0);
10552 }
10553 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010554 if (u.mWifiMulticastTimer != null) {
10555 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010556 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010557 } else {
10558 out.writeInt(0);
10559 }
10560 if (u.mAudioTurnedOnTimer != null) {
10561 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010562 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010563 } else {
10564 out.writeInt(0);
10565 }
10566 if (u.mVideoTurnedOnTimer != null) {
10567 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010568 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010569 } else {
10570 out.writeInt(0);
10571 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010572 if (u.mFlashlightTurnedOnTimer != null) {
10573 out.writeInt(1);
10574 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10575 } else {
10576 out.writeInt(0);
10577 }
10578 if (u.mCameraTurnedOnTimer != null) {
10579 out.writeInt(1);
10580 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10581 } else {
10582 out.writeInt(0);
10583 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010584 if (u.mForegroundActivityTimer != null) {
10585 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010586 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010587 } else {
10588 out.writeInt(0);
10589 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010590 if (u.mBluetoothScanTimer != null) {
10591 out.writeInt(1);
10592 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10593 } else {
10594 out.writeInt(0);
10595 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070010596 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10597 if (u.mProcessStateTimer[i] != null) {
10598 out.writeInt(1);
10599 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10600 } else {
10601 out.writeInt(0);
10602 }
10603 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010604 if (u.mVibratorOnTimer != null) {
10605 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010606 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010607 } else {
10608 out.writeInt(0);
10609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610
Dianne Hackborn617f8772009-03-31 15:04:46 -070010611 if (u.mUserActivityCounters == null) {
10612 out.writeInt(0);
10613 } else {
10614 out.writeInt(1);
10615 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10616 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
10617 }
10618 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010619
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010620 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010621 out.writeInt(0);
10622 } else {
10623 out.writeInt(1);
10624 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010625 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10626 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010627 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010628 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
10629 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010630 }
10631
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010632 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
10633 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010634 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010635
Adam Lesinski6832f392015-09-05 18:05:40 -070010636 if (u.mCpuClusterSpeed != null) {
10637 out.writeInt(1);
10638 out.writeInt(u.mCpuClusterSpeed.length);
10639 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
10640 if (cpuSpeeds != null) {
10641 out.writeInt(1);
10642 out.writeInt(cpuSpeeds.length);
10643 for (LongSamplingCounter c : cpuSpeeds) {
10644 if (c != null) {
10645 out.writeInt(1);
10646 c.writeSummaryFromParcelLocked(out);
10647 } else {
10648 out.writeInt(0);
10649 }
10650 }
10651 } else {
10652 out.writeInt(0);
10653 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010654 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010655 } else {
10656 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010657 }
10658
Dianne Hackbornd953c532014-08-16 18:17:38 -070010659 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
10660 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010662 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010663 out.writeString(wakeStats.keyAt(iw));
10664 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010665 if (wl.mTimerFull != null) {
10666 out.writeInt(1);
10667 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10668 } else {
10669 out.writeInt(0);
10670 }
10671 if (wl.mTimerPartial != null) {
10672 out.writeInt(1);
10673 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10674 } else {
10675 out.writeInt(0);
10676 }
10677 if (wl.mTimerWindow != null) {
10678 out.writeInt(1);
10679 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10680 } else {
10681 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010683 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070010684 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010685 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070010686 } else {
10687 out.writeInt(0);
10688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 }
10690
Dianne Hackbornd953c532014-08-16 18:17:38 -070010691 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
10692 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010693 out.writeInt(NS);
10694 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010695 out.writeString(syncStats.keyAt(is));
10696 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010697 }
10698
Dianne Hackbornd953c532014-08-16 18:17:38 -070010699 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
10700 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010701 out.writeInt(NJ);
10702 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010703 out.writeString(jobStats.keyAt(ij));
10704 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010705 }
10706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 int NSE = u.mSensorStats.size();
10708 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010709 for (int ise=0; ise<NSE; ise++) {
10710 out.writeInt(u.mSensorStats.keyAt(ise));
10711 Uid.Sensor se = u.mSensorStats.valueAt(ise);
10712 if (se.mTimer != null) {
10713 out.writeInt(1);
10714 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10715 } else {
10716 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 }
10718 }
10719
10720 int NP = u.mProcessStats.size();
10721 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010722 for (int ip=0; ip<NP; ip++) {
10723 out.writeString(u.mProcessStats.keyAt(ip));
10724 Uid.Proc ps = u.mProcessStats.valueAt(ip);
10725 out.writeLong(ps.mUserTime);
10726 out.writeLong(ps.mSystemTime);
10727 out.writeLong(ps.mForegroundTime);
10728 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010729 out.writeInt(ps.mNumCrashes);
10730 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010731 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 }
10733
10734 NP = u.mPackageStats.size();
10735 out.writeInt(NP);
10736 if (NP > 0) {
10737 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
10738 : u.mPackageStats.entrySet()) {
10739 out.writeString(ent.getKey());
10740 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010741 final int NWA = ps.mWakeupAlarms.size();
10742 out.writeInt(NWA);
10743 for (int iwa=0; iwa<NWA; iwa++) {
10744 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
10745 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
10746 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010747 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010749 for (int is=0; is<NS; is++) {
10750 out.writeString(ps.mServiceStats.keyAt(is));
10751 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
10752 long time = ss.getStartTimeToNowLocked(
10753 mOnBatteryTimeBase.getUptime(NOW_SYS));
10754 out.writeLong(time);
10755 out.writeInt(ss.mStarts);
10756 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 }
10758 }
10759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760 }
10761 }
10762
10763 public void readFromParcel(Parcel in) {
10764 readFromParcelLocked(in);
10765 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010767 void readFromParcelLocked(Parcel in) {
10768 int magic = in.readInt();
10769 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010770 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 }
10772
Dianne Hackbornae384452011-06-28 12:33:48 -070010773 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010776 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010777 mStartPlatformVersion = in.readString();
10778 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779 mUptime = in.readLong();
10780 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010781 mRealtime = in.readLong();
10782 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 mOnBattery = in.readInt() != 0;
10784 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010785 mOnBatteryTimeBase.readFromParcel(in);
10786 mOnBatteryScreenOffTimeBase.readFromParcel(in);
10787
Jeff Browne95c3cd2014-05-02 16:59:26 -070010788 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080010789 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010790 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010791 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
10792 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010793 }
Dianne Hackborn29325132014-05-21 15:01:03 -070010794 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010795 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010796 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010797 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
10798 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010799 mLongestLightIdleTime = in.readLong();
10800 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080010801 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
10802 mOnBatteryTimeBase, in);
10803 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
10804 mOnBatteryTimeBase, in);
10805 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
10806 mOnBatteryTimeBase, in);
10807 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
10808 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010809 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010810 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010811 null, mOnBatteryTimeBase, in);
10812 }
Joe Onoratoabded112016-02-08 16:49:39 -080010813 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
10814 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010815 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010816 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010817 null, mOnBatteryTimeBase, in);
10818 }
10819 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10820 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10821 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10822 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010823 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080010824 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
10825 mOnBatteryTimeBase, in);
10826 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
10827 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010828 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010829 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
10830 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010831 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010832 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010833 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010834 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010835 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
10836 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010837 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010838 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010839 null, mOnBatteryTimeBase, in);
10840 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010841 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010842 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010843 null, mOnBatteryTimeBase, in);
10844 }
10845 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010846 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010847 null, mOnBatteryTimeBase, in);
10848 }
Adam Lesinski33dac552015-03-09 15:24:48 -070010849
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010850 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10851 NUM_WIFI_TX_LEVELS, in);
10852 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10853 NUM_BT_TX_LEVELS, in);
10854 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10855 ModemActivityInfo.TX_POWER_LEVELS, in);
10856 mHasWifiReporting = in.readInt() != 0;
10857 mHasBluetoothReporting = in.readInt() != 0;
10858 mHasModemReporting = in.readInt() != 0;
10859
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010860 mNumConnectivityChange = in.readInt();
10861 mLoadedNumConnectivityChange = in.readInt();
10862 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010863 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010864 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010865 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010866 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010867 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010868 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010869 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010870 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010871 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010872 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010873 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010874 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010875 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010876 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010877 mLowDischargeAmountSinceCharge = in.readInt();
10878 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010879 mDischargeAmountScreenOn = in.readInt();
10880 mDischargeAmountScreenOnSinceCharge = in.readInt();
10881 mDischargeAmountScreenOff = in.readInt();
10882 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010883 mDischargeStepTracker.readFromParcel(in);
10884 mChargeStepTracker.readFromParcel(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 mLastWriteTime = in.readLong();
10886
Evan Millarc64edde2009-04-18 12:26:32 -070010887 mKernelWakelockStats.clear();
10888 int NKW = in.readInt();
10889 for (int ikw = 0; ikw < NKW; ikw++) {
10890 if (in.readInt() != 0) {
10891 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010892 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070010893 mKernelWakelockStats.put(wakelockName, kwlt);
10894 }
10895 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010896
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010897 mWakeupReasonStats.clear();
10898 int NWR = in.readInt();
10899 for (int iwr = 0; iwr < NWR; iwr++) {
10900 if (in.readInt() != 0) {
10901 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010902 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010903 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010904 }
10905 }
10906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 mPartialTimers.clear();
10908 mFullTimers.clear();
10909 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010910 mWifiRunningTimers.clear();
10911 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070010912 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070010913 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010914 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010915 mAudioTurnedOnTimers.clear();
10916 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010917 mFlashlightTurnedOnTimers.clear();
10918 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919
10920 int numUids = in.readInt();
10921 mUidStats.clear();
10922 for (int i = 0; i < numUids; i++) {
10923 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010924 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010925 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010926 mUidStats.append(uid, u);
10927 }
10928 }
10929
10930 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010931 writeToParcelLocked(out, true, flags);
10932 }
10933
10934 public void writeToParcelWithoutUids(Parcel out, int flags) {
10935 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010937
10938 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010939 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010940 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010941 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010942
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010943 // Pull the clock time. This may update the time and make a new history entry
10944 // if we had originally pulled a time before the RTC was set.
10945 long startClockTime = getStartClockTime();
10946
Joe Onoratoabded112016-02-08 16:49:39 -080010947 final long uSecUptime = mClocks.uptimeMillis() * 1000;
10948 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010949 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
10950 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010953
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010954 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010957 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010958 out.writeString(mStartPlatformVersion);
10959 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 out.writeLong(mUptime);
10961 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 out.writeLong(mRealtime);
10963 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010964 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010965 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10966 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10967
10968 mScreenOnTimer.writeToParcel(out, uSecRealtime);
10969 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10970 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
10971 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010972 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010973 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010974 out.writeLong(mLongestLightIdleTime);
10975 out.writeLong(mLongestFullIdleTime);
10976 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
10977 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
10978 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010979 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010980 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
10981 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
10982 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
10983 }
10984 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
10985 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10986 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
10987 }
10988 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10989 mNetworkByteActivityCounters[i].writeToParcel(out);
10990 mNetworkPacketActivityCounters[i].writeToParcel(out);
10991 }
10992 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
10993 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010994 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010995 mMobileRadioActiveUnknownTime.writeToParcel(out);
10996 mMobileRadioActiveUnknownCount.writeToParcel(out);
10997 mWifiOnTimer.writeToParcel(out, uSecRealtime);
10998 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
10999 for (int i=0; i<NUM_WIFI_STATES; i++) {
11000 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11001 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011002 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11003 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11004 }
11005 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11006 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11007 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011008 mWifiActivity.writeToParcel(out, 0);
11009 mBluetoothActivity.writeToParcel(out, 0);
11010 mModemActivity.writeToParcel(out, 0);
11011 out.writeInt(mHasWifiReporting ? 1 : 0);
11012 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11013 out.writeInt(mHasModemReporting ? 1 : 0);
11014
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011015 out.writeInt(mNumConnectivityChange);
11016 out.writeInt(mLoadedNumConnectivityChange);
11017 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011018 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011019 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011020 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011021 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011022 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011023 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011024 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011025 out.writeInt(mLowDischargeAmountSinceCharge);
11026 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011027 out.writeInt(mDischargeAmountScreenOn);
11028 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11029 out.writeInt(mDischargeAmountScreenOff);
11030 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011031 mDischargeStepTracker.writeToParcel(out);
11032 mChargeStepTracker.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011033 out.writeLong(mLastWriteTime);
11034
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011035 if (inclUids) {
11036 out.writeInt(mKernelWakelockStats.size());
11037 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11038 SamplingTimer kwlt = ent.getValue();
11039 if (kwlt != null) {
11040 out.writeInt(1);
11041 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011042 kwlt.writeToParcel(out, uSecRealtime);
11043 } else {
11044 out.writeInt(0);
11045 }
11046 }
11047 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011048 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11049 SamplingTimer timer = ent.getValue();
11050 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011051 out.writeInt(1);
11052 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011053 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011054 } else {
11055 out.writeInt(0);
11056 }
Evan Millarc64edde2009-04-18 12:26:32 -070011057 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011058 } else {
11059 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011060 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011061
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011062 if (inclUids) {
11063 int size = mUidStats.size();
11064 out.writeInt(size);
11065 for (int i = 0; i < size; i++) {
11066 out.writeInt(mUidStats.keyAt(i));
11067 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011068
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011069 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011070 }
11071 } else {
11072 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011073 }
11074 }
11075
11076 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11077 new Parcelable.Creator<BatteryStatsImpl>() {
11078 public BatteryStatsImpl createFromParcel(Parcel in) {
11079 return new BatteryStatsImpl(in);
11080 }
11081
11082 public BatteryStatsImpl[] newArray(int size) {
11083 return new BatteryStatsImpl[size];
11084 }
11085 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011086
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011087 public void prepareForDumpLocked() {
11088 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011089 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011090
11091 // Pull the clock time. This may update the time and make a new history entry
11092 // if we had originally pulled a time before the RTC was set.
11093 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011094 }
11095
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011096 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011098 pw.println("mOnBatteryTimeBase:");
11099 mOnBatteryTimeBase.dump(pw, " ");
11100 pw.println("mOnBatteryScreenOffTimeBase:");
11101 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011102 Printer pr = new PrintWriterPrinter(pw);
11103 pr.println("*** Screen timer:");
11104 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011105 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011106 pr.println("*** Screen brightness #" + i + ":");
11107 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011108 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011109 pr.println("*** Interactive timer:");
11110 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011111 pr.println("*** Power save mode timer:");
11112 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011113 pr.println("*** Device idle mode light timer:");
11114 mDeviceIdleModeLightTimer.logState(pr, " ");
11115 pr.println("*** Device idle mode full timer:");
11116 mDeviceIdleModeFullTimer.logState(pr, " ");
11117 pr.println("*** Device light idling timer:");
11118 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011119 pr.println("*** Device idling timer:");
11120 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011121 pr.println("*** Phone timer:");
11122 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011123 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011124 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011125 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011126 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011127 pr.println("*** Signal scanning :");
11128 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011129 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011130 pr.println("*** Data connection type #" + i + ":");
11131 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011132 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011133 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011134 pr.println("*** Mobile network active timer:");
11135 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011136 pr.println("*** Mobile network active adjusted timer:");
11137 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011138 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011139 pr.println("*** Wifi timer:");
11140 mWifiOnTimer.logState(pr, " ");
11141 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011142 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011143 for (int i=0; i<NUM_WIFI_STATES; i++) {
11144 pr.println("*** Wifi state #" + i + ":");
11145 mWifiStateTimer[i].logState(pr, " ");
11146 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011147 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11148 pr.println("*** Wifi suppl state #" + i + ":");
11149 mWifiSupplStateTimer[i].logState(pr, " ");
11150 }
11151 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11152 pr.println("*** Wifi signal strength #" + i + ":");
11153 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11154 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011155 pr.println("*** Flashlight timer:");
11156 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011157 pr.println("*** Camera timer:");
11158 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011160 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162}