blob: b174e33251682520ac889457f5ee80230ebbb6bf [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.os;
18
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070019import android.annotation.Nullable;
Dianne Hackborn61659e52014-07-09 16:13:01 -070020import android.app.ActivityManager;
Adam Lesinski33dac552015-03-09 15:24:48 -070021import android.bluetooth.BluetoothActivityEnergyInfo;
Adam Lesinski50e47602015-12-04 17:04:54 -080022import android.bluetooth.UidTraffic;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080023import android.content.Context;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070024import android.content.Intent;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070025import android.net.ConnectivityManager;
26import android.net.NetworkStats;
Adam Lesinski33dac552015-03-09 15:24:48 -070027import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn3251b902014-06-20 14:40:53 -070028import android.net.wifi.WifiManager;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070029import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.BatteryStats;
Dianne Hackborncd0e3352014-08-07 17:08:09 -070031import android.os.Build;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070032import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070033import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080034import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070035import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Parcel;
37import android.os.ParcelFormatException;
38import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070039import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.SystemClock;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080041import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070042import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070043import android.telephony.DataConnectionRealTimeInfo;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080044import android.telephony.ModemActivityInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070045import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070046import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070047import android.telephony.TelephonyManager;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070048import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070049import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070051import android.util.LogWriter;
Dianne Hackbornd953c532014-08-16 18:17:38 -070052import android.util.MutableInt;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070053import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070055import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080057import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070058import android.util.SparseLongArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070059import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080060import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070061import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Adam Lesinski98f0d462016-04-19 16:46:20 -070063import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070064import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080065import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070066import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080067import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070068import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080069import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070070import com.android.server.NetworkManagementSocketTagger;
71import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080072import org.xmlpull.v1.XmlPullParser;
73import org.xmlpull.v1.XmlPullParserException;
74import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070075
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080076import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.File;
78import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080079import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import java.io.FileOutputStream;
81import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070082import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010083import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080085import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070087import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070089import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070090import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * All information we are collecting about things that can happen that impact
94 * battery life. All times are represented in microseconds except where indicated
95 * otherwise.
96 */
Joe Onoratoabded112016-02-08 16:49:39 -080097public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080099 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700100 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800101 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700102 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700103 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700104
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700105 // TODO: remove "tcp" from network methods, since we measure total stats.
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700108 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 // Current on-disk Parcel version
Adam Lesinski5f056f62016-07-14 16:56:08 -0700111 private static final int VERSION = 148 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700112
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700113 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700114 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700115
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700116 // No, really, THIS is the maximum number of items we will record in the history.
117 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
118
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800119 // The maximum number of names wakelocks we will keep track of
120 // per uid; once the limit is reached, we batch the remaining wakelocks
121 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700122 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700123
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800124 // Number of transmit power states the Wifi controller can be in.
125 private static final int NUM_WIFI_TX_LEVELS = 1;
126
127 // Number of transmit power states the Bluetooth controller can be in.
128 private static final int NUM_BT_TX_LEVELS = 1;
129
Joe Onoratoabded112016-02-08 16:49:39 -0800130 protected Clocks mClocks;
131
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700132 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700133 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800134 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700136 static final int MSG_UPDATE_WAKELOCKS = 1;
137 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700138 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700139 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700140
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700141 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
142 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
143
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700144 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700145 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700146
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700147 public interface BatteryCallback {
148 public void batteryNeedsCpuUpdate();
149 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700150 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700151 }
152
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700153 public interface PlatformIdleStateCallback {
154 public String getPlatformLowPowerStats();
155 }
156
157 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
158
159
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700160 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800161 public MyHandler(Looper looper) {
162 super(looper, null, true);
163 }
164
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700165 @Override
166 public void handleMessage(Message msg) {
167 BatteryCallback cb = mCallback;
168 switch (msg.what) {
169 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700170 synchronized (BatteryStatsImpl.this) {
171 updateCpuTimeLocked();
172 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700173 if (cb != null) {
174 cb.batteryNeedsCpuUpdate();
175 }
176 break;
177 case MSG_REPORT_POWER_CHANGE:
178 if (cb != null) {
179 cb.batteryPowerChanged(msg.arg1 != 0);
180 }
181 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700182 case MSG_REPORT_CHARGING:
183 if (cb != null) {
184 final String action;
185 synchronized (BatteryStatsImpl.this) {
186 action = mCharging ? BatteryManager.ACTION_CHARGING
187 : BatteryManager.ACTION_DISCHARGING;
188 }
189 Intent intent = new Intent(action);
190 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
191 cb.batterySendBroadcast(intent);
192 }
193 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700194 }
195 }
196 }
197
Joe Onoratoabded112016-02-08 16:49:39 -0800198 public interface Clocks {
199 public long elapsedRealtime();
200 public long uptimeMillis();
201 }
202
203 public static class SystemClocks implements Clocks {
204 public long elapsedRealtime() {
205 return SystemClock.elapsedRealtime();
206 }
207
208 public long uptimeMillis() {
209 return SystemClock.uptimeMillis();
210 }
211 }
212
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700213 public interface ExternalStatsSync {
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800214 public static final int UPDATE_CPU = 0x01;
215 public static final int UPDATE_WIFI = 0x02;
216 public static final int UPDATE_RADIO = 0x04;
217 public static final int UPDATE_BT = 0x08;
218 public static final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
219
220 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700221 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700222 }
223
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700224 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700225 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700226
227 private BatteryCallback mCallback;
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800230 * Mapping isolated uids to the actual owning app uid.
231 */
232 final SparseIntArray mIsolatedUids = new SparseIntArray();
233
234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * The statistics we have collected organized by uids.
236 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800237 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 // A set of pools of currently active timers. When a timer is queried, we will divide the
240 // elapsed time by the number of active timers to arrive at that timer's share of the time.
241 // In order to do this, we must refresh each timer whenever the number of active timers
242 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700243 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
244 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
245 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700246 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700247 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
248 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
249 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
250 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
251 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
252 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
253 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
254 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700255 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
256 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800257 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700259 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700260 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 // These are the objects that will want to do something when the device
263 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800264 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800265
266 // These are the objects that will want to do something when the device
267 // is unplugged from power *and* the screen is off.
268 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
269
270 // Set to true when we want to distribute CPU across wakelocks for the next
271 // CPU update, even if we aren't currently running wake locks.
272 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700273
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700274 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700275
Dianne Hackborn37de0982014-05-09 09:32:18 -0700276 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800277
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700278 long mHistoryBaseTime;
279 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700280 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700281 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700282
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700283 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
284 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700285 final Parcel mHistoryBuffer = Parcel.obtain();
286 final HistoryItem mHistoryLastWritten = new HistoryItem();
287 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700288 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700289 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700290 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800291 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800292 int[] mReadHistoryUids;
293 int mReadHistoryChars;
294 int mNextHistoryTagIdx = 0;
295 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700296 int mHistoryBufferLastPos = -1;
297 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700298 int mActiveHistoryStates = 0xffffffff;
299 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700300 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700301 long mTrackRunningHistoryElapsedRealtime = 0;
302 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700303
304 final HistoryItem mHistoryCur = new HistoryItem();
305
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700306 HistoryItem mHistory;
307 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700308 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700309 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700310
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800311 // Used by computeHistoryStepDetails
312 HistoryStepDetails mLastHistoryStepDetails = null;
313 byte mLastHistoryStepLevel = 0;
314 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
315 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
316 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700317
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800318 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700319 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800320 */
321 long mLastStepCpuUserTime;
322 long mCurStepCpuUserTime;
323 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700324 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800325 */
326 long mLastStepCpuSystemTime;
327 long mCurStepCpuSystemTime;
328 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700329 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800330 */
331 long mLastStepStatUserTime;
332 long mLastStepStatSystemTime;
333 long mLastStepStatIOWaitTime;
334 long mLastStepStatIrqTime;
335 long mLastStepStatSoftIrqTime;
336 long mLastStepStatIdleTime;
337 long mCurStepStatUserTime;
338 long mCurStepStatSystemTime;
339 long mCurStepStatIOWaitTime;
340 long mCurStepStatIrqTime;
341 long mCurStepStatSoftIrqTime;
342 long mCurStepStatIdleTime;
343
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700344 private HistoryItem mHistoryIterator;
345 private boolean mReadOverflow;
346 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 int mStartCount;
349
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800350 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700351 String mStartPlatformVersion;
352 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 long mUptime;
355 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 long mRealtime;
357 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700358
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800359 int mWakeLockNesting;
360 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700361 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700362 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800363
Jeff Browne95c3cd2014-05-02 16:59:26 -0700364 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700365 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700366
Dianne Hackborn617f8772009-03-31 15:04:46 -0700367 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700368 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700369
Jeff Browne95c3cd2014-05-02 16:59:26 -0700370 boolean mInteractive;
371 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700372
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700373 boolean mPowerSaveModeEnabled;
374 StopwatchTimer mPowerSaveModeEnabledTimer;
375
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700376 boolean mDeviceIdling;
377 StopwatchTimer mDeviceIdlingTimer;
378
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700379 boolean mDeviceLightIdling;
380 StopwatchTimer mDeviceLightIdlingTimer;
381
382 int mDeviceIdleMode;
383 long mLastIdleTimeStart;
384 long mLongestLightIdleTime;
385 long mLongestFullIdleTime;
386 StopwatchTimer mDeviceIdleModeLightTimer;
387 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700390 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700391
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700392 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700393 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700394
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700395 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700396 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700397
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700398 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700399 StopwatchTimer mFlashlightOnTimer;
400
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700401 int mCameraOnNesting;
402 StopwatchTimer mCameraOnTimer;
403
Dianne Hackborn627bba72009-03-24 22:32:56 -0700404 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800405 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700406 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800407 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700408
409 StopwatchTimer mPhoneSignalScanningTimer;
410
Dianne Hackborn627bba72009-03-24 22:32:56 -0700411 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700412 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700413 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700414
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800415 final LongSamplingCounter[] mNetworkByteActivityCounters =
416 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
417 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700418 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
419
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800420 /**
421 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
422 */
423 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700424
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800425 /**
426 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
427 */
428 ControllerActivityCounterImpl mBluetoothActivity;
429
430 /**
431 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
432 */
433 ControllerActivityCounterImpl mModemActivity;
434
435 /**
436 * Whether the device supports WiFi controller energy reporting. This is set to true on
437 * the first WiFi energy report. See {@link #mWifiActivity}.
438 */
439 boolean mHasWifiReporting = false;
440
441 /**
442 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
443 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
444 */
445 boolean mHasBluetoothReporting = false;
446
447 /**
448 * Whether the device supports Modem controller energy reporting. This is set to true on
449 * the first Modem energy report. See {@link #mModemActivity}.
450 */
451 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700452
The Android Open Source Project10592532009-03-18 17:39:46 -0700453 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700454 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700455
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700456 boolean mGlobalWifiRunning;
457 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700458
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800459 int mWifiState = -1;
460 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
461
Dianne Hackborn3251b902014-06-20 14:40:53 -0700462 int mWifiSupplState = -1;
463 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
464
465 int mWifiSignalStrengthBin = -1;
466 final StopwatchTimer[] mWifiSignalStrengthsTimer =
467 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
468
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800469 int mBluetoothScanNesting;
470 StopwatchTimer mBluetoothScanTimer;
471
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700472 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700473 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800474 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800475 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700476 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800477 LongSamplingCounter mMobileRadioActiveUnknownTime;
478 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800479
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700480 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 /**
483 * These provide time bases that discount the time the device is plugged
484 * in to power.
485 */
486 boolean mOnBattery;
487 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700488
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700489 /**
490 * External reporting of whether the device is actually charging.
491 */
492 boolean mCharging = true;
493 int mLastChargingStateLevel;
494
The Android Open Source Project10592532009-03-18 17:39:46 -0700495 /*
496 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
497 */
Evan Millar633a1742009-04-02 16:36:33 -0700498 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700499 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700500 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700501 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700502 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700503 int mLowDischargeAmountSinceCharge;
504 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800505 int mDischargeScreenOnUnplugLevel;
506 int mDischargeScreenOffUnplugLevel;
507 int mDischargeAmountScreenOn;
508 int mDischargeAmountScreenOnSinceCharge;
509 int mDischargeAmountScreenOff;
510 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700511
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700512 private LongSamplingCounter mDischargeScreenOffCounter;
513 private LongSamplingCounter mDischargeCounter;
514
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700515 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700516
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700517 int mInitStepMode = 0;
518 int mCurStepMode = 0;
519 int mModStepMode = 0;
520
Dianne Hackborn260c5022014-04-29 11:23:16 -0700521 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700522 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800523 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
524 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700525 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700526
527 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700528 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800529 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
530 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
531
532 static final int MAX_DAILY_ITEMS = 10;
533
534 long mDailyStartTime = 0;
535 long mNextMinDailyDeadline = 0;
536 long mNextMaxDailyDeadline = 0;
537
538 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700541
Amith Yamasanif37447b2009-10-08 18:28:01 -0700542 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800543 private int mPhoneServiceStateRaw = -1;
544 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700545
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800546 private int mNumConnectivityChange;
547 private int mLoadedNumConnectivityChange;
548 private int mUnpluggedNumConnectivityChange;
549
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700550 private int mEstimatedBatteryCapacity = -1;
551
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700552 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
553
Adam Lesinskie08af192015-03-25 16:42:59 -0700554 private PowerProfile mPowerProfile;
555
Evan Millarc64edde2009-04-18 12:26:32 -0700556 /*
557 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
558 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700559 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700560
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700561 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700562 return mKernelWakelockStats;
563 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700564
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700565 String mLastWakeupReason = null;
566 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700567 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700568
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700569 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700570 return mWakeupReasonStats;
571 }
572
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700573 @Override
574 public LongCounter getDischargeScreenOffCoulombCounter() {
575 return mDischargeScreenOffCounter;
576 }
577
578 @Override
579 public LongCounter getDischargeCoulombCounter() {
580 return mDischargeCounter;
581 }
582
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700583 @Override
584 public int getEstimatedBatteryCapacity() {
585 return mEstimatedBatteryCapacity;
586 }
587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800589 this(new SystemClocks());
590 }
591
592 public BatteryStatsImpl(Clocks clocks) {
593 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700594 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700595 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800596 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700597 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700598 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700599 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700600 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602
Joe Onoratoabded112016-02-08 16:49:39 -0800603 private void init(Clocks clocks) {
604 mClocks = clocks;
605 mMobileNetworkStats = new NetworkStats[] {
606 new NetworkStats(mClocks.elapsedRealtime(), 50),
607 new NetworkStats(mClocks.elapsedRealtime(), 50),
608 new NetworkStats(mClocks.elapsedRealtime(), 50)
609 };
610 mWifiNetworkStats = new NetworkStats[] {
611 new NetworkStats(mClocks.elapsedRealtime(), 50),
612 new NetworkStats(mClocks.elapsedRealtime(), 50),
613 new NetworkStats(mClocks.elapsedRealtime(), 50)
614 };
615 }
616
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800617 public static interface TimeBaseObs {
618 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
619 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
620 }
621
Joe Onoratoabded112016-02-08 16:49:39 -0800622 // methods are protected not private to be VisibleForTesting
623 public static class TimeBase {
624 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800625
Joe Onoratoabded112016-02-08 16:49:39 -0800626 protected long mUptime;
627 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800628
Joe Onoratoabded112016-02-08 16:49:39 -0800629 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800630
Joe Onoratoabded112016-02-08 16:49:39 -0800631 protected long mPastUptime;
632 protected long mUptimeStart;
633 protected long mPastRealtime;
634 protected long mRealtimeStart;
635 protected long mUnpluggedUptime;
636 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800637
638 public void dump(PrintWriter pw, String prefix) {
639 StringBuilder sb = new StringBuilder(128);
640 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
641 sb.setLength(0);
642 sb.append(prefix);
643 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700644 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800645 pw.println(sb.toString());
646 sb.setLength(0);
647 sb.append(prefix);
648 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700649 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800650 pw.println(sb.toString());
651 sb.setLength(0);
652 sb.append(prefix);
653 sb.append("mPastUptime=");
654 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
655 formatTimeMs(sb, mUptimeStart / 1000);
656 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
657 pw.println(sb.toString());
658 sb.setLength(0);
659 sb.append(prefix);
660 sb.append("mPastRealtime=");
661 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
662 formatTimeMs(sb, mRealtimeStart / 1000);
663 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
664 pw.println(sb.toString());
665 }
666
667 public void add(TimeBaseObs observer) {
668 mObservers.add(observer);
669 }
670
671 public void remove(TimeBaseObs observer) {
672 if (!mObservers.remove(observer)) {
673 Slog.wtf(TAG, "Removed unknown observer: " + observer);
674 }
675 }
676
Joe Onoratoabded112016-02-08 16:49:39 -0800677 public boolean hasObserver(TimeBaseObs observer) {
678 return mObservers.contains(observer);
679 }
680
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800681 public void init(long uptime, long realtime) {
682 mRealtime = 0;
683 mUptime = 0;
684 mPastUptime = 0;
685 mPastRealtime = 0;
686 mUptimeStart = uptime;
687 mRealtimeStart = realtime;
688 mUnpluggedUptime = getUptime(mUptimeStart);
689 mUnpluggedRealtime = getRealtime(mRealtimeStart);
690 }
691
692 public void reset(long uptime, long realtime) {
693 if (!mRunning) {
694 mPastUptime = 0;
695 mPastRealtime = 0;
696 } else {
697 mUptimeStart = uptime;
698 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800699 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
700 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800701 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800702 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800703 mUnpluggedRealtime = getRealtime(realtime);
704 }
705 }
706
707 public long computeUptime(long curTime, int which) {
708 switch (which) {
709 case STATS_SINCE_CHARGED:
710 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800711 case STATS_CURRENT:
712 return getUptime(curTime);
713 case STATS_SINCE_UNPLUGGED:
714 return getUptime(curTime) - mUnpluggedUptime;
715 }
716 return 0;
717 }
718
719 public long computeRealtime(long curTime, int which) {
720 switch (which) {
721 case STATS_SINCE_CHARGED:
722 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800723 case STATS_CURRENT:
724 return getRealtime(curTime);
725 case STATS_SINCE_UNPLUGGED:
726 return getRealtime(curTime) - mUnpluggedRealtime;
727 }
728 return 0;
729 }
730
731 public long getUptime(long curTime) {
732 long time = mPastUptime;
733 if (mRunning) {
734 time += curTime - mUptimeStart;
735 }
736 return time;
737 }
738
739 public long getRealtime(long curTime) {
740 long time = mPastRealtime;
741 if (mRunning) {
742 time += curTime - mRealtimeStart;
743 }
744 return time;
745 }
746
747 public long getUptimeStart() {
748 return mUptimeStart;
749 }
750
751 public long getRealtimeStart() {
752 return mRealtimeStart;
753 }
754
755 public boolean isRunning() {
756 return mRunning;
757 }
758
759 public boolean setRunning(boolean running, long uptime, long realtime) {
760 if (mRunning != running) {
761 mRunning = running;
762 if (running) {
763 mUptimeStart = uptime;
764 mRealtimeStart = realtime;
765 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
766 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
767
768 for (int i = mObservers.size() - 1; i >= 0; i--) {
769 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
770 }
771 } else {
772 mPastUptime += uptime - mUptimeStart;
773 mPastRealtime += realtime - mRealtimeStart;
774
775 long batteryUptime = getUptime(uptime);
776 long batteryRealtime = getRealtime(realtime);
777
778 for (int i = mObservers.size() - 1; i >= 0; i--) {
779 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
780 }
781 }
782 return true;
783 }
784 return false;
785 }
786
787 public void readSummaryFromParcel(Parcel in) {
788 mUptime = in.readLong();
789 mRealtime = in.readLong();
790 }
791
792 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
793 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
794 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
795 }
796
797 public void readFromParcel(Parcel in) {
798 mRunning = false;
799 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800800 mPastUptime = in.readLong();
801 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700802 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800803 mPastRealtime = in.readLong();
804 mRealtimeStart = in.readLong();
805 mUnpluggedUptime = in.readLong();
806 mUnpluggedRealtime = in.readLong();
807 }
808
809 public void writeToParcel(Parcel out, long uptime, long realtime) {
810 final long runningUptime = getUptime(uptime);
811 final long runningRealtime = getRealtime(realtime);
812 out.writeLong(mUptime);
813 out.writeLong(runningUptime);
814 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700815 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800816 out.writeLong(runningRealtime);
817 out.writeLong(mRealtimeStart);
818 out.writeLong(mUnpluggedUptime);
819 out.writeLong(mUnpluggedRealtime);
820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700824 * State for keeping track of counting information.
825 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800826 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700827 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800828 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700829 int mLoadedCount;
830 int mLastCount;
831 int mUnpluggedCount;
832 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700833
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800834 Counter(TimeBase timeBase, Parcel in) {
835 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700836 mPluggedCount = in.readInt();
837 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700838 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700839 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700840 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800841 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700842 }
843
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800844 Counter(TimeBase timeBase) {
845 mTimeBase = timeBase;
846 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700847 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700848
Dianne Hackborn617f8772009-03-31 15:04:46 -0700849 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700850 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700851 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700852 out.writeInt(mUnpluggedCount);
853 }
854
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800855 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700856 mUnpluggedCount = mPluggedCount;
857 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700858 }
859
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800860 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700861 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700862 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700863
Dianne Hackborn617f8772009-03-31 15:04:46 -0700864 /**
865 * Writes a possibly null Counter to a Parcel.
866 *
867 * @param out the Parcel to be written to.
868 * @param counter a Counter, or null.
869 */
870 public static void writeCounterToParcel(Parcel out, Counter counter) {
871 if (counter == null) {
872 out.writeInt(0); // indicates null
873 return;
874 }
875 out.writeInt(1); // indicates non-null
876
877 counter.writeToParcel(out);
878 }
879
880 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700881 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700882 int val = mCount.get();
883 if (which == STATS_SINCE_UNPLUGGED) {
884 val -= mUnpluggedCount;
885 } else if (which != STATS_SINCE_CHARGED) {
886 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700887 }
888
889 return val;
890 }
891
892 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700893 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700894 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
895 + " mUnpluggedCount=" + mUnpluggedCount
896 + " mPluggedCount=" + mPluggedCount);
897 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700898
Christopher Tate4cee7252010-03-19 14:50:40 -0700899 void stepAtomic() {
900 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700901 }
902
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700903 /**
904 * Clear state of this counter.
905 */
906 void reset(boolean detachIfReset) {
907 mCount.set(0);
908 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
909 if (detachIfReset) {
910 detach();
911 }
912 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700913
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700914 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800915 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700916 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700917
Dianne Hackborn617f8772009-03-31 15:04:46 -0700918 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700919 int count = mCount.get();
920 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700921 }
922
923 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700924 mLoadedCount = in.readInt();
925 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700926 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700927 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700928 }
929 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700930
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700931 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800932 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700933 long mCount;
934 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700935 long mUnpluggedCount;
936 long mPluggedCount;
937
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800938 LongSamplingCounter(TimeBase timeBase, Parcel in) {
939 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700940 mPluggedCount = in.readLong();
941 mCount = mPluggedCount;
942 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700943 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700945 }
946
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800947 LongSamplingCounter(TimeBase timeBase) {
948 mTimeBase = timeBase;
949 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700950 }
951
952 public void writeToParcel(Parcel out) {
953 out.writeLong(mCount);
954 out.writeLong(mLoadedCount);
955 out.writeLong(mUnpluggedCount);
956 }
957
958 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800959 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700960 mUnpluggedCount = mPluggedCount;
961 mCount = mPluggedCount;
962 }
963
964 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800965 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700966 mPluggedCount = mCount;
967 }
968
969 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700970 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -0700971 if (which == STATS_SINCE_UNPLUGGED) {
972 val -= mUnpluggedCount;
973 } else if (which != STATS_SINCE_CHARGED) {
974 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700975 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700976 return val;
977 }
978
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700979 @Override
980 public void logState(Printer pw, String prefix) {
981 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700982 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700983 + " mUnpluggedCount=" + mUnpluggedCount
984 + " mPluggedCount=" + mPluggedCount);
985 }
986
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700987 void addCountLocked(long count) {
988 mCount += count;
989 }
990
991 /**
992 * Clear state of this counter.
993 */
994 void reset(boolean detachIfReset) {
995 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700996 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700997 if (detachIfReset) {
998 detach();
999 }
1000 }
1001
1002 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001003 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001004 }
1005
1006 void writeSummaryFromParcelLocked(Parcel out) {
1007 out.writeLong(mCount);
1008 }
1009
1010 void readSummaryFromParcelLocked(Parcel in) {
1011 mLoadedCount = in.readLong();
1012 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001013 mUnpluggedCount = mPluggedCount = mLoadedCount;
1014 }
1015 }
1016
Dianne Hackborn617f8772009-03-31 15:04:46 -07001017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * State for keeping track of timing information.
1019 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001020 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001021 protected final Clocks mClocks;
1022 protected final int mType;
1023 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001024
Joe Onoratoabded112016-02-08 16:49:39 -08001025 protected int mCount;
1026 protected int mLoadedCount;
1027 protected int mLastCount;
1028 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 // Times are in microseconds for better accuracy when dividing by the
1031 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 /**
1034 * The total time we have accumulated since the start of the original
1035 * boot, to the last time something interesting happened in the
1036 * current run.
1037 */
Joe Onoratoabded112016-02-08 16:49:39 -08001038 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 /**
1041 * The total time we loaded for the previous runs. Subtract this from
1042 * mTotalTime to find the time for the current run of the system.
1043 */
Joe Onoratoabded112016-02-08 16:49:39 -08001044 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * The run time of the last run of the system, as loaded from the
1048 * saved data.
1049 */
Joe Onoratoabded112016-02-08 16:49:39 -08001050 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 /**
1053 * The value of mTotalTime when unplug() was last called. Subtract
1054 * this from mTotalTime to find the time since the last unplug from
1055 * power.
1056 */
Joe Onoratoabded112016-02-08 16:49:39 -08001057 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001058
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001059 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001060 * The total time this timer has been running until the latest mark has been set.
1061 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1062 */
Joe Onoratoabded112016-02-08 16:49:39 -08001063 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001064
1065 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001066 * Constructs from a parcel.
1067 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001068 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001069 * @param in
1070 */
Joe Onoratoabded112016-02-08 16:49:39 -08001071 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1072 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001074 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 mCount = in.readInt();
1077 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001078 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 mUnpluggedCount = in.readInt();
1080 mTotalTime = in.readLong();
1081 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001082 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001084 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001085 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001086 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088
Joe Onoratoabded112016-02-08 16:49:39 -08001089 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1090 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001092 mTimeBase = timeBase;
1093 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Evan Millarc64edde2009-04-18 12:26:32 -07001095
1096 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001097
Evan Millarc64edde2009-04-18 12:26:32 -07001098 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001099
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001100 /**
1101 * Clear state of this timer. Returns true if the timer is inactive
1102 * so can be completely dropped.
1103 */
Joe Onoratoabded112016-02-08 16:49:39 -08001104 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001105 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001106 mCount = mLoadedCount = mLastCount = 0;
1107 if (detachIfReset) {
1108 detach();
1109 }
1110 return true;
1111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001112
Joe Onoratoabded112016-02-08 16:49:39 -08001113 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001114 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001115 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001116
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001117 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001118 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1119 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001120 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001123 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001126 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128
Adam Lesinskie08af192015-03-25 16:42:59 -07001129 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001130 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001132 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 + " old mUnpluggedTime=" + mUnpluggedTime
1134 + " old mUnpluggedCount=" + mUnpluggedCount);
1135 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001136 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001137 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (DEBUG && mType < 0) {
1139 Log.v(TAG, "unplug #" + mType
1140 + ": new mUnpluggedTime=" + mUnpluggedTime
1141 + " new mUnpluggedCount=" + mUnpluggedCount);
1142 }
1143 }
1144
Adam Lesinskie08af192015-03-25 16:42:59 -07001145 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001147 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001148 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001149 + " old mTotalTime=" + mTotalTime);
1150 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001151 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001152 mCount = computeCurrentCountLocked();
1153 if (DEBUG && mType < 0) {
1154 Log.v(TAG, "plug #" + mType
1155 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Writes a possibly null Timer to a Parcel.
1161 *
1162 * @param out the Parcel to be written to.
1163 * @param timer a Timer, or null.
1164 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001165 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (timer == null) {
1167 out.writeInt(0); // indicates null
1168 return;
1169 }
1170 out.writeInt(1); // indicates non-null
1171
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001172 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174
1175 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001176 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001177 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1178 if (which == STATS_SINCE_UNPLUGGED) {
1179 val -= mUnpluggedTime;
1180 } else if (which != STATS_SINCE_CHARGED) {
1181 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183
1184 return val;
1185 }
1186
1187 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001188 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001189 int val = computeCurrentCountLocked();
1190 if (which == STATS_SINCE_UNPLUGGED) {
1191 val -= mUnpluggedCount;
1192 } else if (which != STATS_SINCE_CHARGED) {
1193 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
1196 return val;
1197 }
1198
Adam Lesinskie08af192015-03-25 16:42:59 -07001199 @Override
1200 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1201 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1202 return val - mTimeBeforeMark;
1203 }
1204
1205 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001206 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001207 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1209 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001210 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001212 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001214 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001215
1216
Joe Onoratoabded112016-02-08 16:49:39 -08001217 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001218 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1219 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001220 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001221 }
1222
Joe Onoratoabded112016-02-08 16:49:39 -08001223 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001224 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001225 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001226 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001227 mUnpluggedTime = mTotalTime;
1228 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001229 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001230 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001231
1232 // When reading the summary, we set the mark to be the latest information.
1233 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001234 }
1235 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001236
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001237 /**
1238 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1239 * method. The state of the timer according to its {@link TimeBase} will determine how much
1240 * of the value is recorded.
1241 *
1242 * If the value being recorded resets, {@link #endSample()} can be called in order to
1243 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1244 * between calls, the {@link #endSample()} is automatically called and the new value is
1245 * expected to increase monotonically from that point on.
1246 */
Joe Onoratoabded112016-02-08 16:49:39 -08001247 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001248
Evan Millarc64edde2009-04-18 12:26:32 -07001249 /**
1250 * The most recent reported count from /proc/wakelocks.
1251 */
1252 int mCurrentReportedCount;
1253
1254 /**
1255 * The reported count from /proc/wakelocks when unplug() was last
1256 * called.
1257 */
1258 int mUnpluggedReportedCount;
1259
1260 /**
1261 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001262 */
Evan Millarc64edde2009-04-18 12:26:32 -07001263 long mCurrentReportedTotalTime;
1264
1265
1266 /**
1267 * The reported total_time from /proc/wakelocks when unplug() was last
1268 * called.
1269 */
1270 long mUnpluggedReportedTotalTime;
1271
1272 /**
1273 * Whether we are currently in a discharge cycle.
1274 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001275 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001276
1277 /**
1278 * Whether we are currently recording reported values.
1279 */
1280 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001281
Evan Millarc64edde2009-04-18 12:26:32 -07001282 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001283 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001284 */
1285 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001286
Adam Lesinski98f0d462016-04-19 16:46:20 -07001287 @VisibleForTesting
1288 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001289 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001290 mCurrentReportedCount = in.readInt();
1291 mUnpluggedReportedCount = in.readInt();
1292 mCurrentReportedTotalTime = in.readLong();
1293 mUnpluggedReportedTotalTime = in.readLong();
1294 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001295 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001296 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001297
Adam Lesinski98f0d462016-04-19 16:46:20 -07001298 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001299 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001300 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001301 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001302 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001303 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001304
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001305 /**
1306 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1307 * be less than the values used for a previous invocation.
1308 */
1309 public void endSample() {
1310 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1311 mCount = computeCurrentCountLocked();
1312 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1313 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001314 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001315
Evan Millarc64edde2009-04-18 12:26:32 -07001316 public void setUpdateVersion(int version) {
1317 mUpdateVersion = version;
1318 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001319
Evan Millarc64edde2009-04-18 12:26:32 -07001320 public int getUpdateVersion() {
1321 return mUpdateVersion;
1322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001323
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001324 /**
1325 * Updates the current recorded values. These are meant to be monotonically increasing
1326 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1327 *
1328 * If the values being recorded have been reset, the monotonically increasing requirement
1329 * will be broken. In this case, {@link #endSample()} is automatically called and
1330 * the total value of totalTime and count are recorded, starting a new monotonically
1331 * increasing sample.
1332 *
1333 * @param totalTime total time of sample in microseconds.
1334 * @param count total number of times the event being sampled occurred.
1335 */
1336 public void update(long totalTime, int count) {
1337 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001338 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001339 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001340 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001341 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001342
1343 mTrackingReportedValues = true;
1344
1345 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1346 endSample();
1347 }
1348
1349 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001350 mCurrentReportedCount = count;
1351 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001352
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001353 /**
1354 * Adds deltaTime and deltaCount to the current sample.
1355 *
1356 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1357 * @param deltaCount additional number of times the event being sampled occurred.
1358 */
1359 public void add(long deltaTime, int deltaCount) {
1360 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001361 }
1362
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001363 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001364 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1365 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001366 if (mTrackingReportedValues) {
1367 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1368 mUnpluggedReportedCount = mCurrentReportedCount;
1369 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001370 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001371 }
1372
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001373 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001374 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1375 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1376 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001377 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001378
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001379 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001380 public void logState(Printer pw, String prefix) {
1381 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001382 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001383 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1384 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1385 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1386 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001387
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001388 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001389 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001390 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001391 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1392 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001393
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001394 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001395 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001396 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001397 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1398 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001399
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001400 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001401 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1402 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001403 out.writeInt(mCurrentReportedCount);
1404 out.writeInt(mUnpluggedReportedCount);
1405 out.writeLong(mCurrentReportedTotalTime);
1406 out.writeLong(mUnpluggedReportedTotalTime);
1407 out.writeInt(mTrackingReportedValues ? 1 : 0);
1408 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001409
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001410 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001411 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001412 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001413 mTrackingReportedValues = false;
1414 mUnpluggedReportedTotalTime = 0;
1415 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001416 return true;
1417 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001418
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001419 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001420 public void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001421 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1422 out.writeLong(mCurrentReportedTotalTime);
1423 out.writeInt(mCurrentReportedCount);
1424 out.writeInt(mTrackingReportedValues ? 1 : 0);
1425 }
1426
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001427 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001428 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001429 super.readSummaryFromParcelLocked(in);
1430 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1431 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1432 mTrackingReportedValues = in.readInt() == 1;
1433 }
1434 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001435
Evan Millarc64edde2009-04-18 12:26:32 -07001436 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001437 * A timer that increments in batches. It does not run for durations, but just jumps
1438 * for a pre-determined amount.
1439 */
Joe Onoratoabded112016-02-08 16:49:39 -08001440 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001441 final Uid mUid;
1442
1443 /**
1444 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1445 */
1446 long mLastAddedTime;
1447
1448 /**
1449 * The last duration that we added to the timer. This is in microseconds.
1450 */
1451 long mLastAddedDuration;
1452
1453 /**
1454 * Whether we are currently in a discharge cycle.
1455 */
1456 boolean mInDischarge;
1457
Joe Onoratoabded112016-02-08 16:49:39 -08001458 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1459 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001460 mUid = uid;
1461 mLastAddedTime = in.readLong();
1462 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001463 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001464 }
1465
Joe Onoratoabded112016-02-08 16:49:39 -08001466 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1467 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001468 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001469 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001470 }
1471
1472 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001473 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1474 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001475 out.writeLong(mLastAddedTime);
1476 out.writeLong(mLastAddedDuration);
1477 }
1478
1479 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001480 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001481 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001482 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001483 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001484 }
1485
1486 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001487 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001488 recomputeLastDuration(elapsedRealtime, false);
1489 mInDischarge = true;
1490 // If we are still within the last added duration, then re-added whatever remains.
1491 if (mLastAddedTime == elapsedRealtime) {
1492 mTotalTime += mLastAddedDuration;
1493 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001494 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001495 }
1496
1497 @Override
1498 public void logState(Printer pw, String prefix) {
1499 super.logState(pw, prefix);
1500 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1501 + " mLastAddedDuration=" + mLastAddedDuration);
1502 }
1503
1504 private long computeOverage(long curTime) {
1505 if (mLastAddedTime > 0) {
1506 return mLastTime + mLastAddedDuration - curTime;
1507 }
1508 return 0;
1509 }
1510
1511 private void recomputeLastDuration(long curTime, boolean abort) {
1512 final long overage = computeOverage(curTime);
1513 if (overage > 0) {
1514 // Aborting before the duration ran out -- roll back the remaining
1515 // duration. Only do this if currently discharging; otherwise we didn't
1516 // actually add the time.
1517 if (mInDischarge) {
1518 mTotalTime -= overage;
1519 }
1520 if (abort) {
1521 mLastAddedTime = 0;
1522 } else {
1523 mLastAddedTime = curTime;
1524 mLastAddedDuration -= overage;
1525 }
1526 }
1527 }
1528
1529 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001530 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001531 recomputeLastDuration(now, true);
1532 mLastAddedTime = now;
1533 mLastAddedDuration = durationMillis * 1000;
1534 if (mInDischarge) {
1535 mTotalTime += mLastAddedDuration;
1536 mCount++;
1537 }
1538 }
1539
1540 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001541 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001542 recomputeLastDuration(now, true);
1543 }
1544
1545 @Override
1546 protected int computeCurrentCountLocked() {
1547 return mCount;
1548 }
1549
1550 @Override
1551 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001552 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001553 if (overage > 0) {
1554 return mTotalTime = overage;
1555 }
1556 return mTotalTime;
1557 }
1558
1559 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001560 public boolean reset(boolean detachIfReset) {
1561 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001562 recomputeLastDuration(now, true);
1563 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001564 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001565 return !stillActive;
1566 }
1567 }
1568
1569 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001570 * State for keeping track of timing information.
1571 */
Joe Onoratoabded112016-02-08 16:49:39 -08001572 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001573 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001574 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001575
Evan Millarc64edde2009-04-18 12:26:32 -07001576 int mNesting;
1577
Evan Millarc64edde2009-04-18 12:26:32 -07001578 /**
1579 * The last time at which we updated the timer. If mNesting is > 0,
1580 * subtract this from the current battery time to find the amount of
1581 * time we have been running since we last computed an update.
1582 */
1583 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001584
Evan Millarc64edde2009-04-18 12:26:32 -07001585 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001586 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001587 * was actually held for an interesting duration.
1588 */
1589 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001590
Amith Yamasanif37447b2009-10-08 18:28:01 -07001591 long mTimeout;
1592
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001593 /**
1594 * For partial wake locks, keep track of whether we are in the list
1595 * to consume CPU cycles.
1596 */
1597 boolean mInList;
1598
Joe Onoratoabded112016-02-08 16:49:39 -08001599 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001600 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001601 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001602 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001603 mTimerPool = timerPool;
1604 mUpdateTime = in.readLong();
1605 }
1606
Joe Onoratoabded112016-02-08 16:49:39 -08001607 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001608 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001609 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001610 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001611 mTimerPool = timerPool;
1612 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001613
Joe Onoratoabded112016-02-08 16:49:39 -08001614 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001615 mTimeout = timeout;
1616 }
1617
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001618 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1619 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001620 out.writeLong(mUpdateTime);
1621 }
1622
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001623 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001624 if (mNesting > 0) {
1625 if (DEBUG && mType < 0) {
1626 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1627 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001628 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1629 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001630 if (DEBUG && mType < 0) {
1631 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1632 }
1633 }
1634 }
1635
1636 public void logState(Printer pw, String prefix) {
1637 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001638 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 + " mAcquireTime=" + mAcquireTime);
1640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001641
Joe Onoratoabded112016-02-08 16:49:39 -08001642 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001644 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001645 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 if (mTimerPool != null) {
1647 // Accumulate time to all currently active timers before adding
1648 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001649 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 // Add this timer to the active pool
1651 mTimerPool.add(this);
1652 }
1653 // Increment the count
1654 mCount++;
1655 mAcquireTime = mTotalTime;
1656 if (DEBUG && mType < 0) {
1657 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1658 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1659 + " mAcquireTime=" + mAcquireTime);
1660 }
1661 }
1662 }
1663
Joe Onoratoabded112016-02-08 16:49:39 -08001664 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001665 return mNesting > 0;
1666 }
1667
Joe Onoratoabded112016-02-08 16:49:39 -08001668 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 // Ignore attempt to stop a timer that isn't running
1670 if (mNesting == 0) {
1671 return;
1672 }
1673 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001674 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if (mTimerPool != null) {
1676 // Accumulate time to all active counters, scaled by the total
1677 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001678 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 // Remove this timer from the active pool
1680 mTimerPool.remove(this);
1681 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 mNesting = 1;
1683 mTotalTime = computeRunTimeLocked(batteryRealtime);
1684 mNesting = 0;
1685 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (DEBUG && mType < 0) {
1688 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1689 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1690 + " mAcquireTime=" + mAcquireTime);
1691 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (mTotalTime == mAcquireTime) {
1694 // If there was no change in the time, then discard this
1695 // count. A somewhat cheezy strategy, but hey.
1696 mCount--;
1697 }
1698 }
1699 }
1700
Joe Onoratoabded112016-02-08 16:49:39 -08001701 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001702 if (mNesting > 0) {
1703 mNesting = 1;
1704 stopRunningLocked(elapsedRealtimeMs);
1705 }
1706 }
1707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 // Update the total time for all other running Timers with the same type as this Timer
1709 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001710 private static long refreshTimersLocked(long batteryRealtime,
1711 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001712 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 final int N = pool.size();
1714 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001715 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 long heldTime = batteryRealtime - t.mUpdateTime;
1717 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001718 final long myTime = heldTime / N;
1719 if (t == self) {
1720 selfTime = myTime;
1721 }
1722 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 t.mUpdateTime = batteryRealtime;
1725 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001726 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728
Evan Millarc64edde2009-04-18 12:26:32 -07001729 @Override
1730 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001731 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1732 curBatteryRealtime = mUpdateTime + mTimeout;
1733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 return mTotalTime + (mNesting > 0
1735 ? (curBatteryRealtime - mUpdateTime)
1736 / (mTimerPool != null ? mTimerPool.size() : 1)
1737 : 0);
1738 }
1739
Evan Millarc64edde2009-04-18 12:26:32 -07001740 @Override
1741 protected int computeCurrentCountLocked() {
1742 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744
Adam Lesinskie08af192015-03-25 16:42:59 -07001745 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001746 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001747 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001748 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001749 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001750 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001751 }
1752 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001753 return canDetach;
1754 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001755
Adam Lesinskie08af192015-03-25 16:42:59 -07001756 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001757 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001758 super.detach();
1759 if (mTimerPool != null) {
1760 mTimerPool.remove(this);
1761 }
1762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001763
Adam Lesinskie08af192015-03-25 16:42:59 -07001764 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001765 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001766 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 mNesting = 0;
1768 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001769
1770 /**
1771 * Set the mark so that we can query later for the total time the timer has
1772 * accumulated since this point. The timer can be running or not.
1773 *
1774 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1775 */
1776 public void setMark(long elapsedRealtimeMs) {
1777 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1778 if (mNesting > 0) {
1779 // We are running.
1780 if (mTimerPool != null) {
1781 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1782 } else {
1783 mTotalTime += batteryRealtime - mUpdateTime;
1784 mUpdateTime = batteryRealtime;
1785 }
1786 }
1787 mTimeBeforeMark = mTotalTime;
1788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001790
Dianne Hackbornd953c532014-08-16 18:17:38 -07001791 public abstract class OverflowArrayMap<T> {
1792 private static final String OVERFLOW_NAME = "*overflow*";
1793
1794 final ArrayMap<String, T> mMap = new ArrayMap<>();
1795 T mCurOverflow;
1796 ArrayMap<String, MutableInt> mActiveOverflow;
1797
1798 public OverflowArrayMap() {
1799 }
1800
1801 public ArrayMap<String, T> getMap() {
1802 return mMap;
1803 }
1804
1805 public void clear() {
1806 mMap.clear();
1807 mCurOverflow = null;
1808 mActiveOverflow = null;
1809 }
1810
1811 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07001812 if (name == null) {
1813 name = "";
1814 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001815 mMap.put(name, obj);
1816 if (OVERFLOW_NAME.equals(name)) {
1817 mCurOverflow = obj;
1818 }
1819 }
1820
1821 public void cleanup() {
1822 if (mActiveOverflow != null) {
1823 if (mActiveOverflow.size() == 0) {
1824 mActiveOverflow = null;
1825 }
1826 }
1827 if (mActiveOverflow == null) {
1828 // There is no currently active overflow, so we should no longer have
1829 // an overflow entry.
1830 if (mMap.containsKey(OVERFLOW_NAME)) {
1831 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1832 + mMap.get(OVERFLOW_NAME));
1833 mMap.remove(OVERFLOW_NAME);
1834 }
1835 mCurOverflow = null;
1836 } else {
1837 // There is currently active overflow, so we should still have an overflow entry.
1838 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1839 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1840 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1841 }
1842 }
1843 }
1844
1845 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001846 if (name == null) {
1847 name = "";
1848 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001849 T obj = mMap.get(name);
1850 if (obj != null) {
1851 return obj;
1852 }
1853
1854 // No object exists for the given name, but do we currently have it
1855 // running as part of the overflow?
1856 if (mActiveOverflow != null) {
1857 MutableInt over = mActiveOverflow.get(name);
1858 if (over != null) {
1859 // We are already actively counting this name in the overflow object.
1860 obj = mCurOverflow;
1861 if (obj == null) {
1862 // Shouldn't be here, but we'll try to recover.
1863 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1864 obj = mCurOverflow = instantiateObject();
1865 mMap.put(OVERFLOW_NAME, obj);
1866 }
1867 over.value++;
1868 return obj;
1869 }
1870 }
1871
1872 // No object exists for given name nor in the overflow; we need to make
1873 // a new one.
1874 final int N = mMap.size();
1875 if (N >= MAX_WAKELOCKS_PER_UID) {
1876 // Went over the limit on number of objects to track; this one goes
1877 // in to the overflow.
1878 obj = mCurOverflow;
1879 if (obj == null) {
1880 // Need to start overflow now...
1881 obj = mCurOverflow = instantiateObject();
1882 mMap.put(OVERFLOW_NAME, obj);
1883 }
1884 if (mActiveOverflow == null) {
1885 mActiveOverflow = new ArrayMap<>();
1886 }
1887 mActiveOverflow.put(name, new MutableInt(1));
1888 return obj;
1889 }
1890
1891 // Normal case where we just need to make a new object.
1892 obj = instantiateObject();
1893 mMap.put(name, obj);
1894 return obj;
1895 }
1896
1897 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001898 if (name == null) {
1899 name = "";
1900 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001901 T obj = mMap.get(name);
1902 if (obj != null) {
1903 return obj;
1904 }
1905
1906 // No object exists for the given name, but do we currently have it
1907 // running as part of the overflow?
1908 if (mActiveOverflow != null) {
1909 MutableInt over = mActiveOverflow.get(name);
1910 if (over != null) {
1911 // We are already actively counting this name in the overflow object.
1912 obj = mCurOverflow;
1913 if (obj != null) {
1914 over.value--;
1915 if (over.value <= 0) {
1916 mActiveOverflow.remove(name);
1917 }
1918 return obj;
1919 }
1920 }
1921 }
1922
1923 // Huh, they are stopping an active operation but we can't find one!
1924 // That's not good.
1925 Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
1926 + mMap.size() + " activeoverflow=" + mActiveOverflow
1927 + " curoverflow=" + mCurOverflow);
1928 return null;
1929 }
1930
1931 public abstract T instantiateObject();
1932 }
1933
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001934 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
1935 implements Parcelable {
1936 private final LongSamplingCounter mIdleTimeMillis;
1937 private final LongSamplingCounter mRxTimeMillis;
1938 private final LongSamplingCounter[] mTxTimeMillis;
1939 private final LongSamplingCounter mPowerDrainMaMs;
1940
1941 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
1942 mIdleTimeMillis = new LongSamplingCounter(timeBase);
1943 mRxTimeMillis = new LongSamplingCounter(timeBase);
1944 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1945 for (int i = 0; i < numTxStates; i++) {
1946 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
1947 }
1948 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
1949 }
1950
1951 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
1952 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
1953 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
1954 final int recordedTxStates = in.readInt();
1955 if (recordedTxStates != numTxStates) {
1956 throw new ParcelFormatException("inconsistent tx state lengths");
1957 }
1958
1959 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1960 for (int i = 0; i < numTxStates; i++) {
1961 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
1962 }
1963 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
1964 }
1965
1966 public void readSummaryFromParcel(Parcel in) {
1967 mIdleTimeMillis.readSummaryFromParcelLocked(in);
1968 mRxTimeMillis.readSummaryFromParcelLocked(in);
1969 final int recordedTxStates = in.readInt();
1970 if (recordedTxStates != mTxTimeMillis.length) {
1971 throw new ParcelFormatException("inconsistent tx state lengths");
1972 }
1973 for (LongSamplingCounter counter : mTxTimeMillis) {
1974 counter.readSummaryFromParcelLocked(in);
1975 }
1976 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
1977 }
1978
1979 @Override
1980 public int describeContents() {
1981 return 0;
1982 }
1983
1984 public void writeSummaryToParcel(Parcel dest) {
1985 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
1986 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
1987 dest.writeInt(mTxTimeMillis.length);
1988 for (LongSamplingCounter counter : mTxTimeMillis) {
1989 counter.writeSummaryFromParcelLocked(dest);
1990 }
1991 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
1992 }
1993
1994 @Override
1995 public void writeToParcel(Parcel dest, int flags) {
1996 mIdleTimeMillis.writeToParcel(dest);
1997 mRxTimeMillis.writeToParcel(dest);
1998 dest.writeInt(mTxTimeMillis.length);
1999 for (LongSamplingCounter counter : mTxTimeMillis) {
2000 counter.writeToParcel(dest);
2001 }
2002 mPowerDrainMaMs.writeToParcel(dest);
2003 }
2004
2005 public void reset(boolean detachIfReset) {
2006 mIdleTimeMillis.reset(detachIfReset);
2007 mRxTimeMillis.reset(detachIfReset);
2008 for (LongSamplingCounter counter : mTxTimeMillis) {
2009 counter.reset(detachIfReset);
2010 }
2011 mPowerDrainMaMs.reset(detachIfReset);
2012 }
2013
2014 public void detach() {
2015 mIdleTimeMillis.detach();
2016 mRxTimeMillis.detach();
2017 for (LongSamplingCounter counter : mTxTimeMillis) {
2018 counter.detach();
2019 }
2020 mPowerDrainMaMs.detach();
2021 }
2022
2023 /**
2024 * @return a LongSamplingCounter, measuring time spent in the idle state in
2025 * milliseconds.
2026 */
2027 @Override
2028 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002029 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002030 }
2031
2032 /**
2033 * @return a LongSamplingCounter, measuring time spent in the receive state in
2034 * milliseconds.
2035 */
2036 @Override
2037 public LongSamplingCounter getRxTimeCounter() {
2038 return mRxTimeMillis;
2039 }
2040
2041 /**
2042 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2043 * milliseconds.
2044 */
2045 @Override
2046 public LongSamplingCounter[] getTxTimeCounters() {
2047 return mTxTimeMillis;
2048 }
2049
2050 /**
2051 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2052 */
2053 @Override
2054 public LongSamplingCounter getPowerCounter() {
2055 return mPowerDrainMaMs;
2056 }
2057 }
2058
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002059 /*
2060 * Get the wakeup reason counter, and create a new one if one
2061 * doesn't already exist.
2062 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002063 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2064 SamplingTimer timer = mWakeupReasonStats.get(name);
2065 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002066 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002067 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002068 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002069 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002070 }
2071
Evan Millarc64edde2009-04-18 12:26:32 -07002072 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002073 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002074 * doesn't already exist.
2075 */
2076 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2077 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2078 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002079 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002080 mKernelWakelockStats.put(name, kwlt);
2081 }
2082 return kwlt;
2083 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002084
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002085 private int writeHistoryTag(HistoryTag tag) {
2086 Integer idxObj = mHistoryTagPool.get(tag);
2087 int idx;
2088 if (idxObj != null) {
2089 idx = idxObj;
2090 } else {
2091 idx = mNextHistoryTagIdx;
2092 HistoryTag key = new HistoryTag();
2093 key.setTo(tag);
2094 tag.poolIdx = idx;
2095 mHistoryTagPool.put(key, idx);
2096 mNextHistoryTagIdx++;
2097 mNumHistoryTagChars += key.string.length() + 1;
2098 }
2099 return idx;
2100 }
2101
2102 private void readHistoryTag(int index, HistoryTag tag) {
2103 tag.string = mReadHistoryStrings[index];
2104 tag.uid = mReadHistoryUids[index];
2105 tag.poolIdx = index;
2106 }
2107
Adam Lesinski926969b2016-04-28 17:31:12 -07002108 /*
2109 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2110
2111 There is always the first token, which may contain the delta time, or an indicator of
2112 the length of the time (int or long) following this token.
2113
2114 First token: always present,
2115 31 23 15 7 0
2116 â–ˆ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â–ˆ
2117
2118 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2119 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2120 delta time.
2121 A: battery level changed and an int follows with battery data.
2122 B: state changed and an int follows with state change data.
2123 C: state2 has changed and an int follows with state2 change data.
2124 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2125 E: event data has changed and an event struct follows.
2126 F: battery charge in coulombs has changed and an int with the charge follows.
2127 G: state flag denoting that the mobile radio was active.
2128 H: state flag denoting that the wifi radio was active.
2129 I: state flag denoting that a wifi scan occurred.
2130 J: state flag denoting that a wifi full lock was held.
2131 K: state flag denoting that the gps was on.
2132 L: state flag denoting that a wakelock was held.
2133 M: state flag denoting that the cpu was running.
2134
2135 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2136 with the time delta.
2137
2138 Battery level int: if A in the first token is set,
2139 31 23 15 7 0
2140 â–ˆ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â–ˆ
2141
2142 D: indicates that extra history details follow.
2143 V: the battery voltage.
2144 T: the battery temperature.
2145 L: the battery level (out of 100).
2146
2147 State change int: if B in the first token is set,
2148 31 23 15 7 0
2149 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2150
2151 A: wifi multicast was on.
2152 B: battery was plugged in.
2153 C: screen was on.
2154 D: phone was scanning for signal.
2155 E: audio was on.
2156 F: a sensor was active.
2157
2158 State2 change int: if C in the first token is set,
2159 31 23 15 7 0
2160 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2161
2162 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2163 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2164 C: a bluetooth scan was active.
2165 D: the camera was active.
2166 E: bluetooth was on.
2167 F: a phone call was active.
2168 G: the device was charging.
2169 H: 2 bits indicating the device-idle (doze) state: off, light, full
2170 I: the flashlight was on.
2171 J: wifi was on.
2172 K: wifi was running.
2173 L: video was playing.
2174 M: power save mode was on.
2175
2176 Wakelock/wakereason struct: if D in the first token is set,
2177 TODO(adamlesinski): describe wakelock/wakereason struct.
2178
2179 Event struct: if E in the first token is set,
2180 TODO(adamlesinski): describe the event struct.
2181
2182 History step details struct: if D in the battery level int is set,
2183 TODO(adamlesinski): describe the history step details struct.
2184
2185 Battery charge int: if F in the first token is set, an int representing the battery charge
2186 in coulombs follows.
2187 */
2188
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002189 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002190 static final int DELTA_TIME_MASK = 0x7ffff;
2191 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2192 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2193 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002194 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002195 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002196 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002197 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002198 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002199 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002200 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002201 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002202 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002203 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002204 // Flag in delta int: contains the battery charge count in uAh.
2205 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002206 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002207 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002208
2209 // These are the pieces of battery state that are packed in to the upper bits of
2210 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002211 // in STATE_BATTERY_MASK.
2212 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002213 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2214 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2215 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2216 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2217 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2218 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002219
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002220 // We use the low bit of the battery state int to indicate that we have full details
2221 // from a battery level change.
2222 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2223
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002224 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002225 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002226 dest.writeInt(DELTA_TIME_ABS);
2227 cur.writeToParcel(dest, 0);
2228 return;
2229 }
2230
2231 final long deltaTime = cur.time - last.time;
2232 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2233 final int lastStateInt = buildStateInt(last);
2234
2235 int deltaTimeToken;
2236 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2237 deltaTimeToken = DELTA_TIME_LONG;
2238 } else if (deltaTime >= DELTA_TIME_ABS) {
2239 deltaTimeToken = DELTA_TIME_INT;
2240 } else {
2241 deltaTimeToken = (int)deltaTime;
2242 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002243 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002244 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2245 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2246 final boolean computeStepDetails = includeStepDetails != 0
2247 || mLastHistoryStepDetails == null;
2248 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002249 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2250 if (batteryLevelIntChanged) {
2251 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2252 }
2253 final int stateInt = buildStateInt(cur);
2254 final boolean stateIntChanged = stateInt != lastStateInt;
2255 if (stateIntChanged) {
2256 firstToken |= DELTA_STATE_FLAG;
2257 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002258 final boolean state2IntChanged = cur.states2 != last.states2;
2259 if (state2IntChanged) {
2260 firstToken |= DELTA_STATE2_FLAG;
2261 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002262 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002263 firstToken |= DELTA_WAKELOCK_FLAG;
2264 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002265 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2266 firstToken |= DELTA_EVENT_FLAG;
2267 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002268
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002269 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2270 if (batteryChargeChanged) {
2271 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002272 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 dest.writeInt(firstToken);
2274 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2275 + " deltaTime=" + deltaTime);
2276
2277 if (deltaTimeToken >= DELTA_TIME_INT) {
2278 if (deltaTimeToken == DELTA_TIME_INT) {
2279 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2280 dest.writeInt((int)deltaTime);
2281 } else {
2282 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2283 dest.writeLong(deltaTime);
2284 }
2285 }
2286 if (batteryLevelIntChanged) {
2287 dest.writeInt(batteryLevelInt);
2288 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2289 + Integer.toHexString(batteryLevelInt)
2290 + " batteryLevel=" + cur.batteryLevel
2291 + " batteryTemp=" + cur.batteryTemperature
2292 + " batteryVolt=" + (int)cur.batteryVoltage);
2293 }
2294 if (stateIntChanged) {
2295 dest.writeInt(stateInt);
2296 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2297 + Integer.toHexString(stateInt)
2298 + " batteryStatus=" + cur.batteryStatus
2299 + " batteryHealth=" + cur.batteryHealth
2300 + " batteryPlugType=" + cur.batteryPlugType
2301 + " states=0x" + Integer.toHexString(cur.states));
2302 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002303 if (state2IntChanged) {
2304 dest.writeInt(cur.states2);
2305 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2306 + Integer.toHexString(cur.states2));
2307 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002308 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2309 int wakeLockIndex;
2310 int wakeReasonIndex;
2311 if (cur.wakelockTag != null) {
2312 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2313 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2314 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2315 } else {
2316 wakeLockIndex = 0xffff;
2317 }
2318 if (cur.wakeReasonTag != null) {
2319 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2320 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2321 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2322 } else {
2323 wakeReasonIndex = 0xffff;
2324 }
2325 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002326 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002327 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002328 int index = writeHistoryTag(cur.eventTag);
2329 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002330 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002331 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2332 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2333 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002334 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002335 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002336 if (mPlatformIdleStateCallback != null) {
2337 mCurHistoryStepDetails.statPlatformIdleState =
2338 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2339 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2340 mCurHistoryStepDetails.statPlatformIdleState);
2341 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002342 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2343 if (includeStepDetails != 0) {
2344 mCurHistoryStepDetails.writeToParcel(dest);
2345 }
2346 cur.stepDetails = mCurHistoryStepDetails;
2347 mLastHistoryStepDetails = mCurHistoryStepDetails;
2348 } else {
2349 cur.stepDetails = null;
2350 }
2351 if (mLastHistoryStepLevel < cur.batteryLevel) {
2352 mLastHistoryStepDetails = null;
2353 }
2354 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002355
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002356 if (batteryChargeChanged) {
2357 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2358 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002359 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002360 }
2361
2362 private int buildBatteryLevelInt(HistoryItem h) {
2363 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002364 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2365 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2366 }
2367
2368 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2369 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2370 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2371 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002372 }
2373
2374 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002375 int plugType = 0;
2376 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2377 plugType = 1;
2378 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2379 plugType = 2;
2380 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2381 plugType = 3;
2382 }
2383 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2384 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2385 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002386 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002387 }
2388
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002389 private void computeHistoryStepDetails(final HistoryStepDetails out,
2390 final HistoryStepDetails last) {
2391 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2392
2393 // Perform a CPU update right after we do this collection, so we have started
2394 // collecting good data for the next step.
2395 requestImmediateCpuUpdate();
2396
2397 if (last == null) {
2398 // We are not generating a delta, so all we need to do is reset the stats
2399 // we will later be doing a delta from.
2400 final int NU = mUidStats.size();
2401 for (int i=0; i<NU; i++) {
2402 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2403 uid.mLastStepUserTime = uid.mCurStepUserTime;
2404 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2405 }
2406 mLastStepCpuUserTime = mCurStepCpuUserTime;
2407 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2408 mLastStepStatUserTime = mCurStepStatUserTime;
2409 mLastStepStatSystemTime = mCurStepStatSystemTime;
2410 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2411 mLastStepStatIrqTime = mCurStepStatIrqTime;
2412 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2413 mLastStepStatIdleTime = mCurStepStatIdleTime;
2414 tmp.clear();
2415 return;
2416 }
2417 if (DEBUG) {
2418 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2419 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2420 + " irq=" + mLastStepStatIrqTime + " sirq="
2421 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2422 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2423 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2424 + " irq=" + mCurStepStatIrqTime + " sirq="
2425 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2426 }
2427 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2428 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2429 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2430 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2431 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2432 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2433 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2434 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2435 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2436 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2437 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2438 final int NU = mUidStats.size();
2439 for (int i=0; i<NU; i++) {
2440 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2441 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2442 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2443 final int totalTime = totalUTime + totalSTime;
2444 uid.mLastStepUserTime = uid.mCurStepUserTime;
2445 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2446 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2447 continue;
2448 }
2449 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2450 out.appCpuUid3 = uid.mUid;
2451 out.appCpuUTime3 = totalUTime;
2452 out.appCpuSTime3 = totalSTime;
2453 } else {
2454 out.appCpuUid3 = out.appCpuUid2;
2455 out.appCpuUTime3 = out.appCpuUTime2;
2456 out.appCpuSTime3 = out.appCpuSTime2;
2457 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2458 out.appCpuUid2 = uid.mUid;
2459 out.appCpuUTime2 = totalUTime;
2460 out.appCpuSTime2 = totalSTime;
2461 } else {
2462 out.appCpuUid2 = out.appCpuUid1;
2463 out.appCpuUTime2 = out.appCpuUTime1;
2464 out.appCpuSTime2 = out.appCpuSTime1;
2465 out.appCpuUid1 = uid.mUid;
2466 out.appCpuUTime1 = totalUTime;
2467 out.appCpuSTime1 = totalSTime;
2468 }
2469 }
2470 }
2471 mLastStepCpuUserTime = mCurStepCpuUserTime;
2472 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2473 mLastStepStatUserTime = mCurStepStatUserTime;
2474 mLastStepStatSystemTime = mCurStepStatSystemTime;
2475 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2476 mLastStepStatIrqTime = mCurStepStatIrqTime;
2477 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2478 mLastStepStatIdleTime = mCurStepStatIdleTime;
2479 }
2480
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002481 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2482 int firstToken = src.readInt();
2483 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002484 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002485 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002486 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2487 + " deltaTimeToken=" + deltaTimeToken);
2488
2489 if (deltaTimeToken < DELTA_TIME_ABS) {
2490 cur.time += deltaTimeToken;
2491 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2492 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002493 cur.numReadInts += 2;
2494 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002495 cur.readFromParcel(src);
2496 return;
2497 } else if (deltaTimeToken == DELTA_TIME_INT) {
2498 int delta = src.readInt();
2499 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002500 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002501 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2502 } else {
2503 long delta = src.readLong();
2504 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2505 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002506 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002507 }
2508
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002509 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002510 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002511 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002512 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002513 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002514 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2515 + Integer.toHexString(batteryLevelInt)
2516 + " batteryLevel=" + cur.batteryLevel
2517 + " batteryTemp=" + cur.batteryTemperature
2518 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002519 } else {
2520 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002521 }
2522
2523 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2524 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002525 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002526 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2527 & STATE_BATTERY_STATUS_MASK);
2528 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2529 & STATE_BATTERY_HEALTH_MASK);
2530 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2531 & STATE_BATTERY_PLUG_MASK);
2532 switch (cur.batteryPlugType) {
2533 case 1:
2534 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2535 break;
2536 case 2:
2537 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2538 break;
2539 case 3:
2540 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2541 break;
2542 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002543 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002544 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2545 + Integer.toHexString(stateInt)
2546 + " batteryStatus=" + cur.batteryStatus
2547 + " batteryHealth=" + cur.batteryHealth
2548 + " batteryPlugType=" + cur.batteryPlugType
2549 + " states=0x" + Integer.toHexString(cur.states));
2550 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002551 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002552 }
2553
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002554 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2555 cur.states2 = src.readInt();
2556 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2557 + Integer.toHexString(cur.states2));
2558 }
2559
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002560 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002561 int indexes = src.readInt();
2562 int wakeLockIndex = indexes&0xffff;
2563 int wakeReasonIndex = (indexes>>16)&0xffff;
2564 if (wakeLockIndex != 0xffff) {
2565 cur.wakelockTag = cur.localWakelockTag;
2566 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2567 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2568 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2569 } else {
2570 cur.wakelockTag = null;
2571 }
2572 if (wakeReasonIndex != 0xffff) {
2573 cur.wakeReasonTag = cur.localWakeReasonTag;
2574 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2575 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2576 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2577 } else {
2578 cur.wakeReasonTag = null;
2579 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002580 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002581 } else {
2582 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002583 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002584 }
2585
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002586 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002587 cur.eventTag = cur.localEventTag;
2588 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002589 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002590 final int index = ((codeAndIndex>>16)&0xffff);
2591 readHistoryTag(index, cur.eventTag);
2592 cur.numReadInts += 1;
2593 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2594 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2595 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002596 } else {
2597 cur.eventCode = HistoryItem.EVENT_NONE;
2598 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002599
2600 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2601 cur.stepDetails = mReadHistoryStepDetails;
2602 cur.stepDetails.readFromParcel(src);
2603 } else {
2604 cur.stepDetails = null;
2605 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002606
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002607 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2608 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002609 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002610 }
2611
Dianne Hackbornfc064132014-06-02 12:42:12 -07002612 @Override
2613 public void commitCurrentHistoryBatchLocked() {
2614 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2615 }
2616
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002617 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002618 if (!mHaveBatteryLevel || !mRecordingHistory) {
2619 return;
2620 }
2621
Dianne Hackborn40c87252014-03-19 16:55:40 -07002622 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002623 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2624 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002625 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002626 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002627 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2628 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002629 + Integer.toHexString(lastDiffStates) + " diff2="
2630 + Integer.toHexString(diffStates2) + " lastDiff2="
2631 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002632 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002633 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002634 && (diffStates2&lastDiffStates2) == 0
2635 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2636 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002637 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002638 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002639 || cur.eventCode == HistoryItem.EVENT_NONE)
2640 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2641 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2642 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2643 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2644 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2645 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002646 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002647 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002648 // as long as no bit has changed both between now and the last entry, as
2649 // well as the last entry and the one before it (so we capture any toggles).
2650 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002651 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2652 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2653 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002654 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002655 // If the last written history had a wakelock tag, we need to retain it.
2656 // Note that the condition above made sure that we aren't in a case where
2657 // both it and the current history item have a wakelock tag.
2658 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002659 cur.wakelockTag = cur.localWakelockTag;
2660 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002661 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002662 // If the last written history had a wake reason tag, we need to retain it.
2663 // Note that the condition above made sure that we aren't in a case where
2664 // both it and the current history item have a wakelock tag.
2665 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002666 cur.wakeReasonTag = cur.localWakeReasonTag;
2667 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002668 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002669 // If the last written history had an event, we need to retain it.
2670 // Note that the condition above made sure that we aren't in a case where
2671 // both it and the current history item have an event.
2672 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002673 cur.eventCode = mHistoryLastWritten.eventCode;
2674 cur.eventTag = cur.localEventTag;
2675 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002676 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002677 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002678 }
2679
2680 final int dataSize = mHistoryBuffer.dataSize();
2681 if (dataSize >= MAX_HISTORY_BUFFER) {
2682 if (!mHistoryOverflow) {
2683 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002684 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2685 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002686 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002687 }
2688
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002689 // After overflow, we allow various bit-wise states to settle to 0.
2690 boolean writeAnyway = false;
2691 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2692 & mActiveHistoryStates;
2693 if (mHistoryLastWritten.states != curStates) {
2694 // mActiveHistoryStates keeps track of which bits in .states are now being
2695 // forced to 0.
2696 int old = mActiveHistoryStates;
2697 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2698 writeAnyway |= old != mActiveHistoryStates;
2699 }
2700 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2701 & mActiveHistoryStates2;
2702 if (mHistoryLastWritten.states2 != curStates2) {
2703 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2704 // forced to 0.
2705 int old = mActiveHistoryStates2;
2706 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2707 writeAnyway |= old != mActiveHistoryStates2;
2708 }
2709
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002710 // Once we've reached the maximum number of items, we only
2711 // record changes to the battery level and the most interesting states.
2712 // Once we've reached the maximum maximum number of items, we only
2713 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002714 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002715 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002716 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002717 & HistoryItem.MOST_INTERESTING_STATES) == 0
2718 || ((mHistoryLastWritten.states2^cur.states2)
2719 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002720 return;
2721 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002722
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002723 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002724 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002725 }
2726
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002727 if (dataSize == 0) {
2728 // The history is currently empty; we need it to start with a time stamp.
2729 cur.currentTime = System.currentTimeMillis();
2730 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2731 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002732 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002733 }
2734
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002735 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2736 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002737 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002738 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002739 }
2740 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2741 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002742 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002743 mHistoryLastWritten.states &= mActiveHistoryStates;
2744 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002745 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002746 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002747 cur.wakelockTag = null;
2748 cur.wakeReasonTag = null;
2749 cur.eventCode = HistoryItem.EVENT_NONE;
2750 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002751 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2752 + " now " + mHistoryBuffer.dataPosition()
2753 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002754 }
2755
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002756 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002757 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002758
Dianne Hackborn40c87252014-03-19 16:55:40 -07002759 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002760 if (mTrackRunningHistoryElapsedRealtime != 0) {
2761 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2762 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2763 if (diffUptime < (diffElapsed-20)) {
2764 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2765 mHistoryAddTmp.setTo(mHistoryLastWritten);
2766 mHistoryAddTmp.wakelockTag = null;
2767 mHistoryAddTmp.wakeReasonTag = null;
2768 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2769 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2770 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2771 }
2772 }
2773 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2774 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2775 mTrackRunningHistoryUptime = uptimeMs;
2776 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2777 }
2778
2779 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2780 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002781
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002782 if (!USE_OLD_HISTORY) {
2783 return;
2784 }
2785
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002786 if (!mHaveBatteryLevel || !mRecordingHistory) {
2787 return;
2788 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002789
2790 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002791 // and no states have since the last recorded entry changed and
2792 // are now resetting back to their original value, then just collapse
2793 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002794 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002795 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002796 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2797 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002798 // If the current is the same as the one before, then we no
2799 // longer need the entry.
2800 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002801 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002802 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002803 mHistoryLastEnd.next = null;
2804 mHistoryEnd.next = mHistoryCache;
2805 mHistoryCache = mHistoryEnd;
2806 mHistoryEnd = mHistoryLastEnd;
2807 mHistoryLastEnd = null;
2808 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002809 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2810 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002811 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002812 }
2813 return;
2814 }
2815
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002816 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002817 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002818
2819 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2820 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002821 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002822 }
2823
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002824 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2825 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002826 // record changes to the battery level and the most interesting states.
2827 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002828 // record changes to the battery level.
2829 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002830 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002831 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002832 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002833 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002834 return;
2835 }
2836 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002837
Dianne Hackborn40c87252014-03-19 16:55:40 -07002838 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002839 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002840
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002841 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002842 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002843 mHistoryCur.eventCode = code;
2844 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2845 mHistoryCur.eventTag.string = name;
2846 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002847 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002848 }
2849
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002850 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002851 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002852 if (rec != null) {
2853 mHistoryCache = rec.next;
2854 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002855 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002856 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002857 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002858
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002859 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002860 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002861
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002862 void addHistoryRecordLocked(HistoryItem rec) {
2863 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002864 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002865 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002866 if (mHistoryEnd != null) {
2867 mHistoryEnd.next = rec;
2868 mHistoryEnd = rec;
2869 } else {
2870 mHistory = mHistoryEnd = rec;
2871 }
2872 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002873
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002874 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002875 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002876 if (USE_OLD_HISTORY) {
2877 if (mHistory != null) {
2878 mHistoryEnd.next = mHistoryCache;
2879 mHistoryCache = mHistory;
2880 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2881 }
2882 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002883 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002884
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002885 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002886 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002887 mTrackRunningHistoryElapsedRealtime = 0;
2888 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002889
2890 mHistoryBuffer.setDataSize(0);
2891 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002892 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002893 mHistoryLastLastWritten.clear();
2894 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002895 mHistoryTagPool.clear();
2896 mNextHistoryTagIdx = 0;
2897 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002898 mHistoryBufferLastPos = -1;
2899 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002900 mActiveHistoryStates = 0xffffffff;
2901 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002902 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002903
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002904 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2905 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002906 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002907
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002908 boolean unpluggedScreenOff = unplugged && screenOff;
2909 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2910 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07002911 if (DEBUG_ENERGY_CPU) {
2912 Slog.d(TAG, "Updating cpu time because screen is now " +
2913 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002914 }
Adam Lesinski72478f02015-06-17 15:39:43 -07002915 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002916 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 }
2918 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002919
Dianne Hackborn099bc622014-01-22 13:39:16 -08002920 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2921 mIsolatedUids.put(isolatedUid, appUid);
2922 }
2923
Adam Lesinski61db88f2015-07-01 15:05:07 -07002924 /**
2925 * Schedules a read of the latest cpu times before removing the isolated UID.
2926 * @see #removeIsolatedUidLocked(int)
2927 */
2928 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002929 int curUid = mIsolatedUids.get(isolatedUid, -1);
2930 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07002931 if (mExternalSync != null) {
2932 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
2933 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002934 }
2935 }
2936
Adam Lesinski61db88f2015-07-01 15:05:07 -07002937 /**
2938 * This should only be called after the cpu times have been read.
2939 * @see #scheduleRemoveIsolatedUidLocked(int, int)
2940 */
2941 public void removeIsolatedUidLocked(int isolatedUid) {
2942 mIsolatedUids.delete(isolatedUid);
2943 mKernelUidCpuTimeReader.removeUid(isolatedUid);
2944 }
2945
Dianne Hackborn099bc622014-01-22 13:39:16 -08002946 public int mapUid(int uid) {
2947 int isolated = mIsolatedUids.get(uid, -1);
2948 return isolated > 0 ? isolated : uid;
2949 }
2950
2951 public void noteEventLocked(int code, String name, int uid) {
2952 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002953 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2954 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002955 }
Joe Onoratoabded112016-02-08 16:49:39 -08002956 final long elapsedRealtime = mClocks.elapsedRealtime();
2957 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002958 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002959 }
2960
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002961 boolean ensureStartClockTime(final long currentTime) {
2962 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
2963 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
2964 // If the start clock time has changed by more than a year, then presumably
2965 // the previous time was completely bogus. So we are going to figure out a
2966 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08002967 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002968 return true;
2969 }
2970 return false;
2971 }
2972
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002973 public void noteCurrentTimeChangedLocked() {
2974 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08002975 final long elapsedRealtime = mClocks.elapsedRealtime();
2976 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002977 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002978 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002979 }
2980
Dianne Hackborn61659e52014-07-09 16:13:01 -07002981 public void noteProcessStartLocked(String name, int uid) {
2982 uid = mapUid(uid);
2983 if (isOnBattery()) {
2984 Uid u = getUidStatsLocked(uid);
2985 u.getProcessStatsLocked(name).incStartsLocked();
2986 }
2987 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2988 return;
2989 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002990 if (!mRecordAllHistory) {
2991 return;
2992 }
Joe Onoratoabded112016-02-08 16:49:39 -08002993 final long elapsedRealtime = mClocks.elapsedRealtime();
2994 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002995 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2996 }
2997
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002998 public void noteProcessCrashLocked(String name, int uid) {
2999 uid = mapUid(uid);
3000 if (isOnBattery()) {
3001 Uid u = getUidStatsLocked(uid);
3002 u.getProcessStatsLocked(name).incNumCrashesLocked();
3003 }
3004 }
3005
3006 public void noteProcessAnrLocked(String name, int uid) {
3007 uid = mapUid(uid);
3008 if (isOnBattery()) {
3009 Uid u = getUidStatsLocked(uid);
3010 u.getProcessStatsLocked(name).incNumAnrsLocked();
3011 }
3012 }
3013
Dianne Hackborna8d10942015-11-19 17:55:19 -08003014 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003015 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003016 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003017 }
3018
3019 public void noteProcessFinishLocked(String name, int uid) {
3020 uid = mapUid(uid);
3021 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3022 return;
3023 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003024 if (!mRecordAllHistory) {
3025 return;
3026 }
Joe Onoratoabded112016-02-08 16:49:39 -08003027 final long elapsedRealtime = mClocks.elapsedRealtime();
3028 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003029 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003030 }
3031
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003032 public void noteSyncStartLocked(String name, int uid) {
3033 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003034 final long elapsedRealtime = mClocks.elapsedRealtime();
3035 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003036 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3037 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3038 return;
3039 }
3040 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3041 }
3042
3043 public void noteSyncFinishLocked(String name, int uid) {
3044 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003045 final long elapsedRealtime = mClocks.elapsedRealtime();
3046 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003047 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3048 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3049 return;
3050 }
3051 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3052 }
3053
3054 public void noteJobStartLocked(String name, int uid) {
3055 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003056 final long elapsedRealtime = mClocks.elapsedRealtime();
3057 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003058 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3059 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3060 return;
3061 }
3062 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3063 }
3064
3065 public void noteJobFinishLocked(String name, int uid) {
3066 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003067 final long elapsedRealtime = mClocks.elapsedRealtime();
3068 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003069 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3070 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3071 return;
3072 }
3073 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3074 }
3075
Dianne Hackborn1e383822015-04-10 14:02:33 -07003076 public void noteAlarmStartLocked(String name, int uid) {
3077 if (!mRecordAllHistory) {
3078 return;
3079 }
3080 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003081 final long elapsedRealtime = mClocks.elapsedRealtime();
3082 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003083 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3084 return;
3085 }
3086 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3087 }
3088
3089 public void noteAlarmFinishLocked(String name, int uid) {
3090 if (!mRecordAllHistory) {
3091 return;
3092 }
3093 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003094 final long elapsedRealtime = mClocks.elapsedRealtime();
3095 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003096 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3097 return;
3098 }
3099 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3100 }
3101
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003102 private void requestWakelockCpuUpdate() {
3103 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3104 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3105 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3106 }
3107 }
3108
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003109 private void requestImmediateCpuUpdate() {
3110 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3111 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3112 }
3113
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003114 public void setRecordAllHistoryLocked(boolean enabled) {
3115 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003116 if (!enabled) {
3117 // Clear out any existing state.
3118 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003119 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003120 // Record the currently running processes as stopping, now that we are no
3121 // longer tracking them.
3122 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3123 HistoryItem.EVENT_PROC);
3124 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003125 long mSecRealtime = mClocks.elapsedRealtime();
3126 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003127 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3128 SparseIntArray uids = ent.getValue();
3129 for (int j=0; j<uids.size(); j++) {
3130 addHistoryEventLocked(mSecRealtime, mSecUptime,
3131 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3132 }
3133 }
3134 }
3135 } else {
3136 // Record the currently running processes as starting, now that we are tracking them.
3137 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3138 HistoryItem.EVENT_PROC);
3139 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003140 long mSecRealtime = mClocks.elapsedRealtime();
3141 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003142 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3143 SparseIntArray uids = ent.getValue();
3144 for (int j=0; j<uids.size(); j++) {
3145 addHistoryEventLocked(mSecRealtime, mSecUptime,
3146 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3147 }
3148 }
3149 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003150 }
3151 }
3152
Dianne Hackborn9a755432014-05-15 17:05:22 -07003153 public void setNoAutoReset(boolean enabled) {
3154 mNoAutoReset = enabled;
3155 }
3156
3157 private String mInitialAcquireWakeName;
3158 private int mInitialAcquireWakeUid = -1;
3159
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003160 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003161 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003162 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003163 if (type == WAKE_TYPE_PARTIAL) {
3164 // Only care about partial wake locks, since full wake locks
3165 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003166 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003167 if (historyName == null) {
3168 historyName = name;
3169 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003170 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003171 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3172 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003173 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003174 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003175 }
3176 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003177 if (mWakeLockNesting == 0) {
3178 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3179 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3180 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003181 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003182 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003183 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003184 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003185 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003186 } else if (!mWakeLockImportant && !unimportantForLogging
3187 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003188 if (mHistoryLastWritten.wakelockTag != null) {
3189 // We'll try to update the last tag.
3190 mHistoryLastWritten.wakelockTag = null;
3191 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003192 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003193 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003194 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003195 }
3196 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003197 }
3198 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003199 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003200 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003201 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3202 // We only update the cpu time when a wake lock is acquired if the screen is off.
3203 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3204 if (DEBUG_ENERGY_CPU) {
3205 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3206 }
3207 requestWakelockCpuUpdate();
3208 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003209 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003210 }
3211 }
3212
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003213 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3214 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003215 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003216 if (type == WAKE_TYPE_PARTIAL) {
3217 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003218 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003219 if (historyName == null) {
3220 historyName = name;
3221 }
3222 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3223 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003224 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003225 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003226 }
3227 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003228 if (mWakeLockNesting == 0) {
3229 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3230 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3231 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003232 mInitialAcquireWakeName = null;
3233 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003234 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003235 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003236 }
3237 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003238 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3239 if (DEBUG_ENERGY_CPU) {
3240 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3241 }
3242 requestWakelockCpuUpdate();
3243 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003244 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003245 }
3246 }
3247
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003248 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3249 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003250 final long elapsedRealtime = mClocks.elapsedRealtime();
3251 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003252 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003253 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003254 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003255 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003256 }
3257 }
3258
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003259 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3260 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003261 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003262 final long elapsedRealtime = mClocks.elapsedRealtime();
3263 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003264 // For correct semantics, we start the need worksources first, so that we won't
3265 // make inappropriate history items as if all wake locks went away and new ones
3266 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003267 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003268 for (int i=0; i<NN; i++) {
3269 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003270 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003271 }
3272 final int NO = ws.size();
3273 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003274 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003275 }
3276 }
3277
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003278 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3279 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003280 final long elapsedRealtime = mClocks.elapsedRealtime();
3281 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003282 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003283 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003284 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003285 }
3286 }
3287
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003288 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3289 uid = mapUid(uid);
3290 final long elapsedRealtime = mClocks.elapsedRealtime();
3291 final long uptime = mClocks.uptimeMillis();
3292 if (historyName == null) {
3293 historyName = name;
3294 }
3295 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3296 0)) {
3297 return;
3298 }
3299 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3300 historyName, uid);
3301 }
3302
3303 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3304 uid = mapUid(uid);
3305 final long elapsedRealtime = mClocks.elapsedRealtime();
3306 final long uptime = mClocks.uptimeMillis();
3307 if (historyName == null) {
3308 historyName = name;
3309 }
3310 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3311 0)) {
3312 return;
3313 }
3314 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3315 historyName, uid);
3316 }
3317
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003318 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3319 if (mLastWakeupReason != null) {
3320 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003321 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003322 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003323 mLastWakeupReason = null;
3324 }
3325 }
3326
3327 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003328 final long elapsedRealtime = mClocks.elapsedRealtime();
3329 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003330 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003331 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003332 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003333 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3334 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003335 mHistoryCur.wakeReasonTag.uid = 0;
3336 mLastWakeupReason = reason;
3337 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003338 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003339 }
3340
Adam Lesinski72478f02015-06-17 15:39:43 -07003341 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003342 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003343 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003344 }
3345
Adam Lesinski72478f02015-06-17 15:39:43 -07003346 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3347 int statSystemTime, int statIOWaitTime, int statIrqTime,
3348 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003349 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3350 + " user=" + statUserTime + " sys=" + statSystemTime
3351 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3352 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3353 mCurStepCpuUserTime += totalUTime;
3354 mCurStepCpuSystemTime += totalSTime;
3355 mCurStepStatUserTime += statUserTime;
3356 mCurStepStatSystemTime += statSystemTime;
3357 mCurStepStatIOWaitTime += statIOWaitTime;
3358 mCurStepStatIrqTime += statIrqTime;
3359 mCurStepStatSoftIrqTime += statSoftIrqTime;
3360 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003361 }
3362
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003363 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003364 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003365 Uid u = mUidStats.get(uid);
3366 if (u != null) {
3367 u.mPids.remove(pid);
3368 }
3369 }
3370
3371 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003372 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003373 Uid u = mUidStats.get(uid);
3374 if (u != null) {
3375 Uid.Pid p = u.mPids.get(pid);
3376 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003377 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003378 }
3379 }
3380 return 0;
3381 }
3382
3383 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003384 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003385 Uid u = mUidStats.get(uid);
3386 if (u != null) {
3387 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3388 }
3389 }
3390
Dianne Hackborn287952c2010-09-22 22:34:31 -07003391 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003392 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003393 Uid u = mUidStats.get(uid);
3394 if (u != null) {
3395 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3396 }
3397 }
3398
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003399 int mSensorNesting;
3400
3401 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003402 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003403 final long elapsedRealtime = mClocks.elapsedRealtime();
3404 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003405 if (mSensorNesting == 0) {
3406 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3407 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3408 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003409 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003410 }
3411 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003412 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003413 }
3414
3415 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003416 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003417 final long elapsedRealtime = mClocks.elapsedRealtime();
3418 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003419 mSensorNesting--;
3420 if (mSensorNesting == 0) {
3421 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3422 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3423 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003424 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003425 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003426 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003427 }
3428
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003429 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003430
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003431 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003432 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003433 final long elapsedRealtime = mClocks.elapsedRealtime();
3434 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003435 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003436 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003437 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3438 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003439 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003440 }
3441 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003442 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003444
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003445 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003446 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003447 final long elapsedRealtime = mClocks.elapsedRealtime();
3448 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003449 mGpsNesting--;
3450 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003451 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003452 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3453 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003454 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003455 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003456 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003458
Jeff Browne95c3cd2014-05-02 16:59:26 -07003459 public void noteScreenStateLocked(int state) {
3460 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003461 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003462 final int oldState = mScreenState;
3463 mScreenState = state;
3464 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3465 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003466
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003467 if (state != Display.STATE_UNKNOWN) {
3468 int stepState = state-1;
3469 if (stepState < 4) {
3470 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3471 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3472 } else {
3473 Slog.wtf(TAG, "Unexpected screen state: " + state);
3474 }
3475 }
3476
Jeff Browne95c3cd2014-05-02 16:59:26 -07003477 if (state == Display.STATE_ON) {
3478 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003479 final long elapsedRealtime = mClocks.elapsedRealtime();
3480 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003481 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3482 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3483 + Integer.toHexString(mHistoryCur.states));
3484 addHistoryRecordLocked(elapsedRealtime, uptime);
3485 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3486 if (mScreenBrightnessBin >= 0) {
3487 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3488 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003489
Jeff Browne95c3cd2014-05-02 16:59:26 -07003490 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003491 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003492
Jeff Browne95c3cd2014-05-02 16:59:26 -07003493 // Fake a wake lock, so we consider the device waked as long
3494 // as the screen is on.
3495 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3496 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003497
Jeff Browne95c3cd2014-05-02 16:59:26 -07003498 // Update discharge amounts.
3499 if (mOnBatteryInternal) {
3500 updateDischargeScreenLevelsLocked(false, true);
3501 }
3502 } else if (oldState == Display.STATE_ON) {
3503 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003504 final long elapsedRealtime = mClocks.elapsedRealtime();
3505 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003506 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3507 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3508 + Integer.toHexString(mHistoryCur.states));
3509 addHistoryRecordLocked(elapsedRealtime, uptime);
3510 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3511 if (mScreenBrightnessBin >= 0) {
3512 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3513 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003514
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003515 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003516 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003517
Jeff Browne95c3cd2014-05-02 16:59:26 -07003518 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003519 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003520
Jeff Browne95c3cd2014-05-02 16:59:26 -07003521 // Update discharge amounts.
3522 if (mOnBatteryInternal) {
3523 updateDischargeScreenLevelsLocked(true, false);
3524 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003525 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003526 }
3527 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003528
Dianne Hackborn617f8772009-03-31 15:04:46 -07003529 public void noteScreenBrightnessLocked(int brightness) {
3530 // Bin the brightness.
3531 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3532 if (bin < 0) bin = 0;
3533 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3534 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003535 final long elapsedRealtime = mClocks.elapsedRealtime();
3536 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003537 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3538 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003539 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3540 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003541 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003542 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003543 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003544 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003545 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003546 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003547 }
3548 mScreenBrightnessBin = bin;
3549 }
3550 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003551
Dianne Hackborn617f8772009-03-31 15:04:46 -07003552 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003553 if (mOnBatteryInternal) {
3554 uid = mapUid(uid);
3555 getUidStatsLocked(uid).noteUserActivityLocked(event);
3556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003558
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003559 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003560 final long elapsedRealtime = mClocks.elapsedRealtime();
3561 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003562 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3563 reason, reasonUid);
3564 }
3565
Jeff Browne95c3cd2014-05-02 16:59:26 -07003566 public void noteInteractiveLocked(boolean interactive) {
3567 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003568 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003569 mInteractive = interactive;
3570 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3571 if (interactive) {
3572 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3573 } else {
3574 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3575 }
3576 }
3577 }
3578
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003579 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003580 final long elapsedRealtime = mClocks.elapsedRealtime();
3581 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003582 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3583 extra, type);
3584 mNumConnectivityChange++;
3585 }
3586
Adam Lesinski5f056f62016-07-14 16:56:08 -07003587 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
3588 final long uptimeMillis, int uid) {
3589 uid = mapUid(uid);
3590 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
3591 uid);
3592 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
3593 }
3594
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -07003595 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003596 final long elapsedRealtime = mClocks.elapsedRealtime();
3597 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003598 if (mMobileRadioPowerState != powerState) {
3599 long realElapsedRealtimeMs;
3600 final boolean active =
3601 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3602 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3603 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07003604 if (uid > 0) {
3605 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
3606 }
3607
Adam Lesinski9acfd812016-04-19 18:29:50 -07003608 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003609 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3610 } else {
3611 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003612 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003613 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3614 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3615 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003616 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003617 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3618 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3619 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003620 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003621 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3622 }
3623 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3624 + Integer.toHexString(mHistoryCur.states));
3625 addHistoryRecordLocked(elapsedRealtime, uptime);
3626 mMobileRadioPowerState = powerState;
3627 if (active) {
3628 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3629 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3630 } else {
3631 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003632 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003633 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003634 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003635 }
3636 }
3637
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003638 public void notePowerSaveMode(boolean enabled) {
3639 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003640 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3641 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3642 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08003643 final long elapsedRealtime = mClocks.elapsedRealtime();
3644 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003645 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003646 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003647 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3648 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003649 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003650 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003651 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003652 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3653 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003654 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003655 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003656 }
3657 addHistoryRecordLocked(elapsedRealtime, uptime);
3658 }
3659 }
3660
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003661 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003662 final long elapsedRealtime = mClocks.elapsedRealtime();
3663 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003664 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003665 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003666 // We don't go out of general idling mode until explicitly taken out of
3667 // device idle through going active or significant motion.
3668 nowIdling = true;
3669 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003670 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
3671 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
3672 // We don't go out of general light idling mode until explicitly taken out of
3673 // device idle through going active or significant motion.
3674 nowLightIdling = true;
3675 }
3676 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
3677 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3678 activeReason, activeUid);
3679 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003680 if (mDeviceIdling != nowIdling) {
3681 mDeviceIdling = nowIdling;
3682 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3683 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3684 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003685 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003686 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3687 } else {
3688 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3689 }
3690 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003691 if (mDeviceLightIdling != nowLightIdling) {
3692 mDeviceLightIdling = nowLightIdling;
3693 if (nowLightIdling) {
3694 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003695 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003696 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003697 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003698 }
3699 if (mDeviceIdleMode != mode) {
3700 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
3701 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
3702 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
3703 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003704 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003705 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
3706 mLastIdleTimeStart = elapsedRealtime;
3707 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
3708 if (lastDuration > mLongestLightIdleTime) {
3709 mLongestLightIdleTime = lastDuration;
3710 }
3711 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003712 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003713 if (lastDuration > mLongestFullIdleTime) {
3714 mLongestFullIdleTime = lastDuration;
3715 }
3716 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3717 }
3718 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3719 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003720 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003721 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
3722 }
3723 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003724 }
3725 }
3726
3727 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08003728 final long elapsedRealtime = mClocks.elapsedRealtime();
3729 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003730 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3731 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003732 PackageChange pc = new PackageChange();
3733 pc.mPackageName = pkgName;
3734 pc.mUpdate = true;
3735 pc.mVersionCode = versionCode;
3736 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003737 }
3738
3739 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08003740 final long elapsedRealtime = mClocks.elapsedRealtime();
3741 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003742 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3743 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003744 PackageChange pc = new PackageChange();
3745 pc.mPackageName = pkgName;
3746 pc.mUpdate = true;
3747 addPackageChange(pc);
3748 }
3749
3750 private void addPackageChange(PackageChange pc) {
3751 if (mDailyPackageChanges == null) {
3752 mDailyPackageChanges = new ArrayList<>();
3753 }
3754 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003755 }
3756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 public void notePhoneOnLocked() {
3758 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003759 final long elapsedRealtime = mClocks.elapsedRealtime();
3760 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003761 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003762 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3763 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003764 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003766 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 }
3768 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 public void notePhoneOffLocked() {
3771 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003772 final long elapsedRealtime = mClocks.elapsedRealtime();
3773 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003774 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003775 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3776 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003777 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003779 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 }
3781 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003782
Dianne Hackborn3251b902014-06-20 14:40:53 -07003783 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08003784 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003785 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003786 if (i == except) {
3787 continue;
3788 }
3789 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003790 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003791 }
3792 }
3793 }
3794
Dianne Hackborne4a59512010-12-07 11:08:07 -08003795 private int fixPhoneServiceState(int state, int signalBin) {
3796 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3797 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3798 // to infer that we are scanning from other data.
3799 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003800 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003801 state = ServiceState.STATE_IN_SERVICE;
3802 }
3803 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003804
Dianne Hackborne4a59512010-12-07 11:08:07 -08003805 return state;
3806 }
3807
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003808 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003809 boolean scanning = false;
3810 boolean newHistory = false;
3811
3812 mPhoneServiceStateRaw = state;
3813 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003814 mPhoneSignalStrengthBinRaw = strengthBin;
3815
Joe Onoratoabded112016-02-08 16:49:39 -08003816 final long elapsedRealtime = mClocks.elapsedRealtime();
3817 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003818
3819 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3820 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3821 // to infer that we are scanning from other data.
3822 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003823 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003824 state = ServiceState.STATE_IN_SERVICE;
3825 }
3826 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003827
3828 // If the phone is powered off, stop all timers.
3829 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003830 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003831
Dianne Hackborne4a59512010-12-07 11:08:07 -08003832 // If we are in service, make sure the correct signal string timer is running.
3833 } else if (state == ServiceState.STATE_IN_SERVICE) {
3834 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003835
3836 // If we're out of service, we are in the lowest signal strength
3837 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003838 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003839 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003840 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003841 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003842 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003843 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003844 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3845 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003846 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003847 }
3848 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003849
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003850 if (!scanning) {
3851 // If we are no longer scanning, then stop the scanning timer.
3852 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3853 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3854 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3855 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003856 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003857 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003858 }
3859 }
3860
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003861 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003862 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3863 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003864 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003865 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003866 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003867 mPhoneServiceState = state;
3868 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003869
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003870 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003871 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003872 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003873 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003874 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003875 if (strengthBin >= 0) {
3876 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003877 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003878 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003879 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3880 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003881 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003882 + Integer.toHexString(mHistoryCur.states));
3883 newHistory = true;
3884 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003885 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003886 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003887 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003888 }
3889
3890 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003891 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003892 }
3893 }
3894
3895 /**
3896 * Telephony stack updates the phone state.
3897 * @param state phone state from ServiceState.getState()
3898 */
3899 public void notePhoneStateLocked(int state, int simState) {
3900 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003901 }
3902
Wink Savillee9b06d72009-05-18 21:47:50 -07003903 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003904 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003905 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003906 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003907 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003908
Dianne Hackborn627bba72009-03-24 22:32:56 -07003909 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3910 int bin = DATA_CONNECTION_NONE;
3911 if (hasData) {
3912 switch (dataType) {
3913 case TelephonyManager.NETWORK_TYPE_EDGE:
3914 bin = DATA_CONNECTION_EDGE;
3915 break;
3916 case TelephonyManager.NETWORK_TYPE_GPRS:
3917 bin = DATA_CONNECTION_GPRS;
3918 break;
3919 case TelephonyManager.NETWORK_TYPE_UMTS:
3920 bin = DATA_CONNECTION_UMTS;
3921 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003922 case TelephonyManager.NETWORK_TYPE_CDMA:
3923 bin = DATA_CONNECTION_CDMA;
3924 break;
3925 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3926 bin = DATA_CONNECTION_EVDO_0;
3927 break;
3928 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3929 bin = DATA_CONNECTION_EVDO_A;
3930 break;
3931 case TelephonyManager.NETWORK_TYPE_1xRTT:
3932 bin = DATA_CONNECTION_1xRTT;
3933 break;
3934 case TelephonyManager.NETWORK_TYPE_HSDPA:
3935 bin = DATA_CONNECTION_HSDPA;
3936 break;
3937 case TelephonyManager.NETWORK_TYPE_HSUPA:
3938 bin = DATA_CONNECTION_HSUPA;
3939 break;
3940 case TelephonyManager.NETWORK_TYPE_HSPA:
3941 bin = DATA_CONNECTION_HSPA;
3942 break;
3943 case TelephonyManager.NETWORK_TYPE_IDEN:
3944 bin = DATA_CONNECTION_IDEN;
3945 break;
3946 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3947 bin = DATA_CONNECTION_EVDO_B;
3948 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003949 case TelephonyManager.NETWORK_TYPE_LTE:
3950 bin = DATA_CONNECTION_LTE;
3951 break;
3952 case TelephonyManager.NETWORK_TYPE_EHRPD:
3953 bin = DATA_CONNECTION_EHRPD;
3954 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003955 case TelephonyManager.NETWORK_TYPE_HSPAP:
3956 bin = DATA_CONNECTION_HSPAP;
3957 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003958 default:
3959 bin = DATA_CONNECTION_OTHER;
3960 break;
3961 }
3962 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003963 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003964 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003965 final long elapsedRealtime = mClocks.elapsedRealtime();
3966 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003967 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3968 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003969 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3970 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003971 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003972 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003973 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003974 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003975 }
3976 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003977 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003978 }
3979 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003980
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003981 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003982 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003983 final long elapsedRealtime = mClocks.elapsedRealtime();
3984 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003985 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003986 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3987 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003988 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003989 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003990 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003991 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003992 }
3993 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003994
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003995 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08003996 final long elapsedRealtime = mClocks.elapsedRealtime();
3997 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003998 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003999 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004000 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4001 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004002 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004003 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004004 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004005 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004006 }
4007 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004008
4009 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004010 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004011 final long elapsedRealtime = mClocks.elapsedRealtime();
4012 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004013 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004014 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004015 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4016 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004017 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004018 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004019 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004020 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004021 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004022 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004023
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004024 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004025 if (mAudioOnNesting == 0) {
4026 return;
4027 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004028 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004029 final long elapsedRealtime = mClocks.elapsedRealtime();
4030 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004031 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004032 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004033 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4034 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004035 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004036 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004037 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004038 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004039 }
4040
4041 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004042 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004043 final long elapsedRealtime = mClocks.elapsedRealtime();
4044 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004045 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004046 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004047 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4048 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004049 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004050 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004051 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004052 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004053 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004054 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004055
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004056 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004057 if (mVideoOnNesting == 0) {
4058 return;
4059 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004060 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004061 final long elapsedRealtime = mClocks.elapsedRealtime();
4062 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004063 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004064 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004065 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4066 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004067 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004068 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004069 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004070 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004071 }
4072
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004073 public void noteResetAudioLocked() {
4074 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004075 final long elapsedRealtime = mClocks.elapsedRealtime();
4076 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004077 mAudioOnNesting = 0;
4078 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4079 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4080 + Integer.toHexString(mHistoryCur.states));
4081 addHistoryRecordLocked(elapsedRealtime, uptime);
4082 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4083 for (int i=0; i<mUidStats.size(); i++) {
4084 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4085 uid.noteResetAudioLocked(elapsedRealtime);
4086 }
4087 }
4088 }
4089
4090 public void noteResetVideoLocked() {
4091 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004092 final long elapsedRealtime = mClocks.elapsedRealtime();
4093 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004094 mAudioOnNesting = 0;
4095 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4096 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4097 + Integer.toHexString(mHistoryCur.states));
4098 addHistoryRecordLocked(elapsedRealtime, uptime);
4099 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4100 for (int i=0; i<mUidStats.size(); i++) {
4101 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4102 uid.noteResetVideoLocked(elapsedRealtime);
4103 }
4104 }
4105 }
4106
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004107 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004108 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004109 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004110 }
4111
4112 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004113 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004114 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004115 }
4116
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004117 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004118 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004119 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4120 }
4121
4122 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004123 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004124 getUidStatsLocked(uid).noteVibratorOffLocked();
4125 }
4126
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004127 public void noteFlashlightOnLocked(int uid) {
4128 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004129 final long elapsedRealtime = mClocks.elapsedRealtime();
4130 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004131 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004132 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4133 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004134 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004135 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004136 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4137 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004138 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4139 }
4140
4141 public void noteFlashlightOffLocked(int uid) {
4142 if (mFlashlightOnNesting == 0) {
4143 return;
4144 }
4145 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004146 final long elapsedRealtime = mClocks.elapsedRealtime();
4147 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004148 if (--mFlashlightOnNesting == 0) {
4149 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4150 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4151 + Integer.toHexString(mHistoryCur.states2));
4152 addHistoryRecordLocked(elapsedRealtime, uptime);
4153 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4154 }
4155 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4156 }
4157
4158 public void noteCameraOnLocked(int uid) {
4159 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004160 final long elapsedRealtime = mClocks.elapsedRealtime();
4161 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004162 if (mCameraOnNesting++ == 0) {
4163 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4164 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4165 + Integer.toHexString(mHistoryCur.states2));
4166 addHistoryRecordLocked(elapsedRealtime, uptime);
4167 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4168 }
4169 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4170 }
4171
4172 public void noteCameraOffLocked(int uid) {
4173 if (mCameraOnNesting == 0) {
4174 return;
4175 }
4176 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004177 final long elapsedRealtime = mClocks.elapsedRealtime();
4178 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004179 if (--mCameraOnNesting == 0) {
4180 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4181 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4182 + Integer.toHexString(mHistoryCur.states2));
4183 addHistoryRecordLocked(elapsedRealtime, uptime);
4184 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4185 }
4186 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4187 }
4188
4189 public void noteResetCameraLocked() {
4190 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004191 final long elapsedRealtime = mClocks.elapsedRealtime();
4192 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004193 mCameraOnNesting = 0;
4194 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4195 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4196 + Integer.toHexString(mHistoryCur.states2));
4197 addHistoryRecordLocked(elapsedRealtime, uptime);
4198 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4199 for (int i=0; i<mUidStats.size(); i++) {
4200 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4201 uid.noteResetCameraLocked(elapsedRealtime);
4202 }
4203 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004204 }
4205
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004206 public void noteResetFlashlightLocked() {
4207 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004208 final long elapsedRealtime = mClocks.elapsedRealtime();
4209 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004210 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004211 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4212 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004213 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004214 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004215 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4216 for (int i=0; i<mUidStats.size(); i++) {
4217 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4218 uid.noteResetFlashlightLocked(elapsedRealtime);
4219 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004220 }
4221 }
4222
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004223 private void noteBluetoothScanStartedLocked(int uid) {
4224 uid = mapUid(uid);
4225 final long elapsedRealtime = SystemClock.elapsedRealtime();
4226 final long uptime = SystemClock.uptimeMillis();
4227 if (mBluetoothScanNesting == 0) {
4228 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4229 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4230 + Integer.toHexString(mHistoryCur.states2));
4231 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004232 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004233 }
4234 mBluetoothScanNesting++;
4235 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4236 }
4237
4238 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4239 final int N = ws.size();
4240 for (int i = 0; i < N; i++) {
4241 noteBluetoothScanStartedLocked(ws.get(i));
4242 }
4243 }
4244
4245 private void noteBluetoothScanStoppedLocked(int uid) {
4246 uid = mapUid(uid);
4247 final long elapsedRealtime = SystemClock.elapsedRealtime();
4248 final long uptime = SystemClock.uptimeMillis();
4249 mBluetoothScanNesting--;
4250 if (mBluetoothScanNesting == 0) {
4251 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4252 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4253 + Integer.toHexString(mHistoryCur.states2));
4254 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004255 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004256 }
4257 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4258 }
4259
4260 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4261 final int N = ws.size();
4262 for (int i = 0; i < N; i++) {
4263 noteBluetoothScanStoppedLocked(ws.get(i));
4264 }
4265 }
4266
4267 public void noteResetBluetoothScanLocked() {
4268 if (mBluetoothScanNesting > 0) {
4269 final long elapsedRealtime = SystemClock.elapsedRealtime();
4270 final long uptime = SystemClock.uptimeMillis();
4271 mBluetoothScanNesting = 0;
4272 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4273 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4274 + Integer.toHexString(mHistoryCur.states2));
4275 addHistoryRecordLocked(elapsedRealtime, uptime);
4276 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4277 for (int i=0; i<mUidStats.size(); i++) {
4278 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4279 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4280 }
4281 }
4282 }
4283
Adam Lesinski5f056f62016-07-14 16:56:08 -07004284 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4285 final long uptimeMillis, int uid) {
4286 uid = mapUid(uid);
4287 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4288 uid);
4289 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4290 }
4291
4292 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004293 final long elapsedRealtime = mClocks.elapsedRealtime();
4294 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004295 if (mWifiRadioPowerState != powerState) {
4296 final boolean active =
4297 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4298 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4299 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004300 if (uid > 0) {
4301 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4302 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004303 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4304 } else {
4305 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4306 }
4307 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4308 + Integer.toHexString(mHistoryCur.states));
4309 addHistoryRecordLocked(elapsedRealtime, uptime);
4310 mWifiRadioPowerState = powerState;
4311 }
4312 }
4313
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004314 public void noteWifiRunningLocked(WorkSource ws) {
4315 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004316 final long elapsedRealtime = mClocks.elapsedRealtime();
4317 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004318 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004319 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4320 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004321 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004322 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004323 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004324 int N = ws.size();
4325 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004326 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004327 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004328 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004329 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004330 } else {
4331 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004332 }
4333 }
4334
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004335 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4336 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004337 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004338 int N = oldWs.size();
4339 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004340 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004341 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004342 }
4343 N = newWs.size();
4344 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004345 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004346 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004347 }
4348 } else {
4349 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4350 }
4351 }
4352
4353 public void noteWifiStoppedLocked(WorkSource ws) {
4354 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004355 final long elapsedRealtime = mClocks.elapsedRealtime();
4356 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004357 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004358 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4359 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004360 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004361 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004362 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004363 int N = ws.size();
4364 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004365 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004366 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004367 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004368 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004369 } else {
4370 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004371 }
4372 }
4373
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004374 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4375 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4376 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004377 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004378 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004379 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004380 }
4381 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004382 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004383 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004384 }
4385 }
4386
Dianne Hackborn3251b902014-06-20 14:40:53 -07004387 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4388 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4389 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004390 final long elapsedRealtime = mClocks.elapsedRealtime();
4391 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004392 if (mWifiSupplState >= 0) {
4393 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4394 }
4395 mWifiSupplState = supplState;
4396 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4397 mHistoryCur.states2 =
4398 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4399 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4400 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4401 + Integer.toHexString(mHistoryCur.states2));
4402 addHistoryRecordLocked(elapsedRealtime, uptime);
4403 }
4404 }
4405
4406 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004407 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004408 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4409 if (i == except) {
4410 continue;
4411 }
4412 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4413 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4414 }
4415 }
4416 }
4417
4418 public void noteWifiRssiChangedLocked(int newRssi) {
4419 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4420 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4421 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004422 final long elapsedRealtime = mClocks.elapsedRealtime();
4423 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004424 if (mWifiSignalStrengthBin >= 0) {
4425 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4426 elapsedRealtime);
4427 }
4428 if (strengthBin >= 0) {
4429 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4430 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4431 }
4432 mHistoryCur.states2 =
4433 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4434 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4435 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4436 + Integer.toHexString(mHistoryCur.states2));
4437 addHistoryRecordLocked(elapsedRealtime, uptime);
4438 } else {
4439 stopAllWifiSignalStrengthTimersLocked(-1);
4440 }
4441 mWifiSignalStrengthBin = strengthBin;
4442 }
4443 }
4444
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004445 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004446
The Android Open Source Project10592532009-03-18 17:39:46 -07004447 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004448 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004449 final long elapsedRealtime = mClocks.elapsedRealtime();
4450 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004451 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004452 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004453 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4454 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004455 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004456 }
4457 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004458 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004459 }
4460
4461 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004462 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004463 final long elapsedRealtime = mClocks.elapsedRealtime();
4464 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004465 mWifiFullLockNesting--;
4466 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004467 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004468 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4469 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004470 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004471 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004472 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004473 }
4474
Nick Pelly6ccaa542012-06-15 15:22:47 -07004475 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004476
Nick Pelly6ccaa542012-06-15 15:22:47 -07004477 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004478 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004479 final long elapsedRealtime = mClocks.elapsedRealtime();
4480 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004481 if (mWifiScanNesting == 0) {
4482 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4483 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004484 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004485 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004486 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004487 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004488 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004489 }
4490
Nick Pelly6ccaa542012-06-15 15:22:47 -07004491 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004492 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004493 final long elapsedRealtime = mClocks.elapsedRealtime();
4494 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004495 mWifiScanNesting--;
4496 if (mWifiScanNesting == 0) {
4497 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4498 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004499 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004500 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004501 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004502 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004503 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004504
Robert Greenwalta029ea12013-09-25 16:38:12 -07004505 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004506 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004507 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004508 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004509 }
4510
4511 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004512 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004513 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004514 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004515 }
4516
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004517 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004518
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004519 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004520 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004521 final long elapsedRealtime = mClocks.elapsedRealtime();
4522 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004523 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004524 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004525 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4526 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004527 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004528 }
4529 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004530 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004531 }
4532
4533 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004534 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004535 final long elapsedRealtime = mClocks.elapsedRealtime();
4536 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004537 mWifiMulticastNesting--;
4538 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004539 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004540 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4541 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004542 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004543 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004544 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004545 }
4546
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004547 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4548 int N = ws.size();
4549 for (int i=0; i<N; i++) {
4550 noteFullWifiLockAcquiredLocked(ws.get(i));
4551 }
4552 }
4553
4554 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4555 int N = ws.size();
4556 for (int i=0; i<N; i++) {
4557 noteFullWifiLockReleasedLocked(ws.get(i));
4558 }
4559 }
4560
Nick Pelly6ccaa542012-06-15 15:22:47 -07004561 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004562 int N = ws.size();
4563 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004564 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004565 }
4566 }
4567
Nick Pelly6ccaa542012-06-15 15:22:47 -07004568 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004569 int N = ws.size();
4570 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004571 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004572 }
4573 }
4574
Robert Greenwalta029ea12013-09-25 16:38:12 -07004575 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4576 int N = ws.size();
4577 for (int i=0; i<N; i++) {
4578 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4579 }
4580 }
4581
4582 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4583 int N = ws.size();
4584 for (int i=0; i<N; i++) {
4585 noteWifiBatchedScanStoppedLocked(ws.get(i));
4586 }
4587 }
4588
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004589 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4590 int N = ws.size();
4591 for (int i=0; i<N; i++) {
4592 noteWifiMulticastEnabledLocked(ws.get(i));
4593 }
4594 }
4595
4596 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4597 int N = ws.size();
4598 for (int i=0; i<N; i++) {
4599 noteWifiMulticastDisabledLocked(ws.get(i));
4600 }
4601 }
4602
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004603 private static String[] includeInStringArray(String[] array, String str) {
4604 if (ArrayUtils.indexOf(array, str) >= 0) {
4605 return array;
4606 }
4607 String[] newArray = new String[array.length+1];
4608 System.arraycopy(array, 0, newArray, 0, array.length);
4609 newArray[array.length] = str;
4610 return newArray;
4611 }
4612
4613 private static String[] excludeFromStringArray(String[] array, String str) {
4614 int index = ArrayUtils.indexOf(array, str);
4615 if (index >= 0) {
4616 String[] newArray = new String[array.length-1];
4617 if (index > 0) {
4618 System.arraycopy(array, 0, newArray, 0, index);
4619 }
4620 if (index < array.length-1) {
4621 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4622 }
4623 return newArray;
4624 }
4625 return array;
4626 }
4627
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004628 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004629 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004630 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004631 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4632 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004633 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004634 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4635 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004636 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004637 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004638 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4639 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004640 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004641 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4642 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004643 }
4644 }
4645
4646 public void noteNetworkStatsEnabledLocked() {
4647 // During device boot, qtaguid isn't enabled until after the inital
4648 // loading of battery stats. Now that they're enabled, take our initial
4649 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08004650 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004651 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004652 }
4653
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004654 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4655 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004657
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004658 @Override public int getScreenOnCount(int which) {
4659 return mScreenOnTimer.getCountLocked(which);
4660 }
4661
Dianne Hackborn617f8772009-03-31 15:04:46 -07004662 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004663 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004664 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004665 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004666 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004667
Jeff Browne95c3cd2014-05-02 16:59:26 -07004668 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4669 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004670 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004671
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004672 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4673 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004674 }
4675
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004676 @Override public int getPowerSaveModeEnabledCount(int which) {
4677 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4678 }
4679
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004680 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
4681 int which) {
4682 switch (mode) {
4683 case DEVICE_IDLE_MODE_LIGHT:
4684 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004685 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004686 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4687 }
4688 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004689 }
4690
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004691 @Override public int getDeviceIdleModeCount(int mode, int which) {
4692 switch (mode) {
4693 case DEVICE_IDLE_MODE_LIGHT:
4694 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004695 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004696 return mDeviceIdleModeFullTimer.getCountLocked(which);
4697 }
4698 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004699 }
4700
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004701 @Override public long getLongestDeviceIdleModeTime(int mode) {
4702 switch (mode) {
4703 case DEVICE_IDLE_MODE_LIGHT:
4704 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004705 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004706 return mLongestFullIdleTime;
4707 }
4708 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004709 }
4710
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004711 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
4712 switch (mode) {
4713 case DEVICE_IDLE_MODE_LIGHT:
4714 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004715 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004716 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4717 }
4718 return 0;
4719 }
4720
4721 @Override public int getDeviceIdlingCount(int mode, int which) {
4722 switch (mode) {
4723 case DEVICE_IDLE_MODE_LIGHT:
4724 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004725 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004726 return mDeviceIdlingTimer.getCountLocked(which);
4727 }
4728 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004729 }
4730
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004731 @Override public int getNumConnectivityChange(int which) {
4732 int val = mNumConnectivityChange;
4733 if (which == STATS_CURRENT) {
4734 val -= mLoadedNumConnectivityChange;
4735 } else if (which == STATS_SINCE_UNPLUGGED) {
4736 val -= mUnpluggedNumConnectivityChange;
4737 }
4738 return val;
4739 }
4740
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004741 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4742 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004744
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004745 @Override public int getPhoneOnCount(int which) {
4746 return mPhoneOnTimer.getCountLocked(which);
4747 }
4748
Dianne Hackborn627bba72009-03-24 22:32:56 -07004749 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004750 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004751 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004752 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004753 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004754
4755 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004756 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004757 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004758 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004759 }
4760
Catherine Liufb900812012-07-17 14:12:56 -05004761 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4762 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004763 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004764
Dianne Hackborn627bba72009-03-24 22:32:56 -07004765 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004766 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004767 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004768 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004769 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004770
Dianne Hackborn617f8772009-03-31 15:04:46 -07004771 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004772 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004773 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004774
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004775 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4776 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004777 }
4778
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004779 @Override public int getMobileRadioActiveCount(int which) {
4780 return mMobileRadioActiveTimer.getCountLocked(which);
4781 }
4782
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004783 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4784 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4785 }
4786
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004787 @Override public long getMobileRadioActiveUnknownTime(int which) {
4788 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4789 }
4790
4791 @Override public int getMobileRadioActiveUnknownCount(int which) {
4792 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4793 }
4794
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004795 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4796 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004797 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004798
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004799 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4800 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004801 }
4802
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004803 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004804 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004805 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004806 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004807 }
4808
4809 @Override public int getWifiStateCount(int wifiState, int which) {
4810 return mWifiStateTimer[wifiState].getCountLocked(which);
4811 }
4812
Dianne Hackborn3251b902014-06-20 14:40:53 -07004813 @Override public long getWifiSupplStateTime(int state,
4814 long elapsedRealtimeUs, int which) {
4815 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4816 elapsedRealtimeUs, which);
4817 }
4818
4819 @Override public int getWifiSupplStateCount(int state, int which) {
4820 return mWifiSupplStateTimer[state].getCountLocked(which);
4821 }
4822
4823 @Override public long getWifiSignalStrengthTime(int strengthBin,
4824 long elapsedRealtimeUs, int which) {
4825 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4826 elapsedRealtimeUs, which);
4827 }
4828
4829 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4830 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4831 }
4832
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004833 @Override
4834 public ControllerActivityCounter getBluetoothControllerActivity() {
4835 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004836 }
4837
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004838 @Override
4839 public ControllerActivityCounter getWifiControllerActivity() {
4840 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07004841 }
4842
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004843 @Override
4844 public ControllerActivityCounter getModemControllerActivity() {
4845 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004846 }
4847
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004848 @Override
4849 public boolean hasBluetoothActivityReporting() {
4850 return mHasBluetoothReporting;
4851 }
4852
4853 @Override
4854 public boolean hasWifiActivityReporting() {
4855 return mHasWifiReporting;
4856 }
4857
4858 @Override
4859 public boolean hasModemActivityReporting() {
4860 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07004861 }
4862
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004863 @Override
4864 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004865 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4866 }
4867
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004868 @Override
4869 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004870 return mFlashlightOnTimer.getCountLocked(which);
4871 }
4872
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004873 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004874 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4875 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4876 }
4877
4878 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004879 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
4880 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4881 }
4882
4883 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004884 public long getNetworkActivityBytes(int type, int which) {
4885 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4886 return mNetworkByteActivityCounters[type].getCountLocked(which);
4887 } else {
4888 return 0;
4889 }
4890 }
4891
4892 @Override
4893 public long getNetworkActivityPackets(int type, int which) {
4894 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4895 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004896 } else {
4897 return 0;
4898 }
4899 }
4900
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004901 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07004902 final long currentTime = System.currentTimeMillis();
4903 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08004904 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
4905 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004906 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004907 return mStartClockTime;
4908 }
4909
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004910 @Override public String getStartPlatformVersion() {
4911 return mStartPlatformVersion;
4912 }
4913
4914 @Override public String getEndPlatformVersion() {
4915 return mEndPlatformVersion;
4916 }
4917
4918 @Override public int getParcelVersion() {
4919 return VERSION;
4920 }
4921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 @Override public boolean getIsOnBattery() {
4923 return mOnBattery;
4924 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4927 return mUidStats;
4928 }
4929
Adam Lesinski5f056f62016-07-14 16:56:08 -07004930 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
4931 if (timer != null) {
4932 timer.detach();
4933 }
4934 }
4935
4936 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
4937 boolean detachIfReset) {
4938 if (timer != null) {
4939 return timer.reset(detachIfReset);
4940 }
4941 return true;
4942 }
4943
4944 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
4945 if (counter != null) {
4946 counter.detach();
4947 }
4948 }
4949
4950 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
4951 boolean detachIfReset) {
4952 if (counter != null) {
4953 counter.reset(detachIfReset);
4954 }
4955 }
4956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957 /**
4958 * The statistics associated with a particular uid.
4959 */
Joe Onoratoabded112016-02-08 16:49:39 -08004960 public static class Uid extends BatteryStats.Uid {
4961 /**
4962 * BatteryStatsImpl that we are associated with.
4963 */
4964 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004967
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004968 boolean mWifiRunning;
4969 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004970
The Android Open Source Project10592532009-03-18 17:39:46 -07004971 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07004972 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004973
Nick Pelly6ccaa542012-06-15 15:22:47 -07004974 boolean mWifiScanStarted;
4975 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004976
Dianne Hackborn61659e52014-07-09 16:13:01 -07004977 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07004978 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4979 StopwatchTimer[] mWifiBatchedScanTimer;
4980
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004981 boolean mWifiMulticastEnabled;
4982 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004983
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004984 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004985 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004986 StopwatchTimer mFlashlightTurnedOnTimer;
4987 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004988 StopwatchTimer mForegroundActivityTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004989 StopwatchTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004990
Dianne Hackborna8d10942015-11-19 17:55:19 -08004991 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004992 StopwatchTimer[] mProcessStateTimer;
4993
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004994 BatchTimer mVibratorOnTimer;
4995
Dianne Hackborn617f8772009-03-31 15:04:46 -07004996 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004997
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004998 LongSamplingCounter[] mNetworkByteActivityCounters;
4999 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005000 LongSamplingCounter mMobileRadioActiveTime;
5001 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005004 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5005 */
5006 private LongSamplingCounter mMobileRadioApWakeupCount;
5007
5008 /**
5009 * How many times this UID woke up the Application Processor due to a Wifi packet.
5010 */
5011 private LongSamplingCounter mWifiRadioApWakeupCount;
5012
5013 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005014 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005015 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005016 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005017 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005018
5019 /**
5020 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005021 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005022 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005023 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5024
5025 /**
5026 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5027 * Can be null if the UID has had no such activity.
5028 */
5029 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005030
5031 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005032 * The CPU times we had at the last history details update.
5033 */
5034 long mLastStepUserTime;
5035 long mLastStepSystemTime;
5036 long mCurStepUserTime;
5037 long mCurStepSystemTime;
5038
Joe Onoratoabded112016-02-08 16:49:39 -08005039 LongSamplingCounter mUserCpuTime;
5040 LongSamplingCounter mSystemCpuTime;
5041 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07005042 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005043
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 * The statistics we have collected for this uid's wake locks.
5046 */
Joe Onoratoabded112016-02-08 16:49:39 -08005047 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048
5049 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005050 * The statistics we have collected for this uid's syncs.
5051 */
Joe Onoratoabded112016-02-08 16:49:39 -08005052 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005053
5054 /**
5055 * The statistics we have collected for this uid's jobs.
5056 */
Joe Onoratoabded112016-02-08 16:49:39 -08005057 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005058
5059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 * The statistics we have collected for this uid's sensor activations.
5061 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005062 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063
5064 /**
5065 * The statistics we have collected for this uid's processes.
5066 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005067 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068
5069 /**
5070 * The statistics we have collected for this uid's processes.
5071 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005072 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005073
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005074 /**
5075 * The transient wake stats we have collected for this uid's pids.
5076 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005077 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005078
Joe Onoratoabded112016-02-08 16:49:39 -08005079 public Uid(BatteryStatsImpl bsi, int uid) {
5080 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005082
5083 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5084 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5085 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5086
5087 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>() {
5088 @Override public Wakelock instantiateObject() {
5089 return new Wakelock(mBsi, Uid.this);
5090 }
5091 };
5092 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
5093 @Override public StopwatchTimer instantiateObject() {
5094 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
5095 mBsi.mOnBatteryTimeBase);
5096 }
5097 };
5098 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
5099 @Override public StopwatchTimer instantiateObject() {
5100 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5101 mBsi.mOnBatteryTimeBase);
5102 }
5103 };
5104
5105 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5106 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5107 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5108 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
5109 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_SCAN,
5110 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005111 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005112 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5113 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005114 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 }
5116
5117 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005118 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005119 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 }
5121
5122 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005123 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005124 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005125 }
5126
5127 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005128 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005129 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005130 }
5131
5132 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005133 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 return mSensorStats;
5135 }
5136
5137 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005138 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 return mProcessStats;
5140 }
5141
5142 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005143 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 return mPackageStats;
5145 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005146
5147 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 public int getUid() {
5149 return mUid;
5150 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005151
5152 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005153 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005154 if (!mWifiRunning) {
5155 mWifiRunning = true;
5156 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005157 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5158 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005159 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005160 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005161 }
5162 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005163
Dianne Hackborn617f8772009-03-31 15:04:46 -07005164 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005165 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005166 if (mWifiRunning) {
5167 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005168 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005169 }
5170 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005171
Dianne Hackborn617f8772009-03-31 15:04:46 -07005172 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005173 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005174 if (!mFullWifiLockOut) {
5175 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005176 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005177 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5178 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005179 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005180 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005181 }
5182 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005183
The Android Open Source Project10592532009-03-18 17:39:46 -07005184 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005185 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005186 if (mFullWifiLockOut) {
5187 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005188 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005189 }
5190 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005191
The Android Open Source Project10592532009-03-18 17:39:46 -07005192 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005193 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005194 if (!mWifiScanStarted) {
5195 mWifiScanStarted = true;
5196 if (mWifiScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005197 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5198 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005199 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005200 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005201 }
5202 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005203
The Android Open Source Project10592532009-03-18 17:39:46 -07005204 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005205 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005206 if (mWifiScanStarted) {
5207 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005208 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005209 }
5210 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005211
5212 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005213 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005214 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005215 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005216 csph = csph >> 3;
5217 bin++;
5218 }
5219
5220 if (mWifiBatchedScanBinStarted == bin) return;
5221
5222 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5223 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005224 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005225 }
5226 mWifiBatchedScanBinStarted = bin;
5227 if (mWifiBatchedScanTimer[bin] == null) {
5228 makeWifiBatchedScanBin(bin, null);
5229 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005230 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005231 }
5232
5233 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005234 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005235 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5236 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005237 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005238 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5239 }
5240 }
5241
5242 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005243 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005244 if (!mWifiMulticastEnabled) {
5245 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005246 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005247 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5248 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005249 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005250 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005251 }
5252 }
5253
5254 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005255 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005256 if (mWifiMulticastEnabled) {
5257 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005258 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005259 }
5260 }
5261
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005262 @Override
5263 public ControllerActivityCounter getWifiControllerActivity() {
5264 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005265 }
5266
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005267 @Override
5268 public ControllerActivityCounter getBluetoothControllerActivity() {
5269 return mBluetoothControllerActivity;
5270 }
5271
5272 @Override
5273 public ControllerActivityCounter getModemControllerActivity() {
5274 return mModemControllerActivity;
5275 }
5276
5277 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5278 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005279 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005280 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005281 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005282 return mWifiControllerActivity;
5283 }
5284
5285 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5286 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005287 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005288 NUM_BT_TX_LEVELS);
5289 }
5290 return mBluetoothControllerActivity;
5291 }
5292
5293 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5294 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005295 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005296 ModemActivityInfo.TX_POWER_LEVELS);
5297 }
5298 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005299 }
5300
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005301 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5302 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005303 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5304 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005305 }
5306 return mAudioTurnedOnTimer;
5307 }
5308
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005309 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005310 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5311 }
5312
5313 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5314 if (mAudioTurnedOnTimer != null) {
5315 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005316 }
5317 }
5318
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005319 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5320 if (mAudioTurnedOnTimer != null) {
5321 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005322 }
5323 }
5324
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005325 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5326 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005327 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5328 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005329 }
5330 return mVideoTurnedOnTimer;
5331 }
5332
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005333 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005334 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5335 }
5336
5337 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5338 if (mVideoTurnedOnTimer != null) {
5339 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005340 }
5341 }
5342
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005343 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5344 if (mVideoTurnedOnTimer != null) {
5345 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005346 }
5347 }
5348
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005349 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5350 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005351 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5352 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005353 }
5354 return mFlashlightTurnedOnTimer;
5355 }
5356
5357 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5358 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5359 }
5360
5361 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5362 if (mFlashlightTurnedOnTimer != null) {
5363 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5364 }
5365 }
5366
5367 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5368 if (mFlashlightTurnedOnTimer != null) {
5369 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5370 }
5371 }
5372
5373 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5374 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005375 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5376 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005377 }
5378 return mCameraTurnedOnTimer;
5379 }
5380
5381 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5382 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5383 }
5384
5385 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5386 if (mCameraTurnedOnTimer != null) {
5387 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5388 }
5389 }
5390
5391 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5392 if (mCameraTurnedOnTimer != null) {
5393 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5394 }
5395 }
5396
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005397 public StopwatchTimer createForegroundActivityTimerLocked() {
5398 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005399 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5400 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005401 }
5402 return mForegroundActivityTimer;
5403 }
5404
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005405 public StopwatchTimer createBluetoothScanTimerLocked() {
5406 if (mBluetoothScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005407 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5408 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005409 }
5410 return mBluetoothScanTimer;
5411 }
5412
5413 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5414 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5415 }
5416
5417 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5418 if (mBluetoothScanTimer != null) {
5419 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5420 }
5421 }
5422
5423 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5424 if (mBluetoothScanTimer != null) {
5425 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5426 }
5427 }
5428
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005429 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005430 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005431 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005432 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005433 }
5434
5435 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005436 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005437 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005438 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005439 }
5440 }
5441
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005442 public BatchTimer createVibratorOnTimerLocked() {
5443 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005444 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5445 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005446 }
5447 return mVibratorOnTimer;
5448 }
5449
5450 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005451 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005452 }
5453
5454 public void noteVibratorOffLocked() {
5455 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005456 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005457 }
5458 }
5459
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005460 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005461 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005462 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005463 return 0;
5464 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005465 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005466 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005467
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005468 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005469 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005470 if (mFullWifiLockTimer == null) {
5471 return 0;
5472 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005473 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005474 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005475
5476 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005477 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005478 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005479 return 0;
5480 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005481 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005482 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005483
5484 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005485 public int getWifiScanCount(int which) {
5486 if (mWifiScanTimer == null) {
5487 return 0;
5488 }
5489 return mWifiScanTimer.getCountLocked(which);
5490 }
5491
5492 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005493 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005494 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5495 if (mWifiBatchedScanTimer[csphBin] == null) {
5496 return 0;
5497 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005498 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005499 }
5500
5501 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005502 public int getWifiBatchedScanCount(int csphBin, int which) {
5503 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5504 if (mWifiBatchedScanTimer[csphBin] == null) {
5505 return 0;
5506 }
5507 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5508 }
5509
5510 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005511 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005512 if (mWifiMulticastTimer == null) {
5513 return 0;
5514 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005515 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005516 }
5517
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005518 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005519 public Timer getAudioTurnedOnTimer() {
5520 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005521 }
5522
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005523 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005524 public Timer getVideoTurnedOnTimer() {
5525 return mVideoTurnedOnTimer;
5526 }
5527
5528 @Override
5529 public Timer getFlashlightTurnedOnTimer() {
5530 return mFlashlightTurnedOnTimer;
5531 }
5532
5533 @Override
5534 public Timer getCameraTurnedOnTimer() {
5535 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005536 }
5537
Dianne Hackborn617f8772009-03-31 15:04:46 -07005538 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005539 public Timer getForegroundActivityTimer() {
5540 return mForegroundActivityTimer;
5541 }
5542
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005543 @Override
5544 public Timer getBluetoothScanTimer() {
5545 return mBluetoothScanTimer;
5546 }
5547
Dianne Hackborn61659e52014-07-09 16:13:01 -07005548 void makeProcessState(int i, Parcel in) {
5549 if (i < 0 || i >= NUM_PROCESS_STATE) return;
5550
5551 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005552 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5553 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005554 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005555 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5556 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005557 }
5558 }
5559
5560 @Override
5561 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
5562 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
5563 if (mProcessStateTimer[state] == null) {
5564 return 0;
5565 }
5566 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
5567 }
5568
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005569 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08005570 public Timer getProcessStateTimer(int state) {
5571 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
5572 return mProcessStateTimer[state];
5573 }
5574
5575 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005576 public Timer getVibratorOnTimer() {
5577 return mVibratorOnTimer;
5578 }
5579
5580 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005581 public void noteUserActivityLocked(int type) {
5582 if (mUserActivityCounters == null) {
5583 initUserActivityLocked();
5584 }
Jeff Browndf693de2012-07-27 12:03:38 -07005585 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5586 mUserActivityCounters[type].stepAtomic();
5587 } else {
5588 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5589 new Throwable());
5590 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005591 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005592
Dianne Hackborn617f8772009-03-31 15:04:46 -07005593 @Override
5594 public boolean hasUserActivity() {
5595 return mUserActivityCounters != null;
5596 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005597
Dianne Hackborn617f8772009-03-31 15:04:46 -07005598 @Override
5599 public int getUserActivityCount(int type, int which) {
5600 if (mUserActivityCounters == null) {
5601 return 0;
5602 }
Evan Millarc64edde2009-04-18 12:26:32 -07005603 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005604 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005605
Robert Greenwalta029ea12013-09-25 16:38:12 -07005606 void makeWifiBatchedScanBin(int i, Parcel in) {
5607 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5608
Joe Onoratoabded112016-02-08 16:49:39 -08005609 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005610 if (collected == null) {
5611 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005612 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005613 }
5614 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005615 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5616 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005617 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005618 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5619 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005620 }
5621 }
5622
5623
Dianne Hackborn617f8772009-03-31 15:04:46 -07005624 void initUserActivityLocked() {
5625 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5626 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005627 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005628 }
5629 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005630
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005631 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5632 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005633 initNetworkActivityLocked();
5634 }
5635 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005636 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5637 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005638 } else {
5639 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5640 new Throwable());
5641 }
5642 }
5643
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005644 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5645 if (mNetworkByteActivityCounters == null) {
5646 initNetworkActivityLocked();
5647 }
5648 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5649 mMobileRadioActiveCount.addCountLocked(1);
5650 }
5651
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005652 @Override
5653 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005654 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005655 }
5656
5657 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005658 public long getNetworkActivityBytes(int type, int which) {
5659 if (mNetworkByteActivityCounters != null && type >= 0
5660 && type < mNetworkByteActivityCounters.length) {
5661 return mNetworkByteActivityCounters[type].getCountLocked(which);
5662 } else {
5663 return 0;
5664 }
5665 }
5666
5667 @Override
5668 public long getNetworkActivityPackets(int type, int which) {
5669 if (mNetworkPacketActivityCounters != null && type >= 0
5670 && type < mNetworkPacketActivityCounters.length) {
5671 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005672 } else {
5673 return 0;
5674 }
5675 }
5676
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005677 @Override
5678 public long getMobileRadioActiveTime(int which) {
5679 return mMobileRadioActiveTime != null
5680 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5681 }
5682
5683 @Override
5684 public int getMobileRadioActiveCount(int which) {
5685 return mMobileRadioActiveCount != null
5686 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5687 }
5688
Adam Lesinskie08af192015-03-25 16:42:59 -07005689 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005690 public long getUserCpuTimeUs(int which) {
5691 return mUserCpuTime.getCountLocked(which);
5692 }
5693
5694 @Override
5695 public long getSystemCpuTimeUs(int which) {
5696 return mSystemCpuTime.getCountLocked(which);
5697 }
5698
5699 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005700 public long getCpuPowerMaUs(int which) {
5701 return mCpuPower.getCountLocked(which);
5702 }
5703
5704 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005705 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5706 if (mCpuClusterSpeed != null) {
5707 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5708 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5709 if (cpuSpeeds != null) {
5710 if (step >= 0 && step < cpuSpeeds.length) {
5711 final LongSamplingCounter c = cpuSpeeds[step];
5712 if (c != null) {
5713 return c.getCountLocked(which);
5714 }
5715 }
5716 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005717 }
5718 }
5719 return 0;
5720 }
5721
Adam Lesinski5f056f62016-07-14 16:56:08 -07005722 public void noteMobileRadioApWakeupLocked() {
5723 if (mMobileRadioApWakeupCount == null) {
5724 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5725 }
5726 mMobileRadioApWakeupCount.addCountLocked(1);
5727 }
5728
5729 @Override
5730 public long getMobileRadioApWakeupCount(int which) {
5731 if (mMobileRadioApWakeupCount != null) {
5732 return mMobileRadioApWakeupCount.getCountLocked(which);
5733 }
5734 return 0;
5735 }
5736
5737 public void noteWifiRadioApWakeupLocked() {
5738 if (mWifiRadioApWakeupCount == null) {
5739 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5740 }
5741 mWifiRadioApWakeupCount.addCountLocked(1);
5742 }
5743
5744 @Override
5745 public long getWifiRadioApWakeupCount(int which) {
5746 if (mWifiRadioApWakeupCount != null) {
5747 return mWifiRadioApWakeupCount.getCountLocked(which);
5748 }
5749 return 0;
5750 }
5751
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005752 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005753 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5754 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005755 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005756 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5757 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005758 }
Joe Onoratoabded112016-02-08 16:49:39 -08005759 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5760 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005762
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005763 /**
5764 * Clear all stats for this uid. Returns true if the uid is completely
5765 * inactive so can be dropped.
5766 */
5767 boolean reset() {
5768 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005769
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005770 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005771 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005772 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005773 }
5774 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005775 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005776 active |= mFullWifiLockOut;
5777 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005778 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005779 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005780 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005781 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005782 if (mWifiBatchedScanTimer != null) {
5783 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5784 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005785 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005786 }
5787 }
5788 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5789 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005790 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005791 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005792 active |= mWifiMulticastEnabled;
5793 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07005794
5795 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
5796 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
5797 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
5798 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
5799 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
5800 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
5801
Dianne Hackborn61659e52014-07-09 16:13:01 -07005802 if (mProcessStateTimer != null) {
5803 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5804 if (mProcessStateTimer[i] != null) {
5805 active |= !mProcessStateTimer[i].reset(false);
5806 }
5807 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005808 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005809 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005810 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005811 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005812 mVibratorOnTimer.detach();
5813 mVibratorOnTimer = null;
5814 } else {
5815 active = true;
5816 }
5817 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005818
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005819 if (mUserActivityCounters != null) {
5820 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5821 mUserActivityCounters[i].reset(false);
5822 }
5823 }
5824
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005825 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005826 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005827 mNetworkByteActivityCounters[i].reset(false);
5828 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005829 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005830 mMobileRadioActiveTime.reset(false);
5831 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005832 }
5833
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005834 if (mWifiControllerActivity != null) {
5835 mWifiControllerActivity.reset(false);
5836 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005837
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005838 if (mBluetoothControllerActivity != null) {
5839 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005840 }
5841
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005842 if (mModemControllerActivity != null) {
5843 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07005844 }
5845
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005846 mUserCpuTime.reset(false);
5847 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005848 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07005849
5850 if (mCpuClusterSpeed != null) {
5851 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
5852 if (speeds != null) {
5853 for (LongSamplingCounter speed : speeds) {
5854 if (speed != null) {
5855 speed.reset(false);
5856 }
5857 }
5858 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005859 }
5860 }
5861
Adam Lesinski5f056f62016-07-14 16:56:08 -07005862 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
5863 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
5864
Dianne Hackbornd953c532014-08-16 18:17:38 -07005865 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5866 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5867 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005868 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005869 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005870 } else {
5871 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005872 }
5873 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005874 mWakelockStats.cleanup();
5875 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5876 for (int is=syncStats.size()-1; is>=0; is--) {
5877 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005878 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005879 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005880 timer.detach();
5881 } else {
5882 active = true;
5883 }
5884 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005885 mSyncStats.cleanup();
5886 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5887 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5888 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005889 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005890 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005891 timer.detach();
5892 } else {
5893 active = true;
5894 }
5895 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005896 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005897 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5898 Sensor s = mSensorStats.valueAt(ise);
5899 if (s.reset()) {
5900 mSensorStats.removeAt(ise);
5901 } else {
5902 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005903 }
5904 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005905 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5906 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08005907 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005908 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005909 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005910 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005911 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005912 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005913 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005914 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005915 } else {
5916 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005917 }
5918 }
5919 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005920 if (mPackageStats.size() > 0) {
5921 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5922 while (it.hasNext()) {
5923 Map.Entry<String, Pkg> pkgEntry = it.next();
5924 Pkg p = pkgEntry.getValue();
5925 p.detach();
5926 if (p.mServiceStats.size() > 0) {
5927 Iterator<Map.Entry<String, Pkg.Serv>> it2
5928 = p.mServiceStats.entrySet().iterator();
5929 while (it2.hasNext()) {
5930 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5931 servEntry.getValue().detach();
5932 }
5933 }
5934 }
5935 mPackageStats.clear();
5936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005937
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005938 mLastStepUserTime = mLastStepSystemTime = 0;
5939 mCurStepUserTime = mCurStepSystemTime = 0;
5940
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005941 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005942 if (mWifiRunningTimer != null) {
5943 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005944 }
5945 if (mFullWifiLockTimer != null) {
5946 mFullWifiLockTimer.detach();
5947 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005948 if (mWifiScanTimer != null) {
5949 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005950 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005951 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5952 if (mWifiBatchedScanTimer[i] != null) {
5953 mWifiBatchedScanTimer[i].detach();
5954 }
5955 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005956 if (mWifiMulticastTimer != null) {
5957 mWifiMulticastTimer.detach();
5958 }
5959 if (mAudioTurnedOnTimer != null) {
5960 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005961 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005962 }
5963 if (mVideoTurnedOnTimer != null) {
5964 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005965 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005966 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005967 if (mFlashlightTurnedOnTimer != null) {
5968 mFlashlightTurnedOnTimer.detach();
5969 mFlashlightTurnedOnTimer = null;
5970 }
5971 if (mCameraTurnedOnTimer != null) {
5972 mCameraTurnedOnTimer.detach();
5973 mCameraTurnedOnTimer = null;
5974 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005975 if (mForegroundActivityTimer != null) {
5976 mForegroundActivityTimer.detach();
5977 mForegroundActivityTimer = null;
5978 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005979 if (mBluetoothScanTimer != null) {
5980 mBluetoothScanTimer.detach();
5981 mBluetoothScanTimer = null;
5982 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005983 if (mUserActivityCounters != null) {
5984 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5985 mUserActivityCounters[i].detach();
5986 }
5987 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005988 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005989 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005990 mNetworkByteActivityCounters[i].detach();
5991 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005992 }
5993 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005994
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005995 if (mWifiControllerActivity != null) {
5996 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07005997 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005998
5999 if (mBluetoothControllerActivity != null) {
6000 mBluetoothControllerActivity.detach();
6001 }
6002
6003 if (mModemControllerActivity != null) {
6004 mModemControllerActivity.detach();
6005 }
6006
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006007 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006008
6009 mUserCpuTime.detach();
6010 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006011 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006012
6013 if (mCpuClusterSpeed != null) {
6014 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6015 if (cpuSpeeds != null) {
6016 for (LongSamplingCounter c : cpuSpeeds) {
6017 if (c != null) {
6018 c.detach();
6019 }
6020 }
6021 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006022 }
6023 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006024
6025 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6026 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006027 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006028
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006029 return !active;
6030 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006031
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006032 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006033 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6034 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006035 out.writeInt(NW);
6036 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006037 out.writeString(wakeStats.keyAt(iw));
6038 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006039 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 }
6041
Dianne Hackbornd953c532014-08-16 18:17:38 -07006042 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6043 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006044 out.writeInt(NS);
6045 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006046 out.writeString(syncStats.keyAt(is));
6047 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006048 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6049 }
6050
Dianne Hackbornd953c532014-08-16 18:17:38 -07006051 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6052 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006053 out.writeInt(NJ);
6054 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006055 out.writeString(jobStats.keyAt(ij));
6056 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006057 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6058 }
6059
Dianne Hackborn61659e52014-07-09 16:13:01 -07006060 int NSE = mSensorStats.size();
6061 out.writeInt(NSE);
6062 for (int ise=0; ise<NSE; ise++) {
6063 out.writeInt(mSensorStats.keyAt(ise));
6064 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006065 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 }
6067
Dianne Hackborn61659e52014-07-09 16:13:01 -07006068 int NP = mProcessStats.size();
6069 out.writeInt(NP);
6070 for (int ip=0; ip<NP; ip++) {
6071 out.writeString(mProcessStats.keyAt(ip));
6072 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 proc.writeToParcelLocked(out);
6074 }
6075
6076 out.writeInt(mPackageStats.size());
6077 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6078 out.writeString(pkgEntry.getKey());
6079 Uid.Pkg pkg = pkgEntry.getValue();
6080 pkg.writeToParcelLocked(out);
6081 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006082
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006083 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006084 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006085 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006086 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006087 out.writeInt(0);
6088 }
6089 if (mFullWifiLockTimer != null) {
6090 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006091 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006092 } else {
6093 out.writeInt(0);
6094 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006095 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006096 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006097 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006098 } else {
6099 out.writeInt(0);
6100 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006101 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6102 if (mWifiBatchedScanTimer[i] != null) {
6103 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006104 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006105 } else {
6106 out.writeInt(0);
6107 }
6108 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006109 if (mWifiMulticastTimer != null) {
6110 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006111 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006112 } else {
6113 out.writeInt(0);
6114 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006115
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006116 if (mAudioTurnedOnTimer != null) {
6117 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006118 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006119 } else {
6120 out.writeInt(0);
6121 }
6122 if (mVideoTurnedOnTimer != null) {
6123 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006124 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006125 } else {
6126 out.writeInt(0);
6127 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006128 if (mFlashlightTurnedOnTimer != null) {
6129 out.writeInt(1);
6130 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6131 } else {
6132 out.writeInt(0);
6133 }
6134 if (mCameraTurnedOnTimer != null) {
6135 out.writeInt(1);
6136 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6137 } else {
6138 out.writeInt(0);
6139 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006140 if (mForegroundActivityTimer != null) {
6141 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006142 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006143 } else {
6144 out.writeInt(0);
6145 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006146 if (mBluetoothScanTimer != null) {
6147 out.writeInt(1);
6148 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6149 } else {
6150 out.writeInt(0);
6151 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006152 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6153 if (mProcessStateTimer[i] != null) {
6154 out.writeInt(1);
6155 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6156 } else {
6157 out.writeInt(0);
6158 }
6159 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006160 if (mVibratorOnTimer != null) {
6161 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006162 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006163 } else {
6164 out.writeInt(0);
6165 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006166 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006167 out.writeInt(1);
6168 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6169 mUserActivityCounters[i].writeToParcel(out);
6170 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006171 } else {
6172 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006173 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006174 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006175 out.writeInt(1);
6176 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006177 mNetworkByteActivityCounters[i].writeToParcel(out);
6178 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006179 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006180 mMobileRadioActiveTime.writeToParcel(out);
6181 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006182 } else {
6183 out.writeInt(0);
6184 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006185
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006186 if (mWifiControllerActivity != null) {
6187 out.writeInt(1);
6188 mWifiControllerActivity.writeToParcel(out, 0);
6189 } else {
6190 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006191 }
6192
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006193 if (mBluetoothControllerActivity != null) {
6194 out.writeInt(1);
6195 mBluetoothControllerActivity.writeToParcel(out, 0);
6196 } else {
6197 out.writeInt(0);
6198 }
6199
6200 if (mModemControllerActivity != null) {
6201 out.writeInt(1);
6202 mModemControllerActivity.writeToParcel(out, 0);
6203 } else {
6204 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006205 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006206
6207 mUserCpuTime.writeToParcel(out);
6208 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006209 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006210
Adam Lesinski6832f392015-09-05 18:05:40 -07006211 if (mCpuClusterSpeed != null) {
6212 out.writeInt(1);
6213 out.writeInt(mCpuClusterSpeed.length);
6214 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6215 if (cpuSpeeds != null) {
6216 out.writeInt(1);
6217 out.writeInt(cpuSpeeds.length);
6218 for (LongSamplingCounter c : cpuSpeeds) {
6219 if (c != null) {
6220 out.writeInt(1);
6221 c.writeToParcel(out);
6222 } else {
6223 out.writeInt(0);
6224 }
6225 }
6226 } else {
6227 out.writeInt(0);
6228 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006229 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006230 } else {
6231 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006232 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006233
6234 if (mMobileRadioApWakeupCount != null) {
6235 out.writeInt(1);
6236 mMobileRadioApWakeupCount.writeToParcel(out);
6237 } else {
6238 out.writeInt(0);
6239 }
6240
6241 if (mWifiRadioApWakeupCount != null) {
6242 out.writeInt(1);
6243 mWifiRadioApWakeupCount.writeToParcel(out);
6244 } else {
6245 out.writeInt(0);
6246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 }
6248
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006249 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 int numWakelocks = in.readInt();
6251 mWakelockStats.clear();
6252 for (int j = 0; j < numWakelocks; j++) {
6253 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006254 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006255 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006256 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 }
6258
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006259 int numSyncs = in.readInt();
6260 mSyncStats.clear();
6261 for (int j = 0; j < numSyncs; j++) {
6262 String syncName = in.readString();
6263 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006264 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006265 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006266 }
6267 }
6268
6269 int numJobs = in.readInt();
6270 mJobStats.clear();
6271 for (int j = 0; j < numJobs; j++) {
6272 String jobName = in.readString();
6273 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006274 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
6275 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006276 }
6277 }
6278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 int numSensors = in.readInt();
6280 mSensorStats.clear();
6281 for (int k = 0; k < numSensors; k++) {
6282 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006283 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
6284 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 mSensorStats.put(sensorNumber, sensor);
6286 }
6287
6288 int numProcs = in.readInt();
6289 mProcessStats.clear();
6290 for (int k = 0; k < numProcs; k++) {
6291 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006292 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006293 proc.readFromParcelLocked(in);
6294 mProcessStats.put(processName, proc);
6295 }
6296
6297 int numPkgs = in.readInt();
6298 mPackageStats.clear();
6299 for (int l = 0; l < numPkgs; l++) {
6300 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006301 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 pkg.readFromParcelLocked(in);
6303 mPackageStats.put(packageName, pkg);
6304 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006305
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006306 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006307 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006308 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6309 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006310 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006311 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006312 }
6313 mFullWifiLockOut = false;
6314 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006315 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6316 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006317 } else {
6318 mFullWifiLockTimer = null;
6319 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006320 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006321 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006322 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6323 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006324 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006325 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006326 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006327 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6328 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6329 if (in.readInt() != 0) {
6330 makeWifiBatchedScanBin(i, in);
6331 } else {
6332 mWifiBatchedScanTimer[i] = null;
6333 }
6334 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006335 mWifiMulticastEnabled = false;
6336 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006337 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6338 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006339 } else {
6340 mWifiMulticastTimer = null;
6341 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006342 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006343 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6344 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006345 } else {
6346 mAudioTurnedOnTimer = null;
6347 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006348 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006349 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6350 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006351 } else {
6352 mVideoTurnedOnTimer = null;
6353 }
6354 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006355 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6356 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006357 } else {
6358 mFlashlightTurnedOnTimer = null;
6359 }
6360 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006361 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6362 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006363 } else {
6364 mCameraTurnedOnTimer = null;
6365 }
6366 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006367 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6368 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006369 } else {
6370 mForegroundActivityTimer = null;
6371 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006372 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006373 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6374 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006375 } else {
6376 mBluetoothScanTimer = null;
6377 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006378 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006379 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6380 if (in.readInt() != 0) {
6381 makeProcessState(i, in);
6382 } else {
6383 mProcessStateTimer[i] = null;
6384 }
6385 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006386 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006387 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6388 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006389 } else {
6390 mVibratorOnTimer = null;
6391 }
6392 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006393 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6394 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006395 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006396 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006397 } else {
6398 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006399 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006400 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006401 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6402 mNetworkPacketActivityCounters
6403 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006404 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006405 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006406 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006407 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006408 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006409 }
Joe Onoratoabded112016-02-08 16:49:39 -08006410 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6411 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006412 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006413 mNetworkByteActivityCounters = null;
6414 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006415 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006416
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006417 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006418 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006419 NUM_WIFI_TX_LEVELS, in);
6420 } else {
6421 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006422 }
6423
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006424 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006425 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006426 NUM_BT_TX_LEVELS, in);
6427 } else {
6428 mBluetoothControllerActivity = null;
6429 }
6430
6431 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006432 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006433 ModemActivityInfo.TX_POWER_LEVELS, in);
6434 } else {
6435 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006436 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006437
Joe Onoratoabded112016-02-08 16:49:39 -08006438 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6439 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6440 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006441
Adam Lesinski6832f392015-09-05 18:05:40 -07006442 if (in.readInt() != 0) {
6443 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006444 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006445 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006446 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006447
6448 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6449 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6450 if (in.readInt() != 0) {
6451 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006452 if (mBsi.mPowerProfile != null &&
6453 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006454 throw new ParcelFormatException("Incompatible number of cpu speeds");
6455 }
6456
6457 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6458 mCpuClusterSpeed[cluster] = cpuSpeeds;
6459 for (int speed = 0; speed < numSpeeds; speed++) {
6460 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006461 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006462 }
6463 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006464 } else {
6465 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006466 }
6467 }
6468 } else {
6469 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006470 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006471
6472 if (in.readInt() != 0) {
6473 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6474 } else {
6475 mMobileRadioApWakeupCount = null;
6476 }
6477
6478 if (in.readInt() != 0) {
6479 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6480 } else {
6481 mWifiRadioApWakeupCount = null;
6482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 }
6484
6485 /**
6486 * The statistics associated with a particular wake lock.
6487 */
Joe Onoratoabded112016-02-08 16:49:39 -08006488 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6489 /**
6490 * BatteryStatsImpl that we are associated with.
6491 */
6492 protected BatteryStatsImpl mBsi;
6493
6494 /**
6495 * BatteryStatsImpl that we are associated with.
6496 */
6497 protected Uid mUid;
6498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 /**
6500 * How long (in ms) this uid has been keeping the device partially awake.
6501 */
Evan Millarc64edde2009-04-18 12:26:32 -07006502 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503
6504 /**
6505 * How long (in ms) this uid has been keeping the device fully awake.
6506 */
Evan Millarc64edde2009-04-18 12:26:32 -07006507 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508
6509 /**
6510 * How long (in ms) this uid has had a window keeping the device awake.
6511 */
Evan Millarc64edde2009-04-18 12:26:32 -07006512 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513
6514 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006515 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006516 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006517 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006518
Joe Onoratoabded112016-02-08 16:49:39 -08006519 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6520 mBsi = bsi;
6521 mUid = uid;
6522 }
6523
Adam Lesinski9425fe22015-06-19 12:02:13 -07006524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6526 * proper timer pool from the given BatteryStatsImpl object.
6527 *
6528 * @param in the Parcel to be read from.
6529 * return a new Timer, or null.
6530 */
Evan Millarc64edde2009-04-18 12:26:32 -07006531 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006532 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 if (in.readInt() == 0) {
6534 return null;
6535 }
6536
Joe Onoratoabded112016-02-08 16:49:39 -08006537 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 }
6539
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006540 boolean reset() {
6541 boolean wlactive = false;
6542 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006543 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006544 }
6545 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006546 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006547 }
6548 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006549 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006550 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006551 if (mTimerDraw != null) {
6552 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006553 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006554 if (!wlactive) {
6555 if (mTimerFull != null) {
6556 mTimerFull.detach();
6557 mTimerFull = null;
6558 }
6559 if (mTimerPartial != null) {
6560 mTimerPartial.detach();
6561 mTimerPartial = null;
6562 }
6563 if (mTimerWindow != null) {
6564 mTimerWindow.detach();
6565 mTimerWindow = null;
6566 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006567 if (mTimerDraw != null) {
6568 mTimerDraw.detach();
6569 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006570 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006571 }
6572 return !wlactive;
6573 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006574
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006575 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006577 mBsi.mPartialTimers, screenOffTimeBase, in);
6578 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
6579 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
6580 mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 }
6582
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006583 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6584 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6585 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6586 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006587 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 }
6589
6590 @Override
6591 public Timer getWakeTime(int type) {
6592 switch (type) {
6593 case WAKE_TYPE_FULL: return mTimerFull;
6594 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6595 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006596 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 default: throw new IllegalArgumentException("type = " + type);
6598 }
6599 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006600
6601 public StopwatchTimer getStopwatchTimer(int type) {
6602 StopwatchTimer t;
6603 switch (type) {
6604 case WAKE_TYPE_PARTIAL:
6605 t = mTimerPartial;
6606 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006607 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
6608 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006609 mTimerPartial = t;
6610 }
6611 return t;
6612 case WAKE_TYPE_FULL:
6613 t = mTimerFull;
6614 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006615 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6616 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006617 mTimerFull = t;
6618 }
6619 return t;
6620 case WAKE_TYPE_WINDOW:
6621 t = mTimerWindow;
6622 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006623 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6624 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006625 mTimerWindow = t;
6626 }
6627 return t;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006628 case WAKE_TYPE_DRAW:
6629 t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006630 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006631 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6632 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006633 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006634 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006635 return t;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006636 default:
6637 throw new IllegalArgumentException("type=" + type);
6638 }
6639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 }
6641
Joe Onoratoabded112016-02-08 16:49:39 -08006642 public static class Sensor extends BatteryStats.Uid.Sensor {
6643 /**
6644 * BatteryStatsImpl that we are associated with.
6645 */
6646 protected BatteryStatsImpl mBsi;
6647
6648 /**
6649 * BatteryStatsImpl that we are associated with.
6650 */
6651 protected Uid mUid;
6652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006654 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006655
Joe Onoratoabded112016-02-08 16:49:39 -08006656 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6657 mBsi = bsi;
6658 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 mHandle = handle;
6660 }
6661
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006662 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 if (in.readInt() == 0) {
6664 return null;
6665 }
6666
Joe Onoratoabded112016-02-08 16:49:39 -08006667 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006668 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006669 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006670 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 }
Joe Onoratoabded112016-02-08 16:49:39 -08006672 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 }
6674
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006675 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006676 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006677 mTimer = null;
6678 return true;
6679 }
6680 return false;
6681 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006682
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006683 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6684 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 }
6686
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006687 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6688 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 }
6690
6691 @Override
6692 public Timer getSensorTime() {
6693 return mTimer;
6694 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006695
6696 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006697 public int getHandle() {
6698 return mHandle;
6699 }
6700 }
6701
6702 /**
6703 * The statistics associated with a particular process.
6704 */
Joe Onoratoabded112016-02-08 16:49:39 -08006705 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
6706 /**
6707 * BatteryStatsImpl that we are associated with.
6708 */
6709 protected BatteryStatsImpl mBsi;
6710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006711 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006712 * The name of this process.
6713 */
6714 final String mName;
6715
6716 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08006717 * Remains true until removed from the stats.
6718 */
6719 boolean mActive = true;
6720
6721 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006722 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006723 */
6724 long mUserTime;
6725
6726 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006727 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 */
6729 long mSystemTime;
6730
6731 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006732 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006733 */
6734 long mForegroundTime;
6735
6736 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006737 * Number of times the process has been started.
6738 */
6739 int mStarts;
6740
6741 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006742 * Number of times the process has crashed.
6743 */
6744 int mNumCrashes;
6745
6746 /**
6747 * Number of times the process has had an ANR.
6748 */
6749 int mNumAnrs;
6750
6751 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 * The amount of user time loaded from a previous save.
6753 */
6754 long mLoadedUserTime;
6755
6756 /**
6757 * The amount of system time loaded from a previous save.
6758 */
6759 long mLoadedSystemTime;
6760
6761 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006762 * The amount of foreground time loaded from a previous save.
6763 */
6764 long mLoadedForegroundTime;
6765
6766 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006767 * The number of times the process has started from a previous save.
6768 */
6769 int mLoadedStarts;
6770
6771 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006772 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006774 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775
6776 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006777 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006779 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006780
6781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006782 * The amount of user time when last unplugged.
6783 */
6784 long mUnpluggedUserTime;
6785
6786 /**
6787 * The amount of system time when last unplugged.
6788 */
6789 long mUnpluggedSystemTime;
6790
6791 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006792 * The amount of foreground time since unplugged.
6793 */
6794 long mUnpluggedForegroundTime;
6795
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006796 /**
6797 * The number of times the process has started before unplugged.
6798 */
6799 int mUnpluggedStarts;
6800
Dianne Hackborn61659e52014-07-09 16:13:01 -07006801 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006802 * Number of times the process has crashed before unplugged.
6803 */
6804 int mUnpluggedNumCrashes;
6805
6806 /**
6807 * Number of times the process has had an ANR before unplugged.
6808 */
6809 int mUnpluggedNumAnrs;
6810
Dianne Hackborn287952c2010-09-22 22:34:31 -07006811 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006812
Joe Onoratoabded112016-02-08 16:49:39 -08006813 public Proc(BatteryStatsImpl bsi, String name) {
6814 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006815 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08006816 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006818
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006819 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 mUnpluggedUserTime = mUserTime;
6821 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006822 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006823 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006824 mUnpluggedNumCrashes = mNumCrashes;
6825 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 }
6827
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006828 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006830
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006831 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006832 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08006833 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006834 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006835
Dianne Hackborn287952c2010-09-22 22:34:31 -07006836 public int countExcessivePowers() {
6837 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006838 }
6839
Dianne Hackborn287952c2010-09-22 22:34:31 -07006840 public ExcessivePower getExcessivePower(int i) {
6841 if (mExcessivePower != null) {
6842 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006843 }
6844 return null;
6845 }
6846
6847 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006848 if (mExcessivePower == null) {
6849 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006850 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006851 ExcessivePower ew = new ExcessivePower();
6852 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006853 ew.overTime = overTime;
6854 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006855 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006856 }
6857
Dianne Hackborn287952c2010-09-22 22:34:31 -07006858 public void addExcessiveCpu(long overTime, long usedTime) {
6859 if (mExcessivePower == null) {
6860 mExcessivePower = new ArrayList<ExcessivePower>();
6861 }
6862 ExcessivePower ew = new ExcessivePower();
6863 ew.type = ExcessivePower.TYPE_CPU;
6864 ew.overTime = overTime;
6865 ew.usedTime = usedTime;
6866 mExcessivePower.add(ew);
6867 }
6868
6869 void writeExcessivePowerToParcelLocked(Parcel out) {
6870 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006871 out.writeInt(0);
6872 return;
6873 }
6874
Dianne Hackborn287952c2010-09-22 22:34:31 -07006875 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006876 out.writeInt(N);
6877 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006878 ExcessivePower ew = mExcessivePower.get(i);
6879 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006880 out.writeLong(ew.overTime);
6881 out.writeLong(ew.usedTime);
6882 }
6883 }
6884
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006885 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006886 final int N = in.readInt();
6887 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006888 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006889 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006890 }
6891
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006892 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006893 throw new ParcelFormatException(
6894 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006895 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006896
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006897 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006898 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006899 ExcessivePower ew = new ExcessivePower();
6900 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006901 ew.overTime = in.readLong();
6902 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006903 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006904 }
6905 }
6906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 void writeToParcelLocked(Parcel out) {
6908 out.writeLong(mUserTime);
6909 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006910 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006912 out.writeInt(mNumCrashes);
6913 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 out.writeLong(mLoadedUserTime);
6915 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006916 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006918 out.writeInt(mLoadedNumCrashes);
6919 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920 out.writeLong(mUnpluggedUserTime);
6921 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006922 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006924 out.writeInt(mUnpluggedNumCrashes);
6925 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006926 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 }
6928
6929 void readFromParcelLocked(Parcel in) {
6930 mUserTime = in.readLong();
6931 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006932 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006934 mNumCrashes = in.readInt();
6935 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 mLoadedUserTime = in.readLong();
6937 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006938 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006940 mLoadedNumCrashes = in.readInt();
6941 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006942 mUnpluggedUserTime = in.readLong();
6943 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006944 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006946 mUnpluggedNumCrashes = in.readInt();
6947 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006948 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 }
6950
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006951 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 mUserTime += utime;
6953 mSystemTime += stime;
6954 }
6955
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006956 public void addForegroundTimeLocked(long ttime) {
6957 mForegroundTime += ttime;
6958 }
6959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 public void incStartsLocked() {
6961 mStarts++;
6962 }
6963
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006964 public void incNumCrashesLocked() {
6965 mNumCrashes++;
6966 }
6967
6968 public void incNumAnrsLocked() {
6969 mNumAnrs++;
6970 }
6971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006973 public boolean isActive() {
6974 return mActive;
6975 }
6976
6977 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006979 long val = mUserTime;
6980 if (which == STATS_CURRENT) {
6981 val -= mLoadedUserTime;
6982 } else if (which == STATS_SINCE_UNPLUGGED) {
6983 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 }
6985 return val;
6986 }
6987
6988 @Override
6989 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006990 long val = mSystemTime;
6991 if (which == STATS_CURRENT) {
6992 val -= mLoadedSystemTime;
6993 } else if (which == STATS_SINCE_UNPLUGGED) {
6994 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 }
6996 return val;
6997 }
6998
6999 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007000 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007001 long val = mForegroundTime;
7002 if (which == STATS_CURRENT) {
7003 val -= mLoadedForegroundTime;
7004 } else if (which == STATS_SINCE_UNPLUGGED) {
7005 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007006 }
7007 return val;
7008 }
7009
7010 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007012 int val = mStarts;
7013 if (which == STATS_CURRENT) {
7014 val -= mLoadedStarts;
7015 } else if (which == STATS_SINCE_UNPLUGGED) {
7016 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007017 }
7018 return val;
7019 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007020
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007021 @Override
7022 public int getNumCrashes(int which) {
7023 int val = mNumCrashes;
7024 if (which == STATS_CURRENT) {
7025 val -= mLoadedNumCrashes;
7026 } else if (which == STATS_SINCE_UNPLUGGED) {
7027 val -= mUnpluggedNumCrashes;
7028 }
7029 return val;
7030 }
7031
7032 @Override
7033 public int getNumAnrs(int which) {
7034 int val = mNumAnrs;
7035 if (which == STATS_CURRENT) {
7036 val -= mLoadedNumAnrs;
7037 } else if (which == STATS_SINCE_UNPLUGGED) {
7038 val -= mUnpluggedNumAnrs;
7039 }
7040 return val;
7041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 }
7043
7044 /**
7045 * The statistics associated with a particular package.
7046 */
Joe Onoratoabded112016-02-08 16:49:39 -08007047 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7048 /**
7049 * BatteryStatsImpl that we are associated with.
7050 */
7051 protected BatteryStatsImpl mBsi;
7052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007054 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007055 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007056 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057
7058 /**
7059 * The statics we have collected for this package's services.
7060 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007061 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062
Joe Onoratoabded112016-02-08 16:49:39 -08007063 public Pkg(BatteryStatsImpl bsi) {
7064 mBsi = bsi;
7065 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007066 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007067
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007068 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 }
7070
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007071 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007073
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007074 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007075 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007076 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007079 int numWA = in.readInt();
7080 mWakeupAlarms.clear();
7081 for (int i=0; i<numWA; i++) {
7082 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007083 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085
7086 int numServs = in.readInt();
7087 mServiceStats.clear();
7088 for (int m = 0; m < numServs; m++) {
7089 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007090 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 mServiceStats.put(serviceName, serv);
7092
7093 serv.readFromParcelLocked(in);
7094 }
7095 }
7096
7097 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007098 int numWA = mWakeupAlarms.size();
7099 out.writeInt(numWA);
7100 for (int i=0; i<numWA; i++) {
7101 out.writeString(mWakeupAlarms.keyAt(i));
7102 mWakeupAlarms.valueAt(i).writeToParcel(out);
7103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007105 final int NS = mServiceStats.size();
7106 out.writeInt(NS);
7107 for (int i=0; i<NS; i++) {
7108 out.writeString(mServiceStats.keyAt(i));
7109 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 serv.writeToParcelLocked(out);
7111 }
7112 }
7113
7114 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007115 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7116 return mWakeupAlarms;
7117 }
7118
7119 public void noteWakeupAlarmLocked(String tag) {
7120 Counter c = mWakeupAlarms.get(tag);
7121 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007122 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007123 mWakeupAlarms.put(tag, c);
7124 }
7125 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 }
7127
7128 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007129 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
7130 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 }
7132
7133 /**
7134 * The statistics associated with a particular service.
7135 */
Joe Onoratoabded112016-02-08 16:49:39 -08007136 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
7137 /**
7138 * BatteryStatsImpl that we are associated with.
7139 */
7140 protected BatteryStatsImpl mBsi;
7141
7142 /**
7143 * The android package in which this service resides.
7144 */
7145 protected Pkg mPkg;
7146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007147 /**
7148 * Total time (ms in battery uptime) the service has been left started.
7149 */
Joe Onoratoabded112016-02-08 16:49:39 -08007150 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151
7152 /**
7153 * If service has been started and not yet stopped, this is
7154 * when it was started.
7155 */
Joe Onoratoabded112016-02-08 16:49:39 -08007156 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007157
7158 /**
7159 * True if we are currently running.
7160 */
Joe Onoratoabded112016-02-08 16:49:39 -08007161 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162
7163 /**
7164 * Total number of times startService() has been called.
7165 */
Joe Onoratoabded112016-02-08 16:49:39 -08007166 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167
7168 /**
7169 * Total time (ms in battery uptime) the service has been left launched.
7170 */
Joe Onoratoabded112016-02-08 16:49:39 -08007171 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172
7173 /**
7174 * If service has been launched and not yet exited, this is
7175 * when it was launched (ms in battery uptime).
7176 */
Joe Onoratoabded112016-02-08 16:49:39 -08007177 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178
7179 /**
7180 * True if we are currently launched.
7181 */
Joe Onoratoabded112016-02-08 16:49:39 -08007182 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183
7184 /**
7185 * Total number times the service has been launched.
7186 */
Joe Onoratoabded112016-02-08 16:49:39 -08007187 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188
7189 /**
7190 * The amount of time spent started loaded from a previous save
7191 * (ms in battery uptime).
7192 */
Joe Onoratoabded112016-02-08 16:49:39 -08007193 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194
7195 /**
7196 * The number of starts loaded from a previous save.
7197 */
Joe Onoratoabded112016-02-08 16:49:39 -08007198 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199
7200 /**
7201 * The number of launches loaded from a previous save.
7202 */
Joe Onoratoabded112016-02-08 16:49:39 -08007203 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204
7205 /**
7206 * The amount of time spent started as of the last run (ms
7207 * in battery uptime).
7208 */
Joe Onoratoabded112016-02-08 16:49:39 -08007209 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210
7211 /**
7212 * The number of starts as of the last run.
7213 */
Joe Onoratoabded112016-02-08 16:49:39 -08007214 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007215
7216 /**
7217 * The number of launches as of the last run.
7218 */
Joe Onoratoabded112016-02-08 16:49:39 -08007219 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220
7221 /**
7222 * The amount of time spent started when last unplugged (ms
7223 * in battery uptime).
7224 */
Joe Onoratoabded112016-02-08 16:49:39 -08007225 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226
7227 /**
7228 * The number of starts when last unplugged.
7229 */
Joe Onoratoabded112016-02-08 16:49:39 -08007230 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231
7232 /**
7233 * The number of launches when last unplugged.
7234 */
Joe Onoratoabded112016-02-08 16:49:39 -08007235 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236
Joe Onoratoabded112016-02-08 16:49:39 -08007237 /**
7238 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7239 */
7240 public Serv(BatteryStatsImpl bsi) {
7241 mBsi = bsi;
7242 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007244
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007245 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7246 long baseRealtime) {
7247 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 mUnpluggedStarts = mStarts;
7249 mUnpluggedLaunches = mLaunches;
7250 }
7251
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007252 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7253 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007255
Joe Onoratoabded112016-02-08 16:49:39 -08007256 /**
7257 * Remove this Serv as a listener from the time base.
7258 */
7259 public void detach() {
7260 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007261 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007262
Joe Onoratoabded112016-02-08 16:49:39 -08007263 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 mStartTime = in.readLong();
7265 mRunningSince = in.readLong();
7266 mRunning = in.readInt() != 0;
7267 mStarts = in.readInt();
7268 mLaunchedTime = in.readLong();
7269 mLaunchedSince = in.readLong();
7270 mLaunched = in.readInt() != 0;
7271 mLaunches = in.readInt();
7272 mLoadedStartTime = in.readLong();
7273 mLoadedStarts = in.readInt();
7274 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007275 mLastStartTime = 0;
7276 mLastStarts = 0;
7277 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 mUnpluggedStartTime = in.readLong();
7279 mUnpluggedStarts = in.readInt();
7280 mUnpluggedLaunches = in.readInt();
7281 }
7282
Joe Onoratoabded112016-02-08 16:49:39 -08007283 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 out.writeLong(mStartTime);
7285 out.writeLong(mRunningSince);
7286 out.writeInt(mRunning ? 1 : 0);
7287 out.writeInt(mStarts);
7288 out.writeLong(mLaunchedTime);
7289 out.writeLong(mLaunchedSince);
7290 out.writeInt(mLaunched ? 1 : 0);
7291 out.writeInt(mLaunches);
7292 out.writeLong(mLoadedStartTime);
7293 out.writeInt(mLoadedStarts);
7294 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 out.writeLong(mUnpluggedStartTime);
7296 out.writeInt(mUnpluggedStarts);
7297 out.writeInt(mUnpluggedLaunches);
7298 }
7299
Joe Onoratoabded112016-02-08 16:49:39 -08007300 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 if (!mLaunched) return mLaunchedTime;
7302 return mLaunchedTime + batteryUptime - mLaunchedSince;
7303 }
7304
Joe Onoratoabded112016-02-08 16:49:39 -08007305 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007306 if (!mRunning) return mStartTime;
7307 return mStartTime + batteryUptime - mRunningSince;
7308 }
7309
7310 public void startLaunchedLocked() {
7311 if (!mLaunched) {
7312 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007313 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 mLaunched = true;
7315 }
7316 }
7317
7318 public void stopLaunchedLocked() {
7319 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007320 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 if (time > 0) {
7322 mLaunchedTime += time;
7323 } else {
7324 mLaunches--;
7325 }
7326 mLaunched = false;
7327 }
7328 }
7329
7330 public void startRunningLocked() {
7331 if (!mRunning) {
7332 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007333 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 mRunning = true;
7335 }
7336 }
7337
7338 public void stopRunningLocked() {
7339 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007340 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341 if (time > 0) {
7342 mStartTime += time;
7343 } else {
7344 mStarts--;
7345 }
7346 mRunning = false;
7347 }
7348 }
7349
7350 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007351 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007352 }
7353
7354 @Override
7355 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007356 int val = mLaunches;
7357 if (which == STATS_CURRENT) {
7358 val -= mLoadedLaunches;
7359 } else if (which == STATS_SINCE_UNPLUGGED) {
7360 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 return val;
7363 }
7364
7365 @Override
7366 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007367 long val = getStartTimeToNowLocked(now);
7368 if (which == STATS_CURRENT) {
7369 val -= mLoadedStartTime;
7370 } else if (which == STATS_SINCE_UNPLUGGED) {
7371 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 return val;
7374 }
7375
7376 @Override
7377 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007378 int val = mStarts;
7379 if (which == STATS_CURRENT) {
7380 val -= mLoadedStarts;
7381 } else if (which == STATS_SINCE_UNPLUGGED) {
7382 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 }
7384
7385 return val;
7386 }
7387 }
7388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007390 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 }
7392 }
7393
7394 /**
7395 * Retrieve the statistics object for a particular process, creating
7396 * if needed.
7397 */
7398 public Proc getProcessStatsLocked(String name) {
7399 Proc ps = mProcessStats.get(name);
7400 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007401 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 mProcessStats.put(name, ps);
7403 }
7404
7405 return ps;
7406 }
7407
Dianne Hackborna8d10942015-11-19 17:55:19 -08007408 public void updateUidProcessStateLocked(int procState) {
7409 int uidRunningState;
7410 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7411 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7412 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7413 uidRunningState = PROCESS_STATE_TOP;
7414 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7415 // Persistent and other foreground states go here.
7416 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7417 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7418 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7419 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7420 // Persistent and other foreground states go here.
7421 uidRunningState = PROCESS_STATE_FOREGROUND;
7422 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7423 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007424 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007425 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007426 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007427
Dianne Hackborna8d10942015-11-19 17:55:19 -08007428 if (mProcessState == uidRunningState) return;
7429
Joe Onoratoabded112016-02-08 16:49:39 -08007430 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007431
7432 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7433 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7434 }
7435 mProcessState = uidRunningState;
7436 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7437 if (mProcessStateTimer[uidRunningState] == null) {
7438 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007439 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007440 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007441 }
7442 }
7443
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007444 public SparseArray<? extends Pid> getPidStats() {
7445 return mPids;
7446 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007447
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007448 public Pid getPidStatsLocked(int pid) {
7449 Pid p = mPids.get(pid);
7450 if (p == null) {
7451 p = new Pid();
7452 mPids.put(pid, p);
7453 }
7454 return p;
7455 }
7456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 /**
7458 * Retrieve the statistics object for a particular service, creating
7459 * if needed.
7460 */
7461 public Pkg getPackageStatsLocked(String name) {
7462 Pkg ps = mPackageStats.get(name);
7463 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007464 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 mPackageStats.put(name, ps);
7466 }
7467
7468 return ps;
7469 }
7470
7471 /**
7472 * Retrieve the statistics object for a particular service, creating
7473 * if needed.
7474 */
7475 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7476 Pkg ps = getPackageStatsLocked(pkg);
7477 Pkg.Serv ss = ps.mServiceStats.get(serv);
7478 if (ss == null) {
7479 ss = ps.newServiceStatsLocked();
7480 ps.mServiceStats.put(serv, ss);
7481 }
7482
7483 return ss;
7484 }
7485
Dianne Hackbornd953c532014-08-16 18:17:38 -07007486 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7487 StopwatchTimer timer = mSyncStats.instantiateObject();
7488 timer.readSummaryFromParcelLocked(in);
7489 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007490 }
7491
Dianne Hackbornd953c532014-08-16 18:17:38 -07007492 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7493 StopwatchTimer timer = mJobStats.instantiateObject();
7494 timer.readSummaryFromParcelLocked(in);
7495 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007496 }
7497
Dianne Hackbornd953c532014-08-16 18:17:38 -07007498 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007499 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007500 mWakelockStats.add(wlName, wl);
7501 if (in.readInt() != 0) {
7502 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007504 if (in.readInt() != 0) {
7505 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7506 }
7507 if (in.readInt() != 0) {
7508 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007510 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007511 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 }
7514
Evan Millarc64edde2009-04-18 12:26:32 -07007515 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 Sensor se = mSensorStats.get(sensor);
7517 if (se == null) {
7518 if (!create) {
7519 return null;
7520 }
Joe Onoratoabded112016-02-08 16:49:39 -08007521 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 mSensorStats.put(sensor, se);
7523 }
Evan Millarc64edde2009-04-18 12:26:32 -07007524 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 if (t != null) {
7526 return t;
7527 }
Joe Onoratoabded112016-02-08 16:49:39 -08007528 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007529 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007530 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007531 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007532 }
Joe Onoratoabded112016-02-08 16:49:39 -08007533 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7534 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 se.mTimer = t;
7536 return t;
7537 }
7538
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007539 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007540 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007541 if (t != null) {
7542 t.startRunningLocked(elapsedRealtimeMs);
7543 }
7544 }
7545
7546 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007547 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007548 if (t != null) {
7549 t.stopRunningLocked(elapsedRealtimeMs);
7550 }
7551 }
7552
7553 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007554 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007555 if (t != null) {
7556 t.startRunningLocked(elapsedRealtimeMs);
7557 }
7558 }
7559
7560 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007561 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007562 if (t != null) {
7563 t.stopRunningLocked(elapsedRealtimeMs);
7564 }
7565 }
7566
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007567 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007568 Wakelock wl = mWakelockStats.startObject(name);
7569 if (wl != null) {
7570 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007572 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007573 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007574 if (p.mWakeNesting++ == 0) {
7575 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007576 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 }
7579
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007580 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007581 Wakelock wl = mWakelockStats.stopObject(name);
7582 if (wl != null) {
7583 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007585 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007586 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007587 if (p != null && p.mWakeNesting > 0) {
7588 if (p.mWakeNesting-- == 1) {
7589 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7590 p.mWakeStartMs = 0;
7591 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007592 }
7593 }
7594 }
7595
7596 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7597 Proc p = getProcessStatsLocked(proc);
7598 if (p != null) {
7599 p.addExcessiveWake(overTime, usedTime);
7600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007601 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007602
Dianne Hackborn287952c2010-09-22 22:34:31 -07007603 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7604 Proc p = getProcessStatsLocked(proc);
7605 if (p != null) {
7606 p.addExcessiveCpu(overTime, usedTime);
7607 }
7608 }
7609
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007610 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007611 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007613 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 }
7616
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007617 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007619 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007620 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007621 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007624
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007625 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007626 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007628 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007630 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007631
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007632 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007633 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007635 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 }
7638
7639 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007640 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 }
7642 }
7643
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007644 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007645 this(new SystemClocks(), systemDir, handler, externalSync, null);
7646 }
7647
7648 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
7649 PlatformIdleStateCallback cb) {
7650 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08007651 }
7652
7653 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007654 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08007655 init(clocks);
7656
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007657 if (systemDir != null) {
7658 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7659 new File(systemDir, "batterystats.bin.tmp"));
7660 } else {
7661 mFile = null;
7662 }
7663 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007664 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007665 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007666 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007668 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007669 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007670 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007671 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007672 }
Joe Onoratoabded112016-02-08 16:49:39 -08007673 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
7674 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
7675 mOnBatteryTimeBase);
7676 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
7677 mOnBatteryTimeBase);
7678 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
7679 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
7680 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
7681 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
7682 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
7683 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
7684 mOnBatteryTimeBase);
7685 }
7686 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
7687 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007688 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007689 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007690 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007691 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007692 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007693 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7694 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007695 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007696 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
7697 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7698 NUM_BT_TX_LEVELS);
7699 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7700 ModemActivityInfo.TX_POWER_LEVELS);
7701
Joe Onoratoabded112016-02-08 16:49:39 -08007702 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
7703 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
7704 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007705 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007706 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
7707 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08007708 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
7709 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007710 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007711 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07007712 mOnBatteryTimeBase);
7713 }
Joe Onoratoabded112016-02-08 16:49:39 -08007714 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7715 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
7716 mOnBatteryTimeBase);
7717 }
7718 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7719 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
7720 mOnBatteryTimeBase);
7721 }
7722 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
7723 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
7724 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
7725 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
7726 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07007727 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
7728 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007730 long uptime = mClocks.uptimeMillis() * 1000;
7731 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007732 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007733 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07007734 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007735 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007736 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07007737 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007738 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007739 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007740 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007741 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007742 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743 }
7744
7745 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08007746 this(new SystemClocks(), p);
7747 }
7748
7749 public BatteryStatsImpl(Clocks clocks, Parcel p) {
7750 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007751 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007752 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007753 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007754 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007755 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007756 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007758 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 }
7760
Adam Lesinskie08af192015-03-25 16:42:59 -07007761 public void setPowerProfile(PowerProfile profile) {
7762 synchronized (this) {
7763 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07007764
7765 // We need to initialize the KernelCpuSpeedReaders to read from
7766 // the first cpu of each core. Once we have the PowerProfile, we have access to this
7767 // information.
7768 final int numClusters = mPowerProfile.getNumCpuClusters();
7769 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
7770 int firstCpuOfCluster = 0;
7771 for (int i = 0; i < numClusters; i++) {
7772 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
7773 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
7774 numSpeedSteps);
7775 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
7776 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07007777
7778 if (mEstimatedBatteryCapacity == -1) {
7779 // Initialize the estimated battery capacity to a known preset one.
7780 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
7781 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007782 }
7783 }
7784
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007785 public void setCallback(BatteryCallback cb) {
7786 mCallback = cb;
7787 }
7788
Amith Yamasanif37447b2009-10-08 18:28:01 -07007789 public void setRadioScanningTimeout(long timeout) {
7790 if (mPhoneSignalScanningTimer != null) {
7791 mPhoneSignalScanningTimer.setTimeout(timeout);
7792 }
7793 }
7794
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007795 public void updateDailyDeadlineLocked() {
7796 // Get the current time.
7797 long currentTime = mDailyStartTime = System.currentTimeMillis();
7798 Calendar calDeadline = Calendar.getInstance();
7799 calDeadline.setTimeInMillis(currentTime);
7800
7801 // Move time up to the next day, ranging from 1am to 3pm.
7802 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
7803 calDeadline.set(Calendar.MILLISECOND, 0);
7804 calDeadline.set(Calendar.SECOND, 0);
7805 calDeadline.set(Calendar.MINUTE, 0);
7806 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
7807 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
7808 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
7809 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
7810 }
7811
7812 public void recordDailyStatsIfNeededLocked(boolean settled) {
7813 long currentTime = System.currentTimeMillis();
7814 if (currentTime >= mNextMaxDailyDeadline) {
7815 recordDailyStatsLocked();
7816 } else if (settled && currentTime >= mNextMinDailyDeadline) {
7817 recordDailyStatsLocked();
7818 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
7819 recordDailyStatsLocked();
7820 }
7821 }
7822
7823 public void recordDailyStatsLocked() {
7824 DailyItem item = new DailyItem();
7825 item.mStartTime = mDailyStartTime;
7826 item.mEndTime = System.currentTimeMillis();
7827 boolean hasData = false;
7828 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
7829 hasData = true;
7830 item.mDischargeSteps = new LevelStepTracker(
7831 mDailyDischargeStepTracker.mNumStepDurations,
7832 mDailyDischargeStepTracker.mStepDurations);
7833 }
7834 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
7835 hasData = true;
7836 item.mChargeSteps = new LevelStepTracker(
7837 mDailyChargeStepTracker.mNumStepDurations,
7838 mDailyChargeStepTracker.mStepDurations);
7839 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007840 if (mDailyPackageChanges != null) {
7841 hasData = true;
7842 item.mPackageChanges = mDailyPackageChanges;
7843 mDailyPackageChanges = null;
7844 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007845 mDailyDischargeStepTracker.init();
7846 mDailyChargeStepTracker.init();
7847 updateDailyDeadlineLocked();
7848
7849 if (hasData) {
7850 mDailyItems.add(item);
7851 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
7852 mDailyItems.remove(0);
7853 }
7854 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
7855 try {
7856 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007857 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007858 writeDailyItemsLocked(out);
7859 BackgroundThread.getHandler().post(new Runnable() {
7860 @Override
7861 public void run() {
7862 synchronized (mCheckinFile) {
7863 FileOutputStream stream = null;
7864 try {
7865 stream = mDailyFile.startWrite();
7866 memStream.writeTo(stream);
7867 stream.flush();
7868 FileUtils.sync(stream);
7869 stream.close();
7870 mDailyFile.finishWrite(stream);
7871 } catch (IOException e) {
7872 Slog.w("BatteryStats",
7873 "Error writing battery daily items", e);
7874 mDailyFile.failWrite(stream);
7875 }
7876 }
7877 }
7878 });
7879 } catch (IOException e) {
7880 }
7881 }
7882 }
7883
7884 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7885 StringBuilder sb = new StringBuilder(64);
7886 out.startDocument(null, true);
7887 out.startTag(null, "daily-items");
7888 for (int i=0; i<mDailyItems.size(); i++) {
7889 final DailyItem dit = mDailyItems.get(i);
7890 out.startTag(null, "item");
7891 out.attribute(null, "start", Long.toString(dit.mStartTime));
7892 out.attribute(null, "end", Long.toString(dit.mEndTime));
7893 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7894 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007895 if (dit.mPackageChanges != null) {
7896 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7897 PackageChange pc = dit.mPackageChanges.get(j);
7898 if (pc.mUpdate) {
7899 out.startTag(null, "upd");
7900 out.attribute(null, "pkg", pc.mPackageName);
7901 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7902 out.endTag(null, "upd");
7903 } else {
7904 out.startTag(null, "rem");
7905 out.attribute(null, "pkg", pc.mPackageName);
7906 out.endTag(null, "rem");
7907 }
7908 }
7909 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007910 out.endTag(null, "item");
7911 }
7912 out.endTag(null, "daily-items");
7913 out.endDocument();
7914 }
7915
7916 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7917 StringBuilder tmpBuilder) throws IOException {
7918 if (steps != null) {
7919 out.startTag(null, tag);
7920 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7921 for (int i=0; i<steps.mNumStepDurations; i++) {
7922 out.startTag(null, "s");
7923 tmpBuilder.setLength(0);
7924 steps.encodeEntryAt(i, tmpBuilder);
7925 out.attribute(null, "v", tmpBuilder.toString());
7926 out.endTag(null, "s");
7927 }
7928 out.endTag(null, tag);
7929 }
7930 }
7931
7932 public void readDailyStatsLocked() {
7933 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7934 mDailyItems.clear();
7935 FileInputStream stream;
7936 try {
7937 stream = mDailyFile.openRead();
7938 } catch (FileNotFoundException e) {
7939 return;
7940 }
7941 try {
7942 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007943 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007944 readDailyItemsLocked(parser);
7945 } catch (XmlPullParserException e) {
7946 } finally {
7947 try {
7948 stream.close();
7949 } catch (IOException e) {
7950 }
7951 }
7952 }
7953
7954 private void readDailyItemsLocked(XmlPullParser parser) {
7955 try {
7956 int type;
7957 while ((type = parser.next()) != XmlPullParser.START_TAG
7958 && type != XmlPullParser.END_DOCUMENT) {
7959 ;
7960 }
7961
7962 if (type != XmlPullParser.START_TAG) {
7963 throw new IllegalStateException("no start tag found");
7964 }
7965
7966 int outerDepth = parser.getDepth();
7967 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7968 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7969 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7970 continue;
7971 }
7972
7973 String tagName = parser.getName();
7974 if (tagName.equals("item")) {
7975 readDailyItemTagLocked(parser);
7976 } else {
7977 Slog.w(TAG, "Unknown element under <daily-items>: "
7978 + parser.getName());
7979 XmlUtils.skipCurrentTag(parser);
7980 }
7981 }
7982
7983 } catch (IllegalStateException e) {
7984 Slog.w(TAG, "Failed parsing daily " + e);
7985 } catch (NullPointerException e) {
7986 Slog.w(TAG, "Failed parsing daily " + e);
7987 } catch (NumberFormatException e) {
7988 Slog.w(TAG, "Failed parsing daily " + e);
7989 } catch (XmlPullParserException e) {
7990 Slog.w(TAG, "Failed parsing daily " + e);
7991 } catch (IOException e) {
7992 Slog.w(TAG, "Failed parsing daily " + e);
7993 } catch (IndexOutOfBoundsException e) {
7994 Slog.w(TAG, "Failed parsing daily " + e);
7995 }
7996 }
7997
7998 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7999 XmlPullParserException, IOException {
8000 DailyItem dit = new DailyItem();
8001 String attr = parser.getAttributeValue(null, "start");
8002 if (attr != null) {
8003 dit.mStartTime = Long.parseLong(attr);
8004 }
8005 attr = parser.getAttributeValue(null, "end");
8006 if (attr != null) {
8007 dit.mEndTime = Long.parseLong(attr);
8008 }
8009 int outerDepth = parser.getDepth();
8010 int type;
8011 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8012 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8013 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8014 continue;
8015 }
8016
8017 String tagName = parser.getName();
8018 if (tagName.equals("dis")) {
8019 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8020 } else if (tagName.equals("chg")) {
8021 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008022 } else if (tagName.equals("upd")) {
8023 if (dit.mPackageChanges == null) {
8024 dit.mPackageChanges = new ArrayList<>();
8025 }
8026 PackageChange pc = new PackageChange();
8027 pc.mUpdate = true;
8028 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8029 String verStr = parser.getAttributeValue(null, "ver");
8030 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8031 dit.mPackageChanges.add(pc);
8032 XmlUtils.skipCurrentTag(parser);
8033 } else if (tagName.equals("rem")) {
8034 if (dit.mPackageChanges == null) {
8035 dit.mPackageChanges = new ArrayList<>();
8036 }
8037 PackageChange pc = new PackageChange();
8038 pc.mUpdate = false;
8039 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8040 dit.mPackageChanges.add(pc);
8041 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008042 } else {
8043 Slog.w(TAG, "Unknown element under <item>: "
8044 + parser.getName());
8045 XmlUtils.skipCurrentTag(parser);
8046 }
8047 }
8048 mDailyItems.add(dit);
8049 }
8050
8051 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8052 String tag)
8053 throws NumberFormatException, XmlPullParserException, IOException {
8054 final String numAttr = parser.getAttributeValue(null, "n");
8055 if (numAttr == null) {
8056 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
8057 XmlUtils.skipCurrentTag(parser);
8058 return;
8059 }
8060 final int num = Integer.parseInt(numAttr);
8061 LevelStepTracker steps = new LevelStepTracker(num);
8062 if (isCharge) {
8063 dit.mChargeSteps = steps;
8064 } else {
8065 dit.mDischargeSteps = steps;
8066 }
8067 int i = 0;
8068 int outerDepth = parser.getDepth();
8069 int type;
8070 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8071 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8072 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8073 continue;
8074 }
8075
8076 String tagName = parser.getName();
8077 if ("s".equals(tagName)) {
8078 if (i < num) {
8079 String valueAttr = parser.getAttributeValue(null, "v");
8080 if (valueAttr != null) {
8081 steps.decodeEntryAt(i, valueAttr);
8082 i++;
8083 }
8084 }
8085 } else {
8086 Slog.w(TAG, "Unknown element under <" + tag + ">: "
8087 + parser.getName());
8088 XmlUtils.skipCurrentTag(parser);
8089 }
8090 }
8091 steps.mNumStepDurations = i;
8092 }
8093
8094 @Override
8095 public DailyItem getDailyItemLocked(int daysAgo) {
8096 int index = mDailyItems.size()-1-daysAgo;
8097 return index >= 0 ? mDailyItems.get(index) : null;
8098 }
8099
8100 @Override
8101 public long getCurrentDailyStartTime() {
8102 return mDailyStartTime;
8103 }
8104
8105 @Override
8106 public long getNextMinDailyDeadline() {
8107 return mNextMinDailyDeadline;
8108 }
8109
8110 @Override
8111 public long getNextMaxDailyDeadline() {
8112 return mNextMaxDailyDeadline;
8113 }
8114
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008115 @Override
8116 public boolean startIteratingOldHistoryLocked() {
8117 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8118 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008119 if ((mHistoryIterator = mHistory) == null) {
8120 return false;
8121 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008122 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008123 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008124 mReadOverflow = false;
8125 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008126 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008127 }
8128
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008129 @Override
8130 public boolean getNextOldHistoryLocked(HistoryItem out) {
8131 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
8132 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008133 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008134 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008135 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008136 HistoryItem cur = mHistoryIterator;
8137 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008138 if (!mReadOverflow && !end) {
8139 Slog.w(TAG, "Old history ends before new history!");
8140 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008141 return false;
8142 }
8143 out.setTo(cur);
8144 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008145 if (!mReadOverflow) {
8146 if (end) {
8147 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008148 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008149 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008150 pw.println("Histories differ!");
8151 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008152 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008153 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008154 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8155 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008156 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008157 }
8158 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008159 return true;
8160 }
8161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008163 public void finishIteratingOldHistoryLocked() {
8164 mIteratingHistory = false;
8165 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008166 mHistoryIterator = null;
8167 }
8168
8169 public int getHistoryTotalSize() {
8170 return MAX_HISTORY_BUFFER;
8171 }
8172
8173 public int getHistoryUsedSize() {
8174 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008175 }
8176
8177 @Override
8178 public boolean startIteratingHistoryLocked() {
8179 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8180 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008181 if (mHistoryBuffer.dataSize() <= 0) {
8182 return false;
8183 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008184 mHistoryBuffer.setDataPosition(0);
8185 mReadOverflow = false;
8186 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008187 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8188 mReadHistoryUids = new int[mHistoryTagPool.size()];
8189 mReadHistoryChars = 0;
8190 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8191 final HistoryTag tag = ent.getKey();
8192 final int idx = ent.getValue();
8193 mReadHistoryStrings[idx] = tag.string;
8194 mReadHistoryUids[idx] = tag.uid;
8195 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008196 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008197 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008198 }
8199
8200 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008201 public int getHistoryStringPoolSize() {
8202 return mReadHistoryStrings.length;
8203 }
8204
8205 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008206 public int getHistoryStringPoolBytes() {
8207 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8208 // Each string character is 2 bytes.
8209 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8210 }
8211
8212 @Override
8213 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008214 return mReadHistoryStrings[index];
8215 }
8216
8217 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008218 public int getHistoryTagPoolUid(int index) {
8219 return mReadHistoryUids[index];
8220 }
8221
8222 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008223 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008224 final int pos = mHistoryBuffer.dataPosition();
8225 if (pos == 0) {
8226 out.clear();
8227 }
8228 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008229 if (end) {
8230 return false;
8231 }
8232
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008233 final long lastRealtime = out.time;
8234 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008235 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008236 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8237 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008238 out.currentTime = lastWalltime + (out.time - lastRealtime);
8239 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008240 return true;
8241 }
8242
8243 @Override
8244 public void finishIteratingHistoryLocked() {
8245 mIteratingHistory = false;
8246 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008247 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008248 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008249
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008250 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008251 public long getHistoryBaseTime() {
8252 return mHistoryBaseTime;
8253 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008254
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008255 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 public int getStartCount() {
8257 return mStartCount;
8258 }
8259
8260 public boolean isOnBattery() {
8261 return mOnBattery;
8262 }
8263
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008264 public boolean isCharging() {
8265 return mCharging;
8266 }
8267
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008268 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008269 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008270 }
8271
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008272 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008273 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008274 mOnBatteryTimeBase.init(uptime, realtime);
8275 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008276 mRealtime = 0;
8277 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008278 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008279 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008280 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008281
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008282 void initDischarge() {
8283 mLowDischargeAmountSinceCharge = 0;
8284 mHighDischargeAmountSinceCharge = 0;
8285 mDischargeAmountScreenOn = 0;
8286 mDischargeAmountScreenOnSinceCharge = 0;
8287 mDischargeAmountScreenOff = 0;
8288 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008289 mDischargeStepTracker.init();
8290 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008291 mDischargeScreenOffCounter.reset(false);
8292 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008293 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008294
8295 public void resetAllStatsCmdLocked() {
8296 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008297 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008298 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008299 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008300 long realtime = mSecRealtime * 1000;
8301 mDischargeStartLevel = mHistoryCur.batteryLevel;
8302 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008303 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008304 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8305 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008306 mOnBatteryTimeBase.reset(uptime, realtime);
8307 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8308 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008309 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008310 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8311 mDischargeScreenOffUnplugLevel = 0;
8312 } else {
8313 mDischargeScreenOnUnplugLevel = 0;
8314 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8315 }
8316 mDischargeAmountScreenOn = 0;
8317 mDischargeAmountScreenOff = 0;
8318 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008319 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008320 }
8321
8322 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008323 final long uptimeMillis = mClocks.uptimeMillis();
8324 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008325 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008326 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008327 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008328 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008329 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008330 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008331
8332 if (mPowerProfile != null) {
8333 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8334 } else {
8335 mEstimatedBatteryCapacity = -1;
8336 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008337 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008338 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008339 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008340 mLongestLightIdleTime = 0;
8341 mLongestFullIdleTime = 0;
8342 mDeviceIdleModeLightTimer.reset(false);
8343 mDeviceIdleModeFullTimer.reset(false);
8344 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008345 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008346 mPhoneOnTimer.reset(false);
8347 mAudioOnTimer.reset(false);
8348 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008349 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008350 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008351 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008352 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008353 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008354 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008355 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008356 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008357 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008358 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008359 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008360 mNetworkByteActivityCounters[i].reset(false);
8361 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008362 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008363 mMobileRadioActiveTimer.reset(false);
8364 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008365 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008366 mMobileRadioActiveUnknownTime.reset(false);
8367 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008368 mWifiOnTimer.reset(false);
8369 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008370 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008371 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008372 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008373 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8374 mWifiSupplStateTimer[i].reset(false);
8375 }
8376 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8377 mWifiSignalStrengthsTimer[i].reset(false);
8378 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008379 mWifiActivity.reset(false);
8380 mBluetoothActivity.reset(false);
8381 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008382 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008383
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008384 for (int i=0; i<mUidStats.size(); i++) {
8385 if (mUidStats.valueAt(i).reset()) {
8386 mUidStats.remove(mUidStats.keyAt(i));
8387 i--;
8388 }
8389 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008390
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008391 if (mKernelWakelockStats.size() > 0) {
8392 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008393 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008394 }
8395 mKernelWakelockStats.clear();
8396 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008397
8398 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008399 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8400 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008401 }
8402 mWakeupReasonStats.clear();
8403 }
8404
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008405 mLastHistoryStepDetails = null;
8406 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8407 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8408 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8409 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8410 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8411 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8412 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8413 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8414 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8415 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8416
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008417 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008418
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008419 clearHistoryLocked();
8420 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008421
Dianne Hackborn40c87252014-03-19 16:55:40 -07008422 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008423 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008424 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8425 // Not recording process starts/stops.
8426 continue;
8427 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008428 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008429 if (active == null) {
8430 continue;
8431 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008432 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8433 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008434 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008435 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8436 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008437 }
8438 }
8439 }
8440 }
8441
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008442 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008443 if (oldScreenOn) {
8444 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8445 if (diff > 0) {
8446 mDischargeAmountScreenOn += diff;
8447 mDischargeAmountScreenOnSinceCharge += diff;
8448 }
8449 } else {
8450 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8451 if (diff > 0) {
8452 mDischargeAmountScreenOff += diff;
8453 mDischargeAmountScreenOffSinceCharge += diff;
8454 }
8455 }
8456 if (newScreenOn) {
8457 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8458 mDischargeScreenOffUnplugLevel = 0;
8459 } else {
8460 mDischargeScreenOnUnplugLevel = 0;
8461 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8462 }
8463 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008464
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008465 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008466 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008467 final boolean screenOn = mScreenState == Display.STATE_ON;
8468 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008469 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008470 }
8471
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008472 private String[] mMobileIfaces = EmptyArray.STRING;
8473 private String[] mWifiIfaces = EmptyArray.STRING;
8474
8475 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8476
8477 private static final int NETWORK_STATS_LAST = 0;
8478 private static final int NETWORK_STATS_NEXT = 1;
8479 private static final int NETWORK_STATS_DELTA = 2;
8480
Joe Onoratoabded112016-02-08 16:49:39 -08008481 private NetworkStats[] mMobileNetworkStats;
8482 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008483
8484 /**
8485 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8486 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8487 */
8488 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8489 NetworkStats[] networkStatsBuffer)
8490 throws IOException {
8491 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8492 false)) {
8493 return null;
8494 }
8495
8496 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8497 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8498 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8499 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8500 networkStatsBuffer[NETWORK_STATS_DELTA]);
8501 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8502 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8503 return networkStatsBuffer[NETWORK_STATS_DELTA];
8504 }
8505
8506 /**
8507 * Distribute WiFi energy info and network traffic to apps.
8508 * @param info The energy information from the WiFi controller.
8509 */
8510 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008511 if (DEBUG_ENERGY) {
8512 Slog.d(TAG, "Updating wifi stats");
8513 }
8514
Joe Onoratoabded112016-02-08 16:49:39 -08008515 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008516 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008517 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008518 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8519 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8520 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008521 } catch (IOException e) {
8522 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8523 return;
8524 }
8525
8526 if (!mOnBatteryInternal) {
8527 return;
8528 }
8529
Adam Lesinskie08af192015-03-25 16:42:59 -07008530 SparseLongArray rxPackets = new SparseLongArray();
8531 SparseLongArray txPackets = new SparseLongArray();
8532 long totalTxPackets = 0;
8533 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008534 if (delta != null) {
8535 final int size = delta.size();
8536 for (int i = 0; i < size; i++) {
8537 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8538
Adam Lesinskie08af192015-03-25 16:42:59 -07008539 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008540 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008541 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8542 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008543 }
8544
Adam Lesinski6cca4142016-02-25 18:23:02 -08008545 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8546 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008547 continue;
8548 }
8549
8550 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008551 if (entry.rxBytes != 0) {
8552 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8553 entry.rxPackets);
8554 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8555 entry.rxBytes);
8556 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8557 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008558
Adam Lesinskiba88e682015-12-08 12:06:55 -08008559 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008560
Adam Lesinskiba88e682015-12-08 12:06:55 -08008561 // Sum the total number of packets so that the Rx Power can
8562 // be evenly distributed amongst the apps.
8563 totalRxPackets += entry.rxPackets;
8564 }
8565
8566 if (entry.txBytes != 0) {
8567 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8568 entry.txPackets);
8569 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8570 entry.txBytes);
8571 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8572 entry.txPackets);
8573
8574 txPackets.put(u.getUid(), entry.txPackets);
8575
8576 // Sum the total number of packets so that the Tx Power can
8577 // be evenly distributed amongst the apps.
8578 totalTxPackets += entry.txPackets;
8579 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008580 }
8581 }
8582
8583 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008584 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008585
Adam Lesinskie08af192015-03-25 16:42:59 -07008586 // Measured in mAms
8587 final long txTimeMs = info.getControllerTxTimeMillis();
8588 final long rxTimeMs = info.getControllerRxTimeMillis();
8589 final long idleTimeMs = info.getControllerIdleTimeMillis();
8590 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8591
8592 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008593 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008594
8595 if (DEBUG_ENERGY) {
8596 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8597 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8598 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8599 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8600 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8601 }
8602
8603 long totalWifiLockTimeMs = 0;
8604 long totalScanTimeMs = 0;
8605
8606 // On the first pass, collect some totals so that we can normalize power
8607 // calculations if we need to.
8608 final int uidStatsSize = mUidStats.size();
8609 for (int i = 0; i < uidStatsSize; i++) {
8610 final Uid uid = mUidStats.valueAt(i);
8611
8612 // Sum the total scan power for all apps.
8613 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8614 elapsedRealtimeMs * 1000) / 1000;
8615
8616 // Sum the total time holding wifi lock for all apps.
8617 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8618 elapsedRealtimeMs * 1000) / 1000;
8619 }
8620
8621 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8622 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8623 + rxTimeMs + " ms). Normalizing scan time.");
8624 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008625 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8626 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8627 + txTimeMs + " ms). Normalizing scan time.");
8628 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008629
8630 // Actually assign and distribute power usage to apps.
8631 for (int i = 0; i < uidStatsSize; i++) {
8632 final Uid uid = mUidStats.valueAt(i);
8633
8634 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8635 elapsedRealtimeMs * 1000) / 1000;
8636 if (scanTimeSinceMarkMs > 0) {
8637 // Set the new mark so that next time we get new data since this point.
8638 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8639
Mitchell Willsf9016492015-07-29 17:47:44 -07008640 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8641 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8642
8643 // Our total scan time is more than the reported Tx/Rx time.
8644 // This is possible because the cost of a scan is approximate.
8645 // Let's normalize the result so that we evenly blame each app
8646 // scanning.
8647 //
8648 // This means that we may have apps that transmitted/received packets not be
8649 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008650 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008651 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8652 totalScanTimeMs;
8653 }
8654 if (totalScanTimeMs > txTimeMs) {
8655 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8656 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008657 }
8658
8659 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008660 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8661 + scanRxTimeSinceMarkMs + " ms Tx:"
8662 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008663 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008664
8665 ControllerActivityCounterImpl activityCounter =
8666 uid.getOrCreateWifiControllerActivityLocked();
8667 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
8668 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07008669 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
8670 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008671 }
8672
8673 // Distribute evenly the power consumed while Idle to each app holding a WiFi
8674 // lock.
8675 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8676 elapsedRealtimeMs * 1000) / 1000;
8677 if (wifiLockTimeSinceMarkMs > 0) {
8678 // Set the new mark so that next time we get new data since this point.
8679 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
8680
8681 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
8682 / totalWifiLockTimeMs;
8683 if (DEBUG_ENERGY) {
8684 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
8685 + myIdleTimeMs + " ms");
8686 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008687 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
8688 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008689 }
8690 }
8691
8692 if (DEBUG_ENERGY) {
8693 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07008694 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07008695 }
8696
Mitchell Willsf9016492015-07-29 17:47:44 -07008697 // Distribute the remaining Tx power appropriately between all apps that transmitted
8698 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07008699 for (int i = 0; i < txPackets.size(); i++) {
8700 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07008701 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07008702 if (DEBUG_ENERGY) {
8703 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
8704 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008705 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
8706 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008707 }
8708
8709 // Distribute the remaining Rx power appropriately between all apps that received
8710 // packets.
8711 for (int i = 0; i < rxPackets.size(); i++) {
8712 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
8713 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
8714 if (DEBUG_ENERGY) {
8715 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
8716 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008717 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
8718 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008719 }
8720
8721 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
8722
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008723 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008724 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
8725 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
8726 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07008727
Adam Lesinski8576cf92015-06-09 12:48:25 -07008728 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8729 final double opVolt = mPowerProfile.getAveragePower(
8730 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8731 if (opVolt != 0) {
8732 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008733 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07008734 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07008735 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008736 }
8737 }
8738
8739 /**
8740 * Distribute Cell radio energy info and network traffic to apps.
8741 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008742 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
8743 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008744 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008745 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008746 }
8747
Adam Lesinskie08af192015-03-25 16:42:59 -07008748 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008749 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008750 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
8751 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
8752 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008753 } catch (IOException e) {
8754 Slog.wtf(TAG, "Failed to get mobile network stats", e);
8755 return;
8756 }
8757
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008758 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008759 return;
8760 }
8761
Adam Lesinskie08af192015-03-25 16:42:59 -07008762 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
8763 elapsedRealtimeMs * 1000);
8764 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008765
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008766 long totalRxPackets = 0;
8767 long totalTxPackets = 0;
8768 if (delta != null) {
8769 final int size = delta.size();
8770 for (int i = 0; i < size; i++) {
8771 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08008772 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008773 continue;
8774 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008775
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008776 if (DEBUG_ENERGY) {
8777 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
8778 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8779 + " txPackets=" + entry.txPackets);
8780 }
8781
8782 totalRxPackets += entry.rxPackets;
8783 totalTxPackets += entry.txPackets;
8784
8785 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8786 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
8787 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
8788
8789 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8790 entry.rxBytes);
8791 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8792 entry.txBytes);
8793 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8794 entry.rxPackets);
8795 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8796 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008797 }
8798
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008799 // Now distribute proportional blame to the apps that did networking.
8800 long totalPackets = totalRxPackets + totalTxPackets;
8801 if (totalPackets > 0) {
8802 for (int i = 0; i < size; i++) {
8803 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8804 if (entry.rxPackets == 0 && entry.txPackets == 0) {
8805 continue;
8806 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008807
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008808 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8809
8810 // Distribute total radio active time in to this app.
8811 final long appPackets = entry.rxPackets + entry.txPackets;
8812 final long appRadioTime = (radioTime * appPackets) / totalPackets;
8813 u.noteMobileRadioActiveTimeLocked(appRadioTime);
8814
8815 // Remove this app from the totals, so that we don't lose any time
8816 // due to rounding.
8817 radioTime -= appRadioTime;
8818 totalPackets -= appPackets;
8819
8820 if (activityInfo != null) {
8821 ControllerActivityCounterImpl activityCounter =
8822 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08008823 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008824 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
8825 / totalRxPackets;
8826 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
8827 }
8828
Adam Lesinskia4268172016-01-29 12:13:54 -08008829 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008830 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8831 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
8832 txMs /= totalTxPackets;
8833 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
8834 }
8835 }
8836 }
8837 }
8838 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008839
8840 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008841 // Whoops, there is some radio time we can't blame on an app!
8842 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
8843 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008844 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008845 }
8846
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008847 if (activityInfo != null) {
8848 mHasModemReporting = true;
8849 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
8850 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
8851 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8852 mModemActivity.getTxTimeCounters()[lvl]
8853 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
8854 }
8855
8856 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8857 final double opVolt = mPowerProfile.getAveragePower(
8858 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8859 if (opVolt != 0) {
8860 // We store the power drain as mAms.
8861 mModemActivity.getPowerCounter().addCountLocked(
8862 (long) (activityInfo.getEnergyUsed() / opVolt));
8863 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008864 }
8865 }
8866
8867 /**
8868 * Distribute Bluetooth energy info and network traffic to apps.
8869 * @param info The energy information from the bluetooth controller.
8870 */
8871 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008872 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008873 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008874 }
8875
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008876 if (info == null || !mOnBatteryInternal) {
8877 return;
8878 }
Adam Lesinskie283d332015-04-16 12:29:25 -07008879
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008880 mHasBluetoothReporting = true;
8881
8882 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
8883 final long rxTimeMs = info.getControllerRxTimeMillis();
8884 final long txTimeMs = info.getControllerTxTimeMillis();
8885
8886 if (DEBUG_ENERGY) {
8887 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
8888 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8889 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8890 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
8891 }
8892
8893 long totalScanTimeMs = 0;
8894
8895 final int uidCount = mUidStats.size();
8896 for (int i = 0; i < uidCount; i++) {
8897 final Uid u = mUidStats.valueAt(i);
8898 if (u.mBluetoothScanTimer == null) {
8899 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07008900 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008901
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008902 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8903 elapsedRealtimeMs * 1000) / 1000;
8904 }
8905
8906 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
8907 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
8908
8909 if (DEBUG_ENERGY) {
8910 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
8911 + " TX=" + normalizeScanTxTime);
8912 }
8913
8914 long leftOverRxTimeMs = rxTimeMs;
8915 long leftOverTxTimeMs = txTimeMs;
8916
8917 for (int i = 0; i < uidCount; i++) {
8918 final Uid u = mUidStats.valueAt(i);
8919 if (u.mBluetoothScanTimer == null) {
8920 continue;
8921 }
8922
8923 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8924 elapsedRealtimeMs * 1000) / 1000;
8925 if (scanTimeSinceMarkMs > 0) {
8926 // Set the new mark so that next time we get new data since this point.
8927 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
8928
8929 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
8930 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
8931
8932 if (normalizeScanRxTime) {
8933 // Scan time is longer than the total rx time in the controller,
8934 // so distribute the scan time proportionately. This means regular traffic
8935 // will not blamed, but scans are more expensive anyways.
8936 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
8937 }
8938
8939 if (normalizeScanTxTime) {
8940 // Scan time is longer than the total tx time in the controller,
8941 // so distribute the scan time proportionately. This means regular traffic
8942 // will not blamed, but scans are more expensive anyways.
8943 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
8944 }
8945
8946 final ControllerActivityCounterImpl counter =
8947 u.getOrCreateBluetoothControllerActivityLocked();
8948 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
8949 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
8950
8951 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
8952 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
8953 }
8954 }
8955
8956 if (DEBUG_ENERGY) {
8957 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
8958 + " TX=" + leftOverTxTimeMs);
8959 }
8960
8961 //
8962 // Now distribute blame to apps that did bluetooth traffic.
8963 //
8964
8965 long totalTxBytes = 0;
8966 long totalRxBytes = 0;
8967
8968 final UidTraffic[] uidTraffic = info.getUidTraffic();
8969 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
8970 for (int i = 0; i < numUids; i++) {
8971 final UidTraffic traffic = uidTraffic[i];
8972
8973 // Add to the global counters.
8974 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
8975 traffic.getRxBytes());
8976 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
8977 traffic.getTxBytes());
8978
8979 // Add to the UID counters.
8980 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
8981 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
8982 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
8983
8984 // Calculate the total traffic.
8985 totalTxBytes += traffic.getTxBytes();
8986 totalRxBytes += traffic.getRxBytes();
8987 }
8988
8989 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
8990 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008991 for (int i = 0; i < numUids; i++) {
8992 final UidTraffic traffic = uidTraffic[i];
8993
Adam Lesinski50e47602015-12-04 17:04:54 -08008994 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008995 final ControllerActivityCounterImpl counter =
8996 u.getOrCreateBluetoothControllerActivityLocked();
8997
8998 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
8999 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9000
9001 if (DEBUG_ENERGY) {
9002 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
9003 + " rx_time=" + timeRxMs);
9004 }
9005 counter.getRxTimeCounter().addCountLocked(timeRxMs);
9006 leftOverRxTimeMs -= timeRxMs;
9007 }
9008
9009 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
9010 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
9011
9012 if (DEBUG_ENERGY) {
9013 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
9014 + " tx_time=" + timeTxMs);
9015 }
9016
9017 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
9018 leftOverTxTimeMs -= timeTxMs;
9019 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009020 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009021 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009022
9023 mBluetoothActivity.getRxTimeCounter().addCountLocked(
9024 info.getControllerRxTimeMillis());
9025 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
9026 info.getControllerTxTimeMillis());
9027 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
9028 info.getControllerIdleTimeMillis());
9029
9030 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9031 final double opVolt = mPowerProfile.getAveragePower(
9032 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9033 if (opVolt != 0) {
9034 // We store the power drain as mAms.
9035 mBluetoothActivity.getPowerCounter().addCountLocked(
9036 (long) (info.getControllerEnergyUsed() / opVolt));
9037 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009038 }
9039
9040 /**
9041 * Read and distribute kernel wake lock use across apps.
9042 */
9043 public void updateKernelWakelocksLocked() {
9044 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
9045 mTmpWakelockStats);
9046 if (wakelockStats == null) {
9047 // Not crashing might make board bringup easier.
9048 Slog.w(TAG, "Couldn't get kernel wake lock stats");
9049 return;
9050 }
9051
9052 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
9053 String name = ent.getKey();
9054 KernelWakelockStats.Entry kws = ent.getValue();
9055
9056 SamplingTimer kwlt = mKernelWakelockStats.get(name);
9057 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009058 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009059 mKernelWakelockStats.put(name, kwlt);
9060 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07009061
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009062 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009063 kwlt.setUpdateVersion(kws.mVersion);
9064 }
9065
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009066 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07009067 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
9068 // this time.
9069 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9070 SamplingTimer st = ent.getValue();
9071 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
9072 st.endSample();
9073 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009074 }
9075 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009076
Adam Lesinskid84ad302016-05-17 18:31:02 -07009077 // Record whether we've seen a non-zero time (for debugging b/22716723).
9078 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009079 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
9080 }
9081
9082 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
9083 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
9084 wakelockStats.kernelWakelockVersion);
9085 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009086 }
9087
Adam Lesinski72478f02015-06-17 15:39:43 -07009088 // We use an anonymous class to access these variables,
9089 // so they can't live on the stack or they'd have to be
9090 // final MutableLong objects (more allocations).
9091 // Used in updateCpuTimeLocked().
9092 long mTempTotalCpuUserTimeUs;
9093 long mTempTotalCpuSystemTimeUs;
9094
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009095 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07009096 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
9097 * and we are on battery with screen off, we give more of the cpu time to those apps holding
9098 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009099 */
Adam Lesinski72478f02015-06-17 15:39:43 -07009100 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009101 if (mPowerProfile == null) {
9102 return;
9103 }
9104
Adam Lesinski72478f02015-06-17 15:39:43 -07009105 if (DEBUG_ENERGY_CPU) {
9106 Slog.d(TAG, "!Cpu updating!");
9107 }
9108
9109 // Holding a wakelock costs more than just using the cpu.
9110 // Currently, we assign only half the cpu time to an app that is running but
9111 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
9112 // If no app is holding a wakelock, then the distribution is normal.
9113 final int wakelockWeight = 50;
9114
Adam Lesinski6832f392015-09-05 18:05:40 -07009115 // Read the time spent for each cluster at various cpu frequencies.
9116 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
9117 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
9118 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
9119 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009120
9121 int numWakelocks = 0;
9122
9123 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
9124 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
9125 final int numPartialTimers = mPartialTimers.size();
9126 if (mOnBatteryScreenOffTimeBase.isRunning()) {
9127 for (int i = 0; i < numPartialTimers; i++) {
9128 final StopwatchTimer timer = mPartialTimers.get(i);
9129 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9130 // Since the collection and blaming of wakelocks can be scheduled to run after
9131 // some delay, the mPartialTimers list may have new entries. We can't blame
9132 // the newly added timer for past cpu time, so we only consider timers that
9133 // were present for one round of collection. Once a timer has gone through
9134 // a round of collection, its mInList field is set to true.
9135 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009136 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009137 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009138 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009139
9140 final int numWakelocksF = numWakelocks;
9141 mTempTotalCpuUserTimeUs = 0;
9142 mTempTotalCpuSystemTimeUs = 0;
9143
9144 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9145 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9146 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009147 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009148 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9149 new KernelUidCpuTimeReader.Callback() {
9150 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009151 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
9152 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009153 final Uid u = getUidStatsLocked(mapUid(uid));
9154
9155 // Accumulate the total system and user time.
9156 mTempTotalCpuUserTimeUs += userTimeUs;
9157 mTempTotalCpuSystemTimeUs += systemTimeUs;
9158
9159 StringBuilder sb = null;
9160 if (DEBUG_ENERGY_CPU) {
9161 sb = new StringBuilder();
9162 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9163 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9164 sb.append(" s=");
9165 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009166 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009167 }
9168
9169 if (numWakelocksF > 0) {
9170 // We have wakelocks being held, so only give a portion of the
9171 // time to the process. The rest will be distributed among wakelock
9172 // holders.
9173 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9174 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9175 }
9176
9177 if (sb != null) {
9178 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9179 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9180 sb.append(" s=");
9181 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009182 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009183 Slog.d(TAG, sb.toString());
9184 }
9185
9186 u.mUserCpuTime.addCountLocked(userTimeUs);
9187 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009188 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009189
9190 // Add the cpu speeds to this UID. These are used as a ratio
9191 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009192 final int numClusters = mPowerProfile.getNumCpuClusters();
9193 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9194 numClusters) {
9195 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009196 }
9197
9198 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009199 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9200 cluster);
9201 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9202 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009203 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009204 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009205 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009206
9207 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9208 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9209 if (cpuSpeeds[speed] == null) {
9210 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9211 }
9212 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9213 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009214 }
9215 }
9216 });
9217
9218 if (DEBUG_ENERGY_CPU) {
Joe Onoratoabded112016-02-08 16:49:39 -08009219 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009220 " ms");
9221 }
9222
9223 if (mOnBatteryInternal && numWakelocks > 0) {
9224 // Distribute a portion of the total cpu time to wakelock holders.
9225 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9226 mTempTotalCpuSystemTimeUs =
9227 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9228
9229 for (int i = 0; i < numPartialTimers; i++) {
9230 final StopwatchTimer timer = mPartialTimers.get(i);
9231
9232 // The system does not share any blame, as it is usually holding the wakelock
9233 // on behalf of an app.
9234 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9235 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9236 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9237
9238 if (DEBUG_ENERGY_CPU) {
9239 StringBuilder sb = new StringBuilder();
9240 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9241 .append(": u=");
9242 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9243 sb.append(" s=");
9244 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9245 Slog.d(TAG, sb.toString());
9246 }
9247
9248 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9249 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9250
9251 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009252 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009253
9254 mTempTotalCpuUserTimeUs -= userTimeUs;
9255 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9256 numWakelocks--;
9257 }
9258 }
9259
9260 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9261 // Anything left over is given to the system.
9262 if (DEBUG_ENERGY_CPU) {
9263 StringBuilder sb = new StringBuilder();
9264 sb.append(" Distributing lost time to system: u=");
9265 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9266 sb.append(" s=");
9267 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9268 Slog.d(TAG, sb.toString());
9269 }
9270
9271 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9272 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9273 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9274
9275 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009276 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9277 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009278 }
9279 }
9280
9281 // See if there is a difference in wakelocks between this collection and the last
9282 // collection.
9283 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9284 // No difference, so each timer is now considered for the next collection.
9285 for (int i = 0; i < numPartialTimers; i++) {
9286 mPartialTimers.get(i).mInList = true;
9287 }
9288 } else {
9289 // The lists are different, meaning we added (or removed a timer) since the last
9290 // collection.
9291 final int numLastPartialTimers = mLastPartialTimers.size();
9292 for (int i = 0; i < numLastPartialTimers; i++) {
9293 mLastPartialTimers.get(i).mInList = false;
9294 }
9295 mLastPartialTimers.clear();
9296
9297 // Mark the current timers as gone through a collection.
9298 for (int i = 0; i < numPartialTimers; i++) {
9299 final StopwatchTimer timer = mPartialTimers.get(i);
9300 timer.mInList = true;
9301 mLastPartialTimers.add(timer);
9302 }
9303 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009304 }
9305
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009306 boolean setChargingLocked(boolean charging) {
9307 if (mCharging != charging) {
9308 mCharging = charging;
9309 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009310 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009311 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009312 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009313 }
9314 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9315 return true;
9316 }
9317 return false;
9318 }
9319
Dianne Hackborn40c87252014-03-19 16:55:40 -07009320 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009321 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009322 boolean doWrite = false;
9323 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9324 m.arg1 = onBattery ? 1 : 0;
9325 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009326
Dianne Hackborn40c87252014-03-19 16:55:40 -07009327 final long uptime = mSecUptime * 1000;
9328 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009329 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009330 if (onBattery) {
9331 // We will reset our status if we are unplugging after the
9332 // battery was last full, or the level is at 100, or
9333 // we have gone through a significant charge (from a very low
9334 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009335 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009336 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009337 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009338 || (mDischargeCurrentLevel < 20 && level >= 80)
9339 || (getHighDischargeAmountSinceCharge() >= 200
9340 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009341 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009342 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009343 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9344 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009345 // Before we write, collect a snapshot of the final aggregated
9346 // stats to be reported in the next checkin. Only do this if we have
9347 // a sufficient amount of data to make it interesting.
9348 if (getLowDischargeAmountSinceCharge() >= 20) {
9349 final Parcel parcel = Parcel.obtain();
9350 writeSummaryToParcel(parcel, true);
9351 BackgroundThread.getHandler().post(new Runnable() {
9352 @Override public void run() {
9353 synchronized (mCheckinFile) {
9354 FileOutputStream stream = null;
9355 try {
9356 stream = mCheckinFile.startWrite();
9357 stream.write(parcel.marshall());
9358 stream.flush();
9359 FileUtils.sync(stream);
9360 stream.close();
9361 mCheckinFile.finishWrite(stream);
9362 } catch (IOException e) {
9363 Slog.w("BatteryStats",
9364 "Error writing checkin battery statistics", e);
9365 mCheckinFile.failWrite(stream);
9366 } finally {
9367 parcel.recycle();
9368 }
9369 }
9370 }
9371 });
9372 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009373 doWrite = true;
9374 resetAllStatsLocked();
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009375 if (chargeUAh > 0) {
9376 // Only use the reported coulomb charge value if it is supported and reported.
9377 mEstimatedBatteryCapacity = (int) ((level / 100.0) * (chargeUAh / 1000));
9378 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009379 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009380 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009381 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009382 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009383 if (mCharging) {
9384 setChargingLocked(false);
9385 }
9386 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009387 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009388 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009389 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009390 mDischargeStepTracker.clearTime();
9391 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009392 mInitStepMode = mCurStepMode;
9393 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009394 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009395 mHistoryCur.batteryLevel = (byte)level;
9396 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9397 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9398 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009399 if (reset) {
9400 mRecordingHistory = true;
9401 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9402 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009403 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009404 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009405 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009406 mDischargeScreenOnUnplugLevel = level;
9407 mDischargeScreenOffUnplugLevel = 0;
9408 } else {
9409 mDischargeScreenOnUnplugLevel = 0;
9410 mDischargeScreenOffUnplugLevel = level;
9411 }
9412 mDischargeAmountScreenOn = 0;
9413 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009414 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009415 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009416 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009417 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009418 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009419 mHistoryCur.batteryLevel = (byte)level;
9420 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9421 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9422 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009423 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009424 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009425 if (level < mDischargeUnplugLevel) {
9426 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9427 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9428 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009429 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9430 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009431 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009432 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009433 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009434 mInitStepMode = mCurStepMode;
9435 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009436 }
9437 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9438 if (mFile != null) {
9439 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 }
9441 }
9442 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009443
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009444 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9445 boolean reset) {
9446 mRecordingHistory = true;
9447 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009448 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9449 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009450 mHistoryCur);
9451 mHistoryCur.currentTime = 0;
9452 if (reset) {
9453 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9454 }
9455 }
9456
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009457 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9458 final long uptimeMs) {
9459 if (mRecordingHistory) {
9460 mHistoryCur.currentTime = currentTime;
9461 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9462 mHistoryCur);
9463 mHistoryCur.currentTime = 0;
9464 }
9465 }
9466
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009467 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9468 if (mRecordingHistory) {
9469 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009470 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9471 mHistoryCur);
9472 mHistoryCur.currentTime = 0;
9473 }
9474 }
9475
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009476 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009477 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009478 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009479 }
9480 }
9481
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009482 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009483 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009484
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009485 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009486 int temp, int volt, int chargeUAh) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009487 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009488 final long uptime = mClocks.uptimeMillis();
9489 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009490 if (!mHaveBatteryLevel) {
9491 mHaveBatteryLevel = true;
9492 // We start out assuming that the device is plugged in (not
9493 // on battery). If our first report is now that we are indeed
9494 // plugged in, then twiddle our state to correctly reflect that
9495 // since we won't be going through the full setOnBattery().
9496 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009497 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009498 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009499 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009500 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009501 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009502 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009503 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009504 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009505 mHistoryCur.batteryStatus = (byte)status;
9506 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009507 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009508 mMaxChargeStepLevel = mMinDischargeStepLevel =
9509 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009510 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009511 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9512 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9513 }
9514 int oldStatus = mHistoryCur.batteryStatus;
9515 if (onBattery) {
9516 mDischargeCurrentLevel = level;
9517 if (!mRecordingHistory) {
9518 mRecordingHistory = true;
9519 startRecordingHistory(elapsedRealtime, uptime, true);
9520 }
9521 } else if (level < 96) {
9522 if (!mRecordingHistory) {
9523 mRecordingHistory = true;
9524 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009525 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009526 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009527 mCurrentBatteryLevel = level;
9528 if (mDischargePlugLevel < 0) {
9529 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009530 }
Adam Lesinski926969b2016-04-28 17:31:12 -07009531
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009532 if (onBattery != mOnBattery) {
9533 mHistoryCur.batteryLevel = (byte)level;
9534 mHistoryCur.batteryStatus = (byte)status;
9535 mHistoryCur.batteryHealth = (byte)health;
9536 mHistoryCur.batteryPlugType = (byte)plugType;
9537 mHistoryCur.batteryTemperature = (short)temp;
9538 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009539 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9540 // Only record discharges
9541 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9542 mDischargeCounter.addCountLocked(chargeDiff);
9543 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9544 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009545 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009546 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009547 } else {
9548 boolean changed = false;
9549 if (mHistoryCur.batteryLevel != level) {
9550 mHistoryCur.batteryLevel = (byte)level;
9551 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009552
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009553 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9554 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009555 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009556 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009557 if (mHistoryCur.batteryStatus != status) {
9558 mHistoryCur.batteryStatus = (byte)status;
9559 changed = true;
9560 }
9561 if (mHistoryCur.batteryHealth != health) {
9562 mHistoryCur.batteryHealth = (byte)health;
9563 changed = true;
9564 }
9565 if (mHistoryCur.batteryPlugType != plugType) {
9566 mHistoryCur.batteryPlugType = (byte)plugType;
9567 changed = true;
9568 }
9569 if (temp >= (mHistoryCur.batteryTemperature+10)
9570 || temp <= (mHistoryCur.batteryTemperature-10)) {
9571 mHistoryCur.batteryTemperature = (short)temp;
9572 changed = true;
9573 }
9574 if (volt > (mHistoryCur.batteryVoltage+20)
9575 || volt < (mHistoryCur.batteryVoltage-20)) {
9576 mHistoryCur.batteryVoltage = (char)volt;
9577 changed = true;
9578 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009579 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
9580 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009581 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9582 // Only record discharges
9583 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9584 mDischargeCounter.addCountLocked(chargeDiff);
9585 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9586 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009587 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07009588 changed = true;
9589 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009590 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9591 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9592 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9593 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009594 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009595 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9596 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9597 modeBits, elapsedRealtime);
9598 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9599 modeBits, elapsedRealtime);
9600 mLastDischargeStepLevel = level;
9601 mMinDischargeStepLevel = level;
9602 mInitStepMode = mCurStepMode;
9603 mModStepMode = 0;
9604 }
9605 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009606 if (level >= 90) {
9607 // If the battery level is at least 90%, always consider the device to be
9608 // charging even if it happens to go down a level.
9609 changed |= setChargingLocked(true);
9610 mLastChargeStepLevel = level;
9611 } if (!mCharging) {
9612 if (mLastChargeStepLevel < level) {
9613 // We have not reporting that we are charging, but the level has now
9614 // gone up, so consider the state to be charging.
9615 changed |= setChargingLocked(true);
9616 mLastChargeStepLevel = level;
9617 }
9618 } else {
9619 if (mLastChargeStepLevel > level) {
9620 // We had reported that the device was charging, but here we are with
9621 // power connected and the level going down. Looks like the current
9622 // power supplied isn't enough, so consider the device to now be
9623 // discharging.
9624 changed |= setChargingLocked(false);
9625 mLastChargeStepLevel = level;
9626 }
9627 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009628 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9629 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9630 modeBits, elapsedRealtime);
9631 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9632 modeBits, elapsedRealtime);
9633 mLastChargeStepLevel = level;
9634 mMaxChargeStepLevel = level;
9635 mInitStepMode = mCurStepMode;
9636 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009637 }
9638 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009639 if (changed) {
9640 addHistoryRecordLocked(elapsedRealtime, uptime);
9641 }
Evan Millarc64edde2009-04-18 12:26:32 -07009642 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009643 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
9644 // We don't record history while we are plugged in and fully charged.
9645 // The next time we are unplugged, history will be cleared.
9646 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08009647 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009648 }
9649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650 public long getAwakeTimeBattery() {
9651 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
9652 }
9653
9654 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -08009655 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009656 }
9657
9658 @Override
9659 public long computeUptime(long curTime, int which) {
9660 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009661 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009663 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009664 }
9665 return 0;
9666 }
9667
9668 @Override
9669 public long computeRealtime(long curTime, int which) {
9670 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009671 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009672 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009673 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 }
9675 return 0;
9676 }
9677
9678 @Override
9679 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009680 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 }
9682
9683 @Override
9684 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009685 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 }
9687
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009688 @Override
9689 public long computeBatteryScreenOffUptime(long curTime, int which) {
9690 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
9691 }
9692
9693 @Override
9694 public long computeBatteryScreenOffRealtime(long curTime, int which) {
9695 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 }
9697
Dianne Hackborn260c5022014-04-29 11:23:16 -07009698 private long computeTimePerLevel(long[] steps, int numSteps) {
9699 // For now we'll do a simple average across all steps.
9700 if (numSteps <= 0) {
9701 return -1;
9702 }
9703 long total = 0;
9704 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009705 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009706 }
9707 return total / numSteps;
9708 /*
9709 long[] buckets = new long[numSteps];
9710 int numBuckets = 0;
9711 int numToAverage = 4;
9712 int i = 0;
9713 while (i < numSteps) {
9714 long totalTime = 0;
9715 int num = 0;
9716 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009717 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009718 num++;
9719 }
9720 buckets[numBuckets] = totalTime / num;
9721 numBuckets++;
9722 numToAverage *= 2;
9723 i += num;
9724 }
9725 if (numBuckets < 1) {
9726 return -1;
9727 }
9728 long averageTime = buckets[numBuckets-1];
9729 for (i=numBuckets-2; i>=0; i--) {
9730 averageTime = (averageTime + buckets[i]) / 2;
9731 }
9732 return averageTime;
9733 */
9734 }
9735
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009736 @Override
9737 public long computeBatteryTimeRemaining(long curTime) {
9738 if (!mOnBattery) {
9739 return -1;
9740 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009741 /* Simple implementation just looks at the average discharge per level across the
9742 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009743 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
9744 if (discharge < 2) {
9745 return -1;
9746 }
9747 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
9748 if (duration < 1000*1000) {
9749 return -1;
9750 }
9751 long usPerLevel = duration/discharge;
9752 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009753 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009754 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009755 return -1;
9756 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009757 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009758 if (msPerLevel <= 0) {
9759 return -1;
9760 }
9761 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009762 }
9763
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009764 @Override
9765 public LevelStepTracker getDischargeLevelStepTracker() {
9766 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009767 }
9768
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009769 @Override
9770 public LevelStepTracker getDailyDischargeLevelStepTracker() {
9771 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009772 }
9773
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009774 @Override
9775 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009776 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009777 // Not yet working.
9778 return -1;
9779 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009780 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009781 int curLevel = mCurrentBatteryLevel;
9782 int plugLevel = mDischargePlugLevel;
9783 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
9784 return -1;
9785 }
9786 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
9787 if (duration < 1000*1000) {
9788 return -1;
9789 }
9790 long usPerLevel = duration/(curLevel-plugLevel);
9791 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07009792 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009793 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009794 return -1;
9795 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009796 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009797 if (msPerLevel <= 0) {
9798 return -1;
9799 }
9800 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009801 }
9802
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009803 @Override
9804 public LevelStepTracker getChargeLevelStepTracker() {
9805 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009806 }
9807
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009808 @Override
9809 public LevelStepTracker getDailyChargeLevelStepTracker() {
9810 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009811 }
9812
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009813 @Override
9814 public ArrayList<PackageChange> getDailyPackageChanges() {
9815 return mDailyPackageChanges;
9816 }
9817
Joe Onoratoe1acd632016-02-23 13:25:10 -08009818 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009819 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 }
9821
9822 @Override
9823 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009824 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 }
9826
9827 @Override
9828 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009829 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07009831
The Android Open Source Project10592532009-03-18 17:39:46 -07009832 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009833 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009834 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009835 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009836 }
9837 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009838
Evan Millar633a1742009-04-02 16:36:33 -07009839 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009840 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009841 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009842
The Android Open Source Project10592532009-03-18 17:39:46 -07009843 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009844 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009845 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009846 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009847 }
9848 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009849
Evan Millar633a1742009-04-02 16:36:33 -07009850 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009851 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853
Amith Yamasanie43530a2009-08-21 13:11:37 -07009854 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009855 public int getLowDischargeAmountSinceCharge() {
9856 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009857 int val = mLowDischargeAmountSinceCharge;
9858 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9859 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
9860 }
9861 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009862 }
9863 }
9864
9865 @Override
9866 public int getHighDischargeAmountSinceCharge() {
9867 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009868 int val = mHighDischargeAmountSinceCharge;
9869 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9870 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
9871 }
9872 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009873 }
9874 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009875
9876 @Override
9877 public int getDischargeAmount(int which) {
9878 int dischargeAmount = which == STATS_SINCE_CHARGED
9879 ? getHighDischargeAmountSinceCharge()
9880 : (getDischargeStartLevel() - getDischargeCurrentLevel());
9881 if (dischargeAmount < 0) {
9882 dischargeAmount = 0;
9883 }
9884 return dischargeAmount;
9885 }
9886
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009887 public int getDischargeAmountScreenOn() {
9888 synchronized(this) {
9889 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009890 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009891 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9892 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9893 }
9894 return val;
9895 }
9896 }
9897
9898 public int getDischargeAmountScreenOnSinceCharge() {
9899 synchronized(this) {
9900 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009901 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009902 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9903 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9904 }
9905 return val;
9906 }
9907 }
9908
9909 public int getDischargeAmountScreenOff() {
9910 synchronized(this) {
9911 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009912 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009913 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9914 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9915 }
9916 return val;
9917 }
9918 }
9919
9920 public int getDischargeAmountScreenOffSinceCharge() {
9921 synchronized(this) {
9922 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009923 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009924 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9925 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9926 }
9927 return val;
9928 }
9929 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 /**
9932 * Retrieve the statistics object for a particular uid, creating if needed.
9933 */
9934 public Uid getUidStatsLocked(int uid) {
9935 Uid u = mUidStats.get(uid);
9936 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009937 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 mUidStats.put(uid, u);
9939 }
9940 return u;
9941 }
9942
9943 /**
9944 * Remove the statistics object for a particular uid.
9945 */
9946 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07009947 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 mUidStats.remove(uid);
9949 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07009950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 /**
9952 * Retrieve the statistics object for a particular process, creating
9953 * if needed.
9954 */
9955 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009956 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 Uid u = getUidStatsLocked(uid);
9958 return u.getProcessStatsLocked(name);
9959 }
9960
9961 /**
9962 * Retrieve the statistics object for a particular process, creating
9963 * if needed.
9964 */
9965 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009966 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 Uid u = getUidStatsLocked(uid);
9968 return u.getPackageStatsLocked(pkg);
9969 }
9970
9971 /**
9972 * Retrieve the statistics object for a particular service, creating
9973 * if needed.
9974 */
9975 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009976 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009977 Uid u = getUidStatsLocked(uid);
9978 return u.getServiceStatsLocked(pkg, name);
9979 }
9980
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009981 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009982 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009983 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009984 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009985 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009986
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009987 Parcel mPendingWrite = null;
9988 final ReentrantLock mWriteLock = new ReentrantLock();
9989
9990 public void writeAsyncLocked() {
9991 writeLocked(false);
9992 }
9993
9994 public void writeSyncLocked() {
9995 writeLocked(true);
9996 }
9997
9998 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009999 if (mFile == null) {
10000 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010001 return;
10002 }
10003
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010004 if (mShuttingDown) {
10005 return;
10006 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010007
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010008 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010009 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080010010 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010011
10012 if (mPendingWrite != null) {
10013 mPendingWrite.recycle();
10014 }
10015 mPendingWrite = out;
10016
10017 if (sync) {
10018 commitPendingDataToDisk();
10019 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010020 BackgroundThread.getHandler().post(new Runnable() {
10021 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010022 commitPendingDataToDisk();
10023 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010024 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010025 }
10026 }
10027
10028 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010029 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010030 synchronized (this) {
10031 next = mPendingWrite;
10032 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010033 if (next == null) {
10034 return;
10035 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010036
10037 mWriteLock.lock();
10038 }
10039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010041 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010042 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070010044 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010046 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010047 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010048 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010049 mFile.rollback();
10050 } finally {
10051 next.recycle();
10052 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070010053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 }
10055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010056 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010057 if (mDailyFile != null) {
10058 readDailyStatsLocked();
10059 }
10060
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010061 if (mFile == null) {
10062 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 return;
10064 }
10065
10066 mUidStats.clear();
10067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010069 File file = mFile.chooseForRead();
10070 if (!file.exists()) {
10071 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010073 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010074
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010075 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 Parcel in = Parcel.obtain();
10077 in.unmarshall(raw, 0, raw.length);
10078 in.setDataPosition(0);
10079 stream.close();
10080
10081 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080010082 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010083 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010084 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010086
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010087 mEndPlatformVersion = Build.ID;
10088
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010089 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010090 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080010091 final long elapsedRealtime = mClocks.elapsedRealtime();
10092 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010093 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010094 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010095 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010096 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
10097 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010098 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010099
10100 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 }
10102
10103 public int describeContents() {
10104 return 0;
10105 }
10106
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010107 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070010108 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010109
10110 mHistoryBuffer.setDataSize(0);
10111 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010112 mHistoryTagPool.clear();
10113 mNextHistoryTagIdx = 0;
10114 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010115
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010116 int numTags = in.readInt();
10117 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080010118 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010119 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010120 if (str == null) {
10121 throw new ParcelFormatException("null history tag string");
10122 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010123 int uid = in.readInt();
10124 HistoryTag tag = new HistoryTag();
10125 tag.string = str;
10126 tag.uid = uid;
10127 tag.poolIdx = idx;
10128 mHistoryTagPool.put(tag, idx);
10129 if (idx >= mNextHistoryTagIdx) {
10130 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010131 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010132 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010133 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010134
10135 int bufSize = in.readInt();
10136 int curPos = in.dataPosition();
10137 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010138 throw new ParcelFormatException("File corrupt: history data buffer too large " +
10139 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010140 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010141 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
10142 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010143 } else {
10144 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
10145 + " bytes at " + curPos);
10146 mHistoryBuffer.appendFrom(in, curPos, bufSize);
10147 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010148 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010149
Dianne Hackbornae384452011-06-28 12:33:48 -070010150 if (andOldHistory) {
10151 readOldHistory(in);
10152 }
10153
10154 if (DEBUG_HISTORY) {
10155 StringBuilder sb = new StringBuilder(128);
10156 sb.append("****************** OLD mHistoryBaseTime: ");
10157 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10158 Slog.i(TAG, sb.toString());
10159 }
10160 mHistoryBaseTime = historyBaseTime;
10161 if (DEBUG_HISTORY) {
10162 StringBuilder sb = new StringBuilder(128);
10163 sb.append("****************** NEW mHistoryBaseTime: ");
10164 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10165 Slog.i(TAG, sb.toString());
10166 }
10167
10168 // We are just arbitrarily going to insert 1 minute from the sample of
10169 // the last run until samples in this run.
10170 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080010171 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010172 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070010173 if (DEBUG_HISTORY) {
10174 StringBuilder sb = new StringBuilder(128);
10175 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
10176 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10177 Slog.i(TAG, sb.toString());
10178 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070010179 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010180 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010181
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010182 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010183 if (!USE_OLD_HISTORY) {
10184 return;
10185 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010186 mHistory = mHistoryEnd = mHistoryCache = null;
10187 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010188 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010189 HistoryItem rec = new HistoryItem(time, in);
10190 addHistoryRecordLocked(rec);
10191 }
10192 }
10193
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010194 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010195 if (DEBUG_HISTORY) {
10196 StringBuilder sb = new StringBuilder(128);
10197 sb.append("****************** WRITING mHistoryBaseTime: ");
10198 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010199 sb.append(" mLastHistoryElapsedRealtime: ");
10200 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010201 Slog.i(TAG, sb.toString());
10202 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010203 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010204 if (!inclData) {
10205 out.writeInt(0);
10206 out.writeInt(0);
10207 return;
10208 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010209 out.writeInt(mHistoryTagPool.size());
10210 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10211 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010212 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010213 out.writeString(tag.string);
10214 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010215 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010216 out.writeInt(mHistoryBuffer.dataSize());
10217 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10218 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10219 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010220
10221 if (andOldHistory) {
10222 writeOldHistory(out);
10223 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010224 }
10225
10226 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010227 if (!USE_OLD_HISTORY) {
10228 return;
10229 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010230 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010231 while (rec != null) {
10232 if (rec.time >= 0) rec.writeToParcel(out, 0);
10233 rec = rec.next;
10234 }
10235 out.writeLong(-1);
10236 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010237
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010238 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010239 final int version = in.readInt();
10240 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010241 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010242 + ", expected " + VERSION + "; erasing old stats");
10243 return;
10244 }
10245
Dianne Hackbornae384452011-06-28 12:33:48 -070010246 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010248 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010249 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010251 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010252 mStartPlatformVersion = in.readString();
10253 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010254 mOnBatteryTimeBase.readSummaryFromParcel(in);
10255 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010256 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010257 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010258 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010259 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010260 mEstimatedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010261 mLowDischargeAmountSinceCharge = in.readInt();
10262 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010263 mDischargeAmountScreenOnSinceCharge = in.readInt();
10264 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010265 mDischargeStepTracker.readFromParcel(in);
10266 mChargeStepTracker.readFromParcel(in);
10267 mDailyDischargeStepTracker.readFromParcel(in);
10268 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010269 mDischargeCounter.readSummaryFromParcelLocked(in);
10270 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010271 int NPKG = in.readInt();
10272 if (NPKG > 0) {
10273 mDailyPackageChanges = new ArrayList<>(NPKG);
10274 while (NPKG > 0) {
10275 NPKG--;
10276 PackageChange pc = new PackageChange();
10277 pc.mPackageName = in.readString();
10278 pc.mUpdate = in.readInt() != 0;
10279 pc.mVersionCode = in.readInt();
10280 mDailyPackageChanges.add(pc);
10281 }
10282 } else {
10283 mDailyPackageChanges = null;
10284 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010285 mDailyStartTime = in.readLong();
10286 mNextMinDailyDeadline = in.readLong();
10287 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010289 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010290
Jeff Browne95c3cd2014-05-02 16:59:26 -070010291 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010292 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010293 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10294 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10295 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010296 mInteractive = false;
10297 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010299 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010300 mLongestLightIdleTime = in.readLong();
10301 mLongestFullIdleTime = in.readLong();
10302 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10303 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10304 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010305 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010307 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010308 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10309 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010310 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010311 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10312 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10313 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010314 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010315 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10316 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010317 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010318 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010319 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010320 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010321 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010322 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10323 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010324 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010325 mWifiOn = false;
10326 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010327 mGlobalWifiRunning = false;
10328 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010329 for (int i=0; i<NUM_WIFI_STATES; i++) {
10330 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10331 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010332 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10333 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10334 }
10335 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10336 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10337 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010338 mWifiActivity.readSummaryFromParcel(in);
10339 mBluetoothActivity.readSummaryFromParcel(in);
10340 mModemActivity.readSummaryFromParcel(in);
10341 mHasWifiReporting = in.readInt() != 0;
10342 mHasBluetoothReporting = in.readInt() != 0;
10343 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010344
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010345 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010346 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010347 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010348 mCameraOnNesting = 0;
10349 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010350 mBluetoothScanNesting = 0;
10351 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352
Evan Millarc64edde2009-04-18 12:26:32 -070010353 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010354 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010355 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010356 }
Evan Millarc64edde2009-04-18 12:26:32 -070010357 for (int ikw = 0; ikw < NKW; ikw++) {
10358 if (in.readInt() != 0) {
10359 String kwltName = in.readString();
10360 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10361 }
10362 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010363
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010364 int NWR = in.readInt();
10365 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010366 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010367 }
10368 for (int iwr = 0; iwr < NWR; iwr++) {
10369 if (in.readInt() != 0) {
10370 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010371 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010372 }
10373 }
10374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010376 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010377 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 for (int iu = 0; iu < NU; iu++) {
10380 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010381 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 mUidStats.put(uid, u);
10383
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010384 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010385 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010386 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010387 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010388 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010389 if (in.readInt() != 0) {
10390 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10391 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010392 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010393 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010394 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010395 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010396 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10397 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10398 if (in.readInt() != 0) {
10399 u.makeWifiBatchedScanBin(i, null);
10400 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10401 }
10402 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010403 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010404 if (in.readInt() != 0) {
10405 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10406 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010407 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010408 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010409 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010410 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010411 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10412 }
10413 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010414 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10415 }
10416 if (in.readInt() != 0) {
10417 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10418 }
10419 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010420 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10421 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010422 if (in.readInt() != 0) {
10423 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10424 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010425 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010426 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10427 if (in.readInt() != 0) {
10428 u.makeProcessState(i, null);
10429 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10430 }
10431 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010432 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010433 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010434 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010435
Dianne Hackborn617f8772009-03-31 15:04:46 -070010436 if (in.readInt() != 0) {
10437 if (u.mUserActivityCounters == null) {
10438 u.initUserActivityLocked();
10439 }
10440 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10441 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10442 }
10443 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010444
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010445 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010446 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010447 u.initNetworkActivityLocked();
10448 }
10449 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010450 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10451 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010452 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010453 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10454 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010455 }
10456
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010457 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10458 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010459 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010460
Adam Lesinski6832f392015-09-05 18:05:40 -070010461 if (in.readInt() != 0) {
10462 final int numClusters = in.readInt();
10463 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10464 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010465 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010466
10467 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10468 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010469 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010470 final int NSB = in.readInt();
10471 if (mPowerProfile != null &&
10472 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10473 throw new ParcelFormatException("File corrupt: too many speed bins " +
10474 NSB);
10475 }
10476
Adam Lesinski6832f392015-09-05 18:05:40 -070010477 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10478 for (int speed = 0; speed < NSB; speed++) {
10479 if (in.readInt() != 0) {
10480 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10481 mOnBatteryTimeBase);
10482 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10483 }
10484 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010485 } else {
10486 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010487 }
10488 }
10489 } else {
10490 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010491 }
10492
Adam Lesinski5f056f62016-07-14 16:56:08 -070010493 if (in.readInt() != 0) {
10494 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10495 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
10496 } else {
10497 u.mMobileRadioApWakeupCount = null;
10498 }
10499
10500 if (in.readInt() != 0) {
10501 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10502 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
10503 } else {
10504 u.mWifiRadioApWakeupCount = null;
10505 }
10506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010508 if (NW > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010509 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 for (int iw = 0; iw < NW; iw++) {
10512 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010513 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 }
10515
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010516 int NS = in.readInt();
10517 if (NS > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010518 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010519 }
10520 for (int is = 0; is < NS; is++) {
10521 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010522 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010523 }
10524
10525 int NJ = in.readInt();
10526 if (NJ > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010527 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010528 }
10529 for (int ij = 0; ij < NJ; ij++) {
10530 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010531 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010532 }
10533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010535 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010536 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010538 for (int is = 0; is < NP; is++) {
10539 int seNumber = in.readInt();
10540 if (in.readInt() != 0) {
10541 u.getSensorTimerLocked(seNumber, true)
10542 .readSummaryFromParcelLocked(in);
10543 }
10544 }
10545
10546 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010547 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010548 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 for (int ip = 0; ip < NP; ip++) {
10551 String procName = in.readString();
10552 Uid.Proc p = u.getProcessStatsLocked(procName);
10553 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010555 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010557 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10558 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010559 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 }
10561
10562 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010563 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010564 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 for (int ip = 0; ip < NP; ip++) {
10567 String pkgName = in.readString();
10568 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010569 final int NWA = in.readInt();
10570 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010571 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010572 }
10573 p.mWakeupAlarms.clear();
10574 for (int iwa=0; iwa<NWA; iwa++) {
10575 String tag = in.readString();
10576 Counter c = new Counter(mOnBatteryTimeBase);
10577 c.readSummaryFromParcelLocked(in);
10578 p.mWakeupAlarms.put(tag, c);
10579 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010580 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010581 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010582 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010584 for (int is = 0; is < NS; is++) {
10585 String servName = in.readString();
10586 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10587 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010588 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 }
10591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 }
10593 }
10594
10595 /**
10596 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10597 * disk. This format does not allow a lossless round-trip.
10598 *
10599 * @param out the Parcel to be written to.
10600 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010601 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010602 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010603
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010604 // Pull the clock time. This may update the time and make a new history entry
10605 // if we had originally pulled a time before the RTC was set.
10606 long startClockTime = getStartClockTime();
10607
Joe Onoratoabded112016-02-08 16:49:39 -080010608 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10609 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610
10611 out.writeInt(VERSION);
10612
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010613 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010616 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010617 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010618 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010619 out.writeString(mStartPlatformVersion);
10620 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010621 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
10622 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010623 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010624 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010625 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010626 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010627 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080010628 out.writeInt(getLowDischargeAmountSinceCharge());
10629 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010630 out.writeInt(getDischargeAmountScreenOnSinceCharge());
10631 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010632 mDischargeStepTracker.writeToParcel(out);
10633 mChargeStepTracker.writeToParcel(out);
10634 mDailyDischargeStepTracker.writeToParcel(out);
10635 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070010636 mDischargeCounter.writeSummaryFromParcelLocked(out);
10637 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010638 if (mDailyPackageChanges != null) {
10639 final int NPKG = mDailyPackageChanges.size();
10640 out.writeInt(NPKG);
10641 for (int i=0; i<NPKG; i++) {
10642 PackageChange pc = mDailyPackageChanges.get(i);
10643 out.writeString(pc.mPackageName);
10644 out.writeInt(pc.mUpdate ? 1 : 0);
10645 out.writeInt(pc.mVersionCode);
10646 }
10647 } else {
10648 out.writeInt(0);
10649 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010650 out.writeLong(mDailyStartTime);
10651 out.writeLong(mNextMinDailyDeadline);
10652 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010653
10654 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010655 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010656 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010657 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010658 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010659 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010660 out.writeLong(mLongestLightIdleTime);
10661 out.writeLong(mLongestFullIdleTime);
10662 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10663 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10664 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010665 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010666 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080010667 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010668 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010669 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010670 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010671 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010672 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010673 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010674 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010675 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10676 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010677 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010678 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10679 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010680 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010681 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
10682 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010683 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10684 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010685 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010686 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010687 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010688 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10689 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10690 }
10691 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10692 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10693 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010694 mWifiActivity.writeSummaryToParcel(out);
10695 mBluetoothActivity.writeSummaryToParcel(out);
10696 mModemActivity.writeSummaryToParcel(out);
10697 out.writeInt(mHasWifiReporting ? 1 : 0);
10698 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10699 out.writeInt(mHasModemReporting ? 1 : 0);
10700
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010701 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010702 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010703 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010704 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705
Evan Millarc64edde2009-04-18 12:26:32 -070010706 out.writeInt(mKernelWakelockStats.size());
10707 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10708 Timer kwlt = ent.getValue();
10709 if (kwlt != null) {
10710 out.writeInt(1);
10711 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010712 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10713 } else {
10714 out.writeInt(0);
10715 }
10716 }
10717
10718 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010719 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10720 SamplingTimer timer = ent.getValue();
10721 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010722 out.writeInt(1);
10723 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010724 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070010725 } else {
10726 out.writeInt(0);
10727 }
10728 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 final int NU = mUidStats.size();
10731 out.writeInt(NU);
10732 for (int iu = 0; iu < NU; iu++) {
10733 out.writeInt(mUidStats.keyAt(iu));
10734 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010735
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010736 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010737 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010738 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010739 } else {
10740 out.writeInt(0);
10741 }
10742 if (u.mFullWifiLockTimer != null) {
10743 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010744 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010745 } else {
10746 out.writeInt(0);
10747 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010748 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010749 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010750 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010751 } else {
10752 out.writeInt(0);
10753 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010754 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10755 if (u.mWifiBatchedScanTimer[i] != null) {
10756 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010757 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070010758 } else {
10759 out.writeInt(0);
10760 }
10761 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010762 if (u.mWifiMulticastTimer != null) {
10763 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010764 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010765 } else {
10766 out.writeInt(0);
10767 }
10768 if (u.mAudioTurnedOnTimer != null) {
10769 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010770 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010771 } else {
10772 out.writeInt(0);
10773 }
10774 if (u.mVideoTurnedOnTimer != null) {
10775 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010776 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010777 } else {
10778 out.writeInt(0);
10779 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010780 if (u.mFlashlightTurnedOnTimer != null) {
10781 out.writeInt(1);
10782 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10783 } else {
10784 out.writeInt(0);
10785 }
10786 if (u.mCameraTurnedOnTimer != null) {
10787 out.writeInt(1);
10788 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10789 } else {
10790 out.writeInt(0);
10791 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010792 if (u.mForegroundActivityTimer != null) {
10793 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010794 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010795 } else {
10796 out.writeInt(0);
10797 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010798 if (u.mBluetoothScanTimer != null) {
10799 out.writeInt(1);
10800 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10801 } else {
10802 out.writeInt(0);
10803 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070010804 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10805 if (u.mProcessStateTimer[i] != null) {
10806 out.writeInt(1);
10807 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10808 } else {
10809 out.writeInt(0);
10810 }
10811 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010812 if (u.mVibratorOnTimer != null) {
10813 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010814 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010815 } else {
10816 out.writeInt(0);
10817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818
Dianne Hackborn617f8772009-03-31 15:04:46 -070010819 if (u.mUserActivityCounters == null) {
10820 out.writeInt(0);
10821 } else {
10822 out.writeInt(1);
10823 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10824 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
10825 }
10826 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010827
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010828 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010829 out.writeInt(0);
10830 } else {
10831 out.writeInt(1);
10832 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010833 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10834 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010835 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010836 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
10837 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010838 }
10839
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010840 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
10841 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010842 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010843
Adam Lesinski6832f392015-09-05 18:05:40 -070010844 if (u.mCpuClusterSpeed != null) {
10845 out.writeInt(1);
10846 out.writeInt(u.mCpuClusterSpeed.length);
10847 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
10848 if (cpuSpeeds != null) {
10849 out.writeInt(1);
10850 out.writeInt(cpuSpeeds.length);
10851 for (LongSamplingCounter c : cpuSpeeds) {
10852 if (c != null) {
10853 out.writeInt(1);
10854 c.writeSummaryFromParcelLocked(out);
10855 } else {
10856 out.writeInt(0);
10857 }
10858 }
10859 } else {
10860 out.writeInt(0);
10861 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010862 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010863 } else {
10864 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010865 }
10866
Adam Lesinski5f056f62016-07-14 16:56:08 -070010867 if (u.mMobileRadioApWakeupCount != null) {
10868 out.writeInt(1);
10869 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
10870 } else {
10871 out.writeInt(0);
10872 }
10873
10874 if (u.mWifiRadioApWakeupCount != null) {
10875 out.writeInt(1);
10876 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
10877 } else {
10878 out.writeInt(0);
10879 }
10880
Dianne Hackbornd953c532014-08-16 18:17:38 -070010881 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
10882 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010884 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010885 out.writeString(wakeStats.keyAt(iw));
10886 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010887 if (wl.mTimerFull != null) {
10888 out.writeInt(1);
10889 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10890 } else {
10891 out.writeInt(0);
10892 }
10893 if (wl.mTimerPartial != null) {
10894 out.writeInt(1);
10895 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10896 } else {
10897 out.writeInt(0);
10898 }
10899 if (wl.mTimerWindow != null) {
10900 out.writeInt(1);
10901 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10902 } else {
10903 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010904 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010905 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070010906 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010907 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070010908 } else {
10909 out.writeInt(0);
10910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010911 }
10912
Dianne Hackbornd953c532014-08-16 18:17:38 -070010913 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
10914 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010915 out.writeInt(NS);
10916 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010917 out.writeString(syncStats.keyAt(is));
10918 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010919 }
10920
Dianne Hackbornd953c532014-08-16 18:17:38 -070010921 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
10922 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010923 out.writeInt(NJ);
10924 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010925 out.writeString(jobStats.keyAt(ij));
10926 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010927 }
10928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010929 int NSE = u.mSensorStats.size();
10930 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010931 for (int ise=0; ise<NSE; ise++) {
10932 out.writeInt(u.mSensorStats.keyAt(ise));
10933 Uid.Sensor se = u.mSensorStats.valueAt(ise);
10934 if (se.mTimer != null) {
10935 out.writeInt(1);
10936 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10937 } else {
10938 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010939 }
10940 }
10941
10942 int NP = u.mProcessStats.size();
10943 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010944 for (int ip=0; ip<NP; ip++) {
10945 out.writeString(u.mProcessStats.keyAt(ip));
10946 Uid.Proc ps = u.mProcessStats.valueAt(ip);
10947 out.writeLong(ps.mUserTime);
10948 out.writeLong(ps.mSystemTime);
10949 out.writeLong(ps.mForegroundTime);
10950 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010951 out.writeInt(ps.mNumCrashes);
10952 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010953 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010954 }
10955
10956 NP = u.mPackageStats.size();
10957 out.writeInt(NP);
10958 if (NP > 0) {
10959 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
10960 : u.mPackageStats.entrySet()) {
10961 out.writeString(ent.getKey());
10962 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010963 final int NWA = ps.mWakeupAlarms.size();
10964 out.writeInt(NWA);
10965 for (int iwa=0; iwa<NWA; iwa++) {
10966 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
10967 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
10968 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010969 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010970 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010971 for (int is=0; is<NS; is++) {
10972 out.writeString(ps.mServiceStats.keyAt(is));
10973 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
10974 long time = ss.getStartTimeToNowLocked(
10975 mOnBatteryTimeBase.getUptime(NOW_SYS));
10976 out.writeLong(time);
10977 out.writeInt(ss.mStarts);
10978 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010979 }
10980 }
10981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 }
10983 }
10984
10985 public void readFromParcel(Parcel in) {
10986 readFromParcelLocked(in);
10987 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 void readFromParcelLocked(Parcel in) {
10990 int magic = in.readInt();
10991 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010992 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010993 }
10994
Dianne Hackbornae384452011-06-28 12:33:48 -070010995 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010997 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010998 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010999 mStartPlatformVersion = in.readString();
11000 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 mUptime = in.readLong();
11002 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 mRealtime = in.readLong();
11004 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011006 mEstimatedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011008 mOnBatteryTimeBase.readFromParcel(in);
11009 mOnBatteryScreenOffTimeBase.readFromParcel(in);
11010
Jeff Browne95c3cd2014-05-02 16:59:26 -070011011 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080011012 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011013 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011014 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
11015 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011016 }
Dianne Hackborn29325132014-05-21 15:01:03 -070011017 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011018 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011019 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011020 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
11021 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011022 mLongestLightIdleTime = in.readLong();
11023 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080011024 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
11025 mOnBatteryTimeBase, in);
11026 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
11027 mOnBatteryTimeBase, in);
11028 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
11029 mOnBatteryTimeBase, in);
11030 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
11031 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011032 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011033 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011034 null, mOnBatteryTimeBase, in);
11035 }
Joe Onoratoabded112016-02-08 16:49:39 -080011036 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
11037 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011038 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011039 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011040 null, mOnBatteryTimeBase, in);
11041 }
11042 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11043 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11044 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11045 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011046 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080011047 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
11048 mOnBatteryTimeBase, in);
11049 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
11050 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011051 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011052 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
11053 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011054 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011055 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011056 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011057 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011058 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
11059 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011060 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011061 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011062 null, mOnBatteryTimeBase, in);
11063 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011064 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011065 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011066 null, mOnBatteryTimeBase, in);
11067 }
11068 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011069 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011070 null, mOnBatteryTimeBase, in);
11071 }
Adam Lesinski33dac552015-03-09 15:24:48 -070011072
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011073 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11074 NUM_WIFI_TX_LEVELS, in);
11075 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11076 NUM_BT_TX_LEVELS, in);
11077 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11078 ModemActivityInfo.TX_POWER_LEVELS, in);
11079 mHasWifiReporting = in.readInt() != 0;
11080 mHasBluetoothReporting = in.readInt() != 0;
11081 mHasModemReporting = in.readInt() != 0;
11082
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011083 mNumConnectivityChange = in.readInt();
11084 mLoadedNumConnectivityChange = in.readInt();
11085 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011086 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011087 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011088 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011089 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011090 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011091 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011092 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011093 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011094 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011095 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011096 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011097 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011098 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011099 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011100 mLowDischargeAmountSinceCharge = in.readInt();
11101 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011102 mDischargeAmountScreenOn = in.readInt();
11103 mDischargeAmountScreenOnSinceCharge = in.readInt();
11104 mDischargeAmountScreenOff = in.readInt();
11105 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011106 mDischargeStepTracker.readFromParcel(in);
11107 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011108 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
11109 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110 mLastWriteTime = in.readLong();
11111
Evan Millarc64edde2009-04-18 12:26:32 -070011112 mKernelWakelockStats.clear();
11113 int NKW = in.readInt();
11114 for (int ikw = 0; ikw < NKW; ikw++) {
11115 if (in.readInt() != 0) {
11116 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011117 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070011118 mKernelWakelockStats.put(wakelockName, kwlt);
11119 }
11120 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011121
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011122 mWakeupReasonStats.clear();
11123 int NWR = in.readInt();
11124 for (int iwr = 0; iwr < NWR; iwr++) {
11125 if (in.readInt() != 0) {
11126 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011127 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011128 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011129 }
11130 }
11131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 mPartialTimers.clear();
11133 mFullTimers.clear();
11134 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011135 mWifiRunningTimers.clear();
11136 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070011137 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070011138 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011139 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011140 mAudioTurnedOnTimers.clear();
11141 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011142 mFlashlightTurnedOnTimers.clear();
11143 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144
11145 int numUids = in.readInt();
11146 mUidStats.clear();
11147 for (int i = 0; i < numUids; i++) {
11148 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011149 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011150 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 mUidStats.append(uid, u);
11152 }
11153 }
11154
11155 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011156 writeToParcelLocked(out, true, flags);
11157 }
11158
11159 public void writeToParcelWithoutUids(Parcel out, int flags) {
11160 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011162
11163 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011164 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011165 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011166 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011167
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011168 // Pull the clock time. This may update the time and make a new history entry
11169 // if we had originally pulled a time before the RTC was set.
11170 long startClockTime = getStartClockTime();
11171
Joe Onoratoabded112016-02-08 16:49:39 -080011172 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11173 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011174 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11175 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011177 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011178
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011179 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011182 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011183 out.writeString(mStartPlatformVersion);
11184 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011185 out.writeLong(mUptime);
11186 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 out.writeLong(mRealtime);
11188 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011189 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011190 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011191 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11192 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11193
11194 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11195 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11196 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11197 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011198 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011199 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011200 out.writeLong(mLongestLightIdleTime);
11201 out.writeLong(mLongestFullIdleTime);
11202 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11203 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11204 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011205 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011206 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11207 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11208 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11209 }
11210 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11211 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11212 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11213 }
11214 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11215 mNetworkByteActivityCounters[i].writeToParcel(out);
11216 mNetworkPacketActivityCounters[i].writeToParcel(out);
11217 }
11218 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11219 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011220 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011221 mMobileRadioActiveUnknownTime.writeToParcel(out);
11222 mMobileRadioActiveUnknownCount.writeToParcel(out);
11223 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11224 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11225 for (int i=0; i<NUM_WIFI_STATES; i++) {
11226 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11227 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011228 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11229 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11230 }
11231 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11232 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11233 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011234 mWifiActivity.writeToParcel(out, 0);
11235 mBluetoothActivity.writeToParcel(out, 0);
11236 mModemActivity.writeToParcel(out, 0);
11237 out.writeInt(mHasWifiReporting ? 1 : 0);
11238 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11239 out.writeInt(mHasModemReporting ? 1 : 0);
11240
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011241 out.writeInt(mNumConnectivityChange);
11242 out.writeInt(mLoadedNumConnectivityChange);
11243 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011244 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011245 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011246 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011247 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011248 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011249 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011250 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011251 out.writeInt(mLowDischargeAmountSinceCharge);
11252 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011253 out.writeInt(mDischargeAmountScreenOn);
11254 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11255 out.writeInt(mDischargeAmountScreenOff);
11256 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011257 mDischargeStepTracker.writeToParcel(out);
11258 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011259 mDischargeCounter.writeToParcel(out);
11260 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011261 out.writeLong(mLastWriteTime);
11262
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011263 if (inclUids) {
11264 out.writeInt(mKernelWakelockStats.size());
11265 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11266 SamplingTimer kwlt = ent.getValue();
11267 if (kwlt != null) {
11268 out.writeInt(1);
11269 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011270 kwlt.writeToParcel(out, uSecRealtime);
11271 } else {
11272 out.writeInt(0);
11273 }
11274 }
11275 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011276 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11277 SamplingTimer timer = ent.getValue();
11278 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011279 out.writeInt(1);
11280 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011281 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011282 } else {
11283 out.writeInt(0);
11284 }
Evan Millarc64edde2009-04-18 12:26:32 -070011285 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011286 } else {
11287 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011288 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011289
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011290 if (inclUids) {
11291 int size = mUidStats.size();
11292 out.writeInt(size);
11293 for (int i = 0; i < size; i++) {
11294 out.writeInt(mUidStats.keyAt(i));
11295 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011297 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011298 }
11299 } else {
11300 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011301 }
11302 }
11303
11304 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11305 new Parcelable.Creator<BatteryStatsImpl>() {
11306 public BatteryStatsImpl createFromParcel(Parcel in) {
11307 return new BatteryStatsImpl(in);
11308 }
11309
11310 public BatteryStatsImpl[] newArray(int size) {
11311 return new BatteryStatsImpl[size];
11312 }
11313 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011314
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011315 public void prepareForDumpLocked() {
11316 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011317 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011318
11319 // Pull the clock time. This may update the time and make a new history entry
11320 // if we had originally pulled a time before the RTC was set.
11321 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011322 }
11323
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011324 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011325 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011326 pw.println("mOnBatteryTimeBase:");
11327 mOnBatteryTimeBase.dump(pw, " ");
11328 pw.println("mOnBatteryScreenOffTimeBase:");
11329 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011330 Printer pr = new PrintWriterPrinter(pw);
11331 pr.println("*** Screen timer:");
11332 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011333 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011334 pr.println("*** Screen brightness #" + i + ":");
11335 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011336 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011337 pr.println("*** Interactive timer:");
11338 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011339 pr.println("*** Power save mode timer:");
11340 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011341 pr.println("*** Device idle mode light timer:");
11342 mDeviceIdleModeLightTimer.logState(pr, " ");
11343 pr.println("*** Device idle mode full timer:");
11344 mDeviceIdleModeFullTimer.logState(pr, " ");
11345 pr.println("*** Device light idling timer:");
11346 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011347 pr.println("*** Device idling timer:");
11348 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011349 pr.println("*** Phone timer:");
11350 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011351 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011352 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011353 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011354 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011355 pr.println("*** Signal scanning :");
11356 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011357 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011358 pr.println("*** Data connection type #" + i + ":");
11359 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011360 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011361 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011362 pr.println("*** Mobile network active timer:");
11363 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011364 pr.println("*** Mobile network active adjusted timer:");
11365 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011366 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011367 pr.println("*** Wifi timer:");
11368 mWifiOnTimer.logState(pr, " ");
11369 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011370 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011371 for (int i=0; i<NUM_WIFI_STATES; i++) {
11372 pr.println("*** Wifi state #" + i + ":");
11373 mWifiStateTimer[i].logState(pr, " ");
11374 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011375 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11376 pr.println("*** Wifi suppl state #" + i + ":");
11377 mWifiSupplStateTimer[i].logState(pr, " ");
11378 }
11379 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11380 pr.println("*** Wifi signal strength #" + i + ":");
11381 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11382 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011383 pr.println("*** Flashlight timer:");
11384 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011385 pr.println("*** Camera timer:");
11386 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011388 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011390}