blob: 1f63f1e3a29935dbfa553ef70ec6bad0d1dedd09 [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;
James Carr3a226052016-07-01 14:49:52 -070052import android.util.LongSparseArray;
53import android.util.LongSparseLongArray;
Dianne Hackbornd953c532014-08-16 18:17:38 -070054import android.util.MutableInt;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070055import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070057import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080059import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070060import android.util.SparseLongArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070061import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080062import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070063import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Adam Lesinski98f0d462016-04-19 16:46:20 -070065import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070066import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080067import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070068import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080069import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070070import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080071import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070072import com.android.server.NetworkManagementSocketTagger;
73import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080074import org.xmlpull.v1.XmlPullParser;
75import org.xmlpull.v1.XmlPullParserException;
76import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070077
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080078import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.io.File;
80import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080081import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.io.FileOutputStream;
83import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070084import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010085import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.ArrayList;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080087import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070089import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070091import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070092import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * All information we are collecting about things that can happen that impact
96 * battery life. All times are represented in microseconds except where indicated
97 * otherwise.
98 */
Joe Onoratoabded112016-02-08 16:49:39 -080099public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800101 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700102 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800103 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
James Carr3a226052016-07-01 14:49:52 -0700104 private static final boolean DEBUG_MEMORY = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700105 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700106 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700107
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700108 // TODO: remove "tcp" from network methods, since we measure total stats.
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700111 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 // Current on-disk Parcel version
Kweku Adams47db5a82016-12-09 19:04:50 -0800114 private static final int VERSION = 151 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700115
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700116 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700117 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700118
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700119 // No, really, THIS is the maximum number of items we will record in the history.
120 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
121
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800122 // The maximum number of names wakelocks we will keep track of
123 // per uid; once the limit is reached, we batch the remaining wakelocks
124 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700125 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700126
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800127 // Number of transmit power states the Wifi controller can be in.
128 private static final int NUM_WIFI_TX_LEVELS = 1;
129
130 // Number of transmit power states the Bluetooth controller can be in.
131 private static final int NUM_BT_TX_LEVELS = 1;
132
Joe Onoratoabded112016-02-08 16:49:39 -0800133 protected Clocks mClocks;
134
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700135 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700136 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800137 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700139 static final int MSG_UPDATE_WAKELOCKS = 1;
140 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700141 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700142 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700143
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700144 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
145 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
146
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700147 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700148 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700149
James Carr3a226052016-07-01 14:49:52 -0700150 private final KernelMemoryBandwidthStats mKernelMemoryBandwidthStats
151 = new KernelMemoryBandwidthStats();
152 private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
153 public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
154 return mKernelMemoryStats;
155 }
156
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700157 public interface BatteryCallback {
158 public void batteryNeedsCpuUpdate();
159 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700160 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700161 }
162
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700163 public interface PlatformIdleStateCallback {
164 public String getPlatformLowPowerStats();
165 }
166
167 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
168
169
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700170 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800171 public MyHandler(Looper looper) {
172 super(looper, null, true);
173 }
174
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700175 @Override
176 public void handleMessage(Message msg) {
177 BatteryCallback cb = mCallback;
178 switch (msg.what) {
179 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700180 synchronized (BatteryStatsImpl.this) {
181 updateCpuTimeLocked();
182 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700183 if (cb != null) {
184 cb.batteryNeedsCpuUpdate();
185 }
186 break;
187 case MSG_REPORT_POWER_CHANGE:
188 if (cb != null) {
189 cb.batteryPowerChanged(msg.arg1 != 0);
190 }
191 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700192 case MSG_REPORT_CHARGING:
193 if (cb != null) {
194 final String action;
195 synchronized (BatteryStatsImpl.this) {
196 action = mCharging ? BatteryManager.ACTION_CHARGING
197 : BatteryManager.ACTION_DISCHARGING;
198 }
199 Intent intent = new Intent(action);
200 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
201 cb.batterySendBroadcast(intent);
202 }
203 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700204 }
205 }
206 }
207
Joe Onoratoabded112016-02-08 16:49:39 -0800208 public interface Clocks {
209 public long elapsedRealtime();
210 public long uptimeMillis();
211 }
212
213 public static class SystemClocks implements Clocks {
214 public long elapsedRealtime() {
215 return SystemClock.elapsedRealtime();
216 }
217
218 public long uptimeMillis() {
219 return SystemClock.uptimeMillis();
220 }
221 }
222
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700223 public interface ExternalStatsSync {
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800224 public static final int UPDATE_CPU = 0x01;
225 public static final int UPDATE_WIFI = 0x02;
226 public static final int UPDATE_RADIO = 0x04;
227 public static final int UPDATE_BT = 0x08;
228 public static final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
229
230 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700231 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700232 }
233
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700234 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700235 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700236
237 private BatteryCallback mCallback;
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800240 * Mapping isolated uids to the actual owning app uid.
241 */
242 final SparseIntArray mIsolatedUids = new SparseIntArray();
243
244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 * The statistics we have collected organized by uids.
246 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800247 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249 // A set of pools of currently active timers. When a timer is queried, we will divide the
250 // elapsed time by the number of active timers to arrive at that timer's share of the time.
251 // In order to do this, we must refresh each timer whenever the number of active timers
252 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700253 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
254 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
255 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700256 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700257 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
258 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
259 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
260 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
261 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
262 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
263 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
264 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700265 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
266 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800267 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700269 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700270 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 // These are the objects that will want to do something when the device
273 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800274 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800275
276 // These are the objects that will want to do something when the device
277 // is unplugged from power *and* the screen is off.
278 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
279
280 // Set to true when we want to distribute CPU across wakelocks for the next
281 // CPU update, even if we aren't currently running wake locks.
282 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700283
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700284 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700285
Dianne Hackborn37de0982014-05-09 09:32:18 -0700286 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800287
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700288 long mHistoryBaseTime;
289 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700290 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700291 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700292
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700293 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
294 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700295 final Parcel mHistoryBuffer = Parcel.obtain();
296 final HistoryItem mHistoryLastWritten = new HistoryItem();
297 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700298 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700299 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700300 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800301 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800302 int[] mReadHistoryUids;
303 int mReadHistoryChars;
304 int mNextHistoryTagIdx = 0;
305 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700306 int mHistoryBufferLastPos = -1;
307 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700308 int mActiveHistoryStates = 0xffffffff;
309 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700310 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700311 long mTrackRunningHistoryElapsedRealtime = 0;
312 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700313
314 final HistoryItem mHistoryCur = new HistoryItem();
315
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700316 HistoryItem mHistory;
317 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700318 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700319 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700320
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800321 // Used by computeHistoryStepDetails
322 HistoryStepDetails mLastHistoryStepDetails = null;
323 byte mLastHistoryStepLevel = 0;
324 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
325 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
326 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700327
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800328 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700329 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800330 */
331 long mLastStepCpuUserTime;
332 long mCurStepCpuUserTime;
333 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700334 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800335 */
336 long mLastStepCpuSystemTime;
337 long mCurStepCpuSystemTime;
338 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700339 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800340 */
341 long mLastStepStatUserTime;
342 long mLastStepStatSystemTime;
343 long mLastStepStatIOWaitTime;
344 long mLastStepStatIrqTime;
345 long mLastStepStatSoftIrqTime;
346 long mLastStepStatIdleTime;
347 long mCurStepStatUserTime;
348 long mCurStepStatSystemTime;
349 long mCurStepStatIOWaitTime;
350 long mCurStepStatIrqTime;
351 long mCurStepStatSoftIrqTime;
352 long mCurStepStatIdleTime;
353
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700354 private HistoryItem mHistoryIterator;
355 private boolean mReadOverflow;
356 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 int mStartCount;
359
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800360 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700361 String mStartPlatformVersion;
362 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 long mUptime;
365 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 long mRealtime;
367 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700368
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800369 int mWakeLockNesting;
370 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700371 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700372 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800373
Jeff Browne95c3cd2014-05-02 16:59:26 -0700374 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700375 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700376
Dianne Hackborn617f8772009-03-31 15:04:46 -0700377 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700378 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700379
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800380 boolean mPretendScreenOff;
381
Jeff Browne95c3cd2014-05-02 16:59:26 -0700382 boolean mInteractive;
383 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700384
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700385 boolean mPowerSaveModeEnabled;
386 StopwatchTimer mPowerSaveModeEnabledTimer;
387
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700388 boolean mDeviceIdling;
389 StopwatchTimer mDeviceIdlingTimer;
390
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700391 boolean mDeviceLightIdling;
392 StopwatchTimer mDeviceLightIdlingTimer;
393
394 int mDeviceIdleMode;
395 long mLastIdleTimeStart;
396 long mLongestLightIdleTime;
397 long mLongestFullIdleTime;
398 StopwatchTimer mDeviceIdleModeLightTimer;
399 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700402 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700403
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700404 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700405 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700406
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700407 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700408 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700409
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700410 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700411 StopwatchTimer mFlashlightOnTimer;
412
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700413 int mCameraOnNesting;
414 StopwatchTimer mCameraOnTimer;
415
Dianne Hackborn627bba72009-03-24 22:32:56 -0700416 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800417 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700418 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800419 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700420
421 StopwatchTimer mPhoneSignalScanningTimer;
422
Dianne Hackborn627bba72009-03-24 22:32:56 -0700423 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700424 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700425 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700426
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800427 final LongSamplingCounter[] mNetworkByteActivityCounters =
428 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
429 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700430 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
431
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800432 /**
433 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
434 */
435 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700436
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800437 /**
438 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
439 */
440 ControllerActivityCounterImpl mBluetoothActivity;
441
442 /**
443 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
444 */
445 ControllerActivityCounterImpl mModemActivity;
446
447 /**
448 * Whether the device supports WiFi controller energy reporting. This is set to true on
449 * the first WiFi energy report. See {@link #mWifiActivity}.
450 */
451 boolean mHasWifiReporting = false;
452
453 /**
454 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
455 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
456 */
457 boolean mHasBluetoothReporting = false;
458
459 /**
460 * Whether the device supports Modem controller energy reporting. This is set to true on
461 * the first Modem energy report. See {@link #mModemActivity}.
462 */
463 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700464
The Android Open Source Project10592532009-03-18 17:39:46 -0700465 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700466 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700467
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700468 boolean mGlobalWifiRunning;
469 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700470
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800471 int mWifiState = -1;
472 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
473
Dianne Hackborn3251b902014-06-20 14:40:53 -0700474 int mWifiSupplState = -1;
475 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
476
477 int mWifiSignalStrengthBin = -1;
478 final StopwatchTimer[] mWifiSignalStrengthsTimer =
479 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
480
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800481 int mBluetoothScanNesting;
482 StopwatchTimer mBluetoothScanTimer;
483
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700484 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700485 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800486 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800487 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700488 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800489 LongSamplingCounter mMobileRadioActiveUnknownTime;
490 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800491
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700492 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 /**
495 * These provide time bases that discount the time the device is plugged
496 * in to power.
497 */
498 boolean mOnBattery;
499 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700500
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700501 /**
502 * External reporting of whether the device is actually charging.
503 */
504 boolean mCharging = true;
505 int mLastChargingStateLevel;
506
The Android Open Source Project10592532009-03-18 17:39:46 -0700507 /*
508 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
509 */
Evan Millar633a1742009-04-02 16:36:33 -0700510 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700511 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700512 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700513 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700514 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700515 int mLowDischargeAmountSinceCharge;
516 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800517 int mDischargeScreenOnUnplugLevel;
518 int mDischargeScreenOffUnplugLevel;
519 int mDischargeAmountScreenOn;
520 int mDischargeAmountScreenOnSinceCharge;
521 int mDischargeAmountScreenOff;
522 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700523
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700524 private LongSamplingCounter mDischargeScreenOffCounter;
525 private LongSamplingCounter mDischargeCounter;
526
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700527 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700528
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700529 int mInitStepMode = 0;
530 int mCurStepMode = 0;
531 int mModStepMode = 0;
532
Dianne Hackborn260c5022014-04-29 11:23:16 -0700533 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700534 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800535 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
536 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700537 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700538
539 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700540 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800541 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
542 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
543
544 static final int MAX_DAILY_ITEMS = 10;
545
546 long mDailyStartTime = 0;
547 long mNextMinDailyDeadline = 0;
548 long mNextMaxDailyDeadline = 0;
549
550 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700553
Amith Yamasanif37447b2009-10-08 18:28:01 -0700554 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800555 private int mPhoneServiceStateRaw = -1;
556 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700557
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800558 private int mNumConnectivityChange;
559 private int mLoadedNumConnectivityChange;
560 private int mUnpluggedNumConnectivityChange;
561
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700562 private int mEstimatedBatteryCapacity = -1;
563
Adam Lesinski041d9172016-12-12 12:03:56 -0800564 // Last learned capacity reported by BatteryService in
565 // setBatteryState().
566 private int mLastChargeFullUAh = 0;
567
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700568 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
569
Adam Lesinskie08af192015-03-25 16:42:59 -0700570 private PowerProfile mPowerProfile;
571
Evan Millarc64edde2009-04-18 12:26:32 -0700572 /*
573 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
574 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700575 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700576
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700577 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700578 return mKernelWakelockStats;
579 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700580
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700581 String mLastWakeupReason = null;
582 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700583 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700584
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700585 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700586 return mWakeupReasonStats;
587 }
588
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700589 @Override
590 public LongCounter getDischargeScreenOffCoulombCounter() {
591 return mDischargeScreenOffCounter;
592 }
593
594 @Override
595 public LongCounter getDischargeCoulombCounter() {
596 return mDischargeCounter;
597 }
598
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700599 @Override
600 public int getEstimatedBatteryCapacity() {
601 return mEstimatedBatteryCapacity;
602 }
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800605 this(new SystemClocks());
606 }
607
608 public BatteryStatsImpl(Clocks clocks) {
609 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700610 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700611 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800612 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700613 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700614 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700615 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700616 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618
Joe Onoratoabded112016-02-08 16:49:39 -0800619 private void init(Clocks clocks) {
620 mClocks = clocks;
621 mMobileNetworkStats = new NetworkStats[] {
622 new NetworkStats(mClocks.elapsedRealtime(), 50),
623 new NetworkStats(mClocks.elapsedRealtime(), 50),
624 new NetworkStats(mClocks.elapsedRealtime(), 50)
625 };
626 mWifiNetworkStats = new NetworkStats[] {
627 new NetworkStats(mClocks.elapsedRealtime(), 50),
628 new NetworkStats(mClocks.elapsedRealtime(), 50),
629 new NetworkStats(mClocks.elapsedRealtime(), 50)
630 };
631 }
632
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800633 public static interface TimeBaseObs {
634 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
635 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
636 }
637
Joe Onoratoabded112016-02-08 16:49:39 -0800638 // methods are protected not private to be VisibleForTesting
639 public static class TimeBase {
640 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800641
Joe Onoratoabded112016-02-08 16:49:39 -0800642 protected long mUptime;
643 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800644
Joe Onoratoabded112016-02-08 16:49:39 -0800645 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800646
Joe Onoratoabded112016-02-08 16:49:39 -0800647 protected long mPastUptime;
648 protected long mUptimeStart;
649 protected long mPastRealtime;
650 protected long mRealtimeStart;
651 protected long mUnpluggedUptime;
652 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800653
654 public void dump(PrintWriter pw, String prefix) {
655 StringBuilder sb = new StringBuilder(128);
656 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
657 sb.setLength(0);
658 sb.append(prefix);
659 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700660 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800661 pw.println(sb.toString());
662 sb.setLength(0);
663 sb.append(prefix);
664 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700665 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800666 pw.println(sb.toString());
667 sb.setLength(0);
668 sb.append(prefix);
669 sb.append("mPastUptime=");
670 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
671 formatTimeMs(sb, mUptimeStart / 1000);
672 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
673 pw.println(sb.toString());
674 sb.setLength(0);
675 sb.append(prefix);
676 sb.append("mPastRealtime=");
677 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
678 formatTimeMs(sb, mRealtimeStart / 1000);
679 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
680 pw.println(sb.toString());
681 }
682
683 public void add(TimeBaseObs observer) {
684 mObservers.add(observer);
685 }
686
687 public void remove(TimeBaseObs observer) {
688 if (!mObservers.remove(observer)) {
689 Slog.wtf(TAG, "Removed unknown observer: " + observer);
690 }
691 }
692
Joe Onoratoabded112016-02-08 16:49:39 -0800693 public boolean hasObserver(TimeBaseObs observer) {
694 return mObservers.contains(observer);
695 }
696
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800697 public void init(long uptime, long realtime) {
698 mRealtime = 0;
699 mUptime = 0;
700 mPastUptime = 0;
701 mPastRealtime = 0;
702 mUptimeStart = uptime;
703 mRealtimeStart = realtime;
704 mUnpluggedUptime = getUptime(mUptimeStart);
705 mUnpluggedRealtime = getRealtime(mRealtimeStart);
706 }
707
708 public void reset(long uptime, long realtime) {
709 if (!mRunning) {
710 mPastUptime = 0;
711 mPastRealtime = 0;
712 } else {
713 mUptimeStart = uptime;
714 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800715 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
716 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800717 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800718 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800719 mUnpluggedRealtime = getRealtime(realtime);
720 }
721 }
722
723 public long computeUptime(long curTime, int which) {
724 switch (which) {
725 case STATS_SINCE_CHARGED:
726 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800727 case STATS_CURRENT:
728 return getUptime(curTime);
729 case STATS_SINCE_UNPLUGGED:
730 return getUptime(curTime) - mUnpluggedUptime;
731 }
732 return 0;
733 }
734
735 public long computeRealtime(long curTime, int which) {
736 switch (which) {
737 case STATS_SINCE_CHARGED:
738 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800739 case STATS_CURRENT:
740 return getRealtime(curTime);
741 case STATS_SINCE_UNPLUGGED:
742 return getRealtime(curTime) - mUnpluggedRealtime;
743 }
744 return 0;
745 }
746
747 public long getUptime(long curTime) {
748 long time = mPastUptime;
749 if (mRunning) {
750 time += curTime - mUptimeStart;
751 }
752 return time;
753 }
754
755 public long getRealtime(long curTime) {
756 long time = mPastRealtime;
757 if (mRunning) {
758 time += curTime - mRealtimeStart;
759 }
760 return time;
761 }
762
763 public long getUptimeStart() {
764 return mUptimeStart;
765 }
766
767 public long getRealtimeStart() {
768 return mRealtimeStart;
769 }
770
771 public boolean isRunning() {
772 return mRunning;
773 }
774
775 public boolean setRunning(boolean running, long uptime, long realtime) {
776 if (mRunning != running) {
777 mRunning = running;
778 if (running) {
779 mUptimeStart = uptime;
780 mRealtimeStart = realtime;
781 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
782 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
783
784 for (int i = mObservers.size() - 1; i >= 0; i--) {
785 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
786 }
787 } else {
788 mPastUptime += uptime - mUptimeStart;
789 mPastRealtime += realtime - mRealtimeStart;
790
791 long batteryUptime = getUptime(uptime);
792 long batteryRealtime = getRealtime(realtime);
793
794 for (int i = mObservers.size() - 1; i >= 0; i--) {
795 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
796 }
797 }
798 return true;
799 }
800 return false;
801 }
802
803 public void readSummaryFromParcel(Parcel in) {
804 mUptime = in.readLong();
805 mRealtime = in.readLong();
806 }
807
808 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
809 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
810 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
811 }
812
813 public void readFromParcel(Parcel in) {
814 mRunning = false;
815 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800816 mPastUptime = in.readLong();
817 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700818 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800819 mPastRealtime = in.readLong();
820 mRealtimeStart = in.readLong();
821 mUnpluggedUptime = in.readLong();
822 mUnpluggedRealtime = in.readLong();
823 }
824
825 public void writeToParcel(Parcel out, long uptime, long realtime) {
826 final long runningUptime = getUptime(uptime);
827 final long runningRealtime = getRealtime(realtime);
828 out.writeLong(mUptime);
829 out.writeLong(runningUptime);
830 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700831 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800832 out.writeLong(runningRealtime);
833 out.writeLong(mRealtimeStart);
834 out.writeLong(mUnpluggedUptime);
835 out.writeLong(mUnpluggedRealtime);
836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700840 * State for keeping track of counting information.
841 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800842 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700843 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800844 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700845 int mLoadedCount;
846 int mLastCount;
847 int mUnpluggedCount;
848 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700849
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800850 Counter(TimeBase timeBase, Parcel in) {
851 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700852 mPluggedCount = in.readInt();
853 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700854 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700855 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700856 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800857 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700858 }
859
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800860 Counter(TimeBase timeBase) {
861 mTimeBase = timeBase;
862 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700863 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700864
Dianne Hackborn617f8772009-03-31 15:04:46 -0700865 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700866 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700867 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700868 out.writeInt(mUnpluggedCount);
869 }
870
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800871 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700872 mUnpluggedCount = mPluggedCount;
873 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700874 }
875
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800876 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700877 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700878 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700879
Dianne Hackborn617f8772009-03-31 15:04:46 -0700880 /**
881 * Writes a possibly null Counter to a Parcel.
882 *
883 * @param out the Parcel to be written to.
884 * @param counter a Counter, or null.
885 */
886 public static void writeCounterToParcel(Parcel out, Counter counter) {
887 if (counter == null) {
888 out.writeInt(0); // indicates null
889 return;
890 }
891 out.writeInt(1); // indicates non-null
892
893 counter.writeToParcel(out);
894 }
895
896 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700897 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700898 int val = mCount.get();
899 if (which == STATS_SINCE_UNPLUGGED) {
900 val -= mUnpluggedCount;
901 } else if (which != STATS_SINCE_CHARGED) {
902 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700903 }
904
905 return val;
906 }
907
908 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700909 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700910 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
911 + " mUnpluggedCount=" + mUnpluggedCount
912 + " mPluggedCount=" + mPluggedCount);
913 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700914
Christopher Tate4cee7252010-03-19 14:50:40 -0700915 void stepAtomic() {
916 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700917 }
918
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700919 /**
920 * Clear state of this counter.
921 */
922 void reset(boolean detachIfReset) {
923 mCount.set(0);
924 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
925 if (detachIfReset) {
926 detach();
927 }
928 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700929
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700930 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800931 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700932 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700933
Dianne Hackborn617f8772009-03-31 15:04:46 -0700934 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700935 int count = mCount.get();
936 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700937 }
938
939 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700940 mLoadedCount = in.readInt();
941 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700942 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700943 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700944 }
945 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700946
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700947 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800948 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700949 long mCount;
950 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700951 long mUnpluggedCount;
952 long mPluggedCount;
953
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800954 LongSamplingCounter(TimeBase timeBase, Parcel in) {
955 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700956 mPluggedCount = in.readLong();
957 mCount = mPluggedCount;
958 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700959 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800960 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700961 }
962
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800963 LongSamplingCounter(TimeBase timeBase) {
964 mTimeBase = timeBase;
965 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700966 }
967
968 public void writeToParcel(Parcel out) {
969 out.writeLong(mCount);
970 out.writeLong(mLoadedCount);
971 out.writeLong(mUnpluggedCount);
972 }
973
974 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800975 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700976 mUnpluggedCount = mPluggedCount;
977 mCount = mPluggedCount;
978 }
979
980 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800981 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700982 mPluggedCount = mCount;
983 }
984
985 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700986 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -0700987 if (which == STATS_SINCE_UNPLUGGED) {
988 val -= mUnpluggedCount;
989 } else if (which != STATS_SINCE_CHARGED) {
990 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700991 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700992 return val;
993 }
994
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700995 @Override
996 public void logState(Printer pw, String prefix) {
997 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700998 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700999 + " mUnpluggedCount=" + mUnpluggedCount
1000 + " mPluggedCount=" + mPluggedCount);
1001 }
1002
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001003 void addCountLocked(long count) {
1004 mCount += count;
1005 }
1006
1007 /**
1008 * Clear state of this counter.
1009 */
1010 void reset(boolean detachIfReset) {
1011 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001012 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001013 if (detachIfReset) {
1014 detach();
1015 }
1016 }
1017
1018 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001019 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001020 }
1021
1022 void writeSummaryFromParcelLocked(Parcel out) {
1023 out.writeLong(mCount);
1024 }
1025
1026 void readSummaryFromParcelLocked(Parcel in) {
1027 mLoadedCount = in.readLong();
1028 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001029 mUnpluggedCount = mPluggedCount = mLoadedCount;
1030 }
1031 }
1032
Dianne Hackborn617f8772009-03-31 15:04:46 -07001033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 * State for keeping track of timing information.
1035 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001036 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001037 protected final Clocks mClocks;
1038 protected final int mType;
1039 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001040
Joe Onoratoabded112016-02-08 16:49:39 -08001041 protected int mCount;
1042 protected int mLoadedCount;
1043 protected int mLastCount;
1044 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 // Times are in microseconds for better accuracy when dividing by the
1047 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 /**
1050 * The total time we have accumulated since the start of the original
1051 * boot, to the last time something interesting happened in the
1052 * current run.
1053 */
Joe Onoratoabded112016-02-08 16:49:39 -08001054 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 /**
1057 * The total time we loaded for the previous runs. Subtract this from
1058 * mTotalTime to find the time for the current run of the system.
1059 */
Joe Onoratoabded112016-02-08 16:49:39 -08001060 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 /**
1063 * The run time of the last run of the system, as loaded from the
1064 * saved data.
1065 */
Joe Onoratoabded112016-02-08 16:49:39 -08001066 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 /**
1069 * The value of mTotalTime when unplug() was last called. Subtract
1070 * this from mTotalTime to find the time since the last unplug from
1071 * power.
1072 */
Joe Onoratoabded112016-02-08 16:49:39 -08001073 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001074
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001075 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001076 * The total time this timer has been running until the latest mark has been set.
1077 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1078 */
Joe Onoratoabded112016-02-08 16:49:39 -08001079 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001080
1081 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001082 * Constructs from a parcel.
1083 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001084 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001085 * @param in
1086 */
Joe Onoratoabded112016-02-08 16:49:39 -08001087 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1088 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001090 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 mCount = in.readInt();
1093 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001094 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 mUnpluggedCount = in.readInt();
1096 mTotalTime = in.readLong();
1097 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001098 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001100 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001101 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001102 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 }
1104
Joe Onoratoabded112016-02-08 16:49:39 -08001105 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1106 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001108 mTimeBase = timeBase;
1109 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
Evan Millarc64edde2009-04-18 12:26:32 -07001111
1112 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001113
Evan Millarc64edde2009-04-18 12:26:32 -07001114 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001115
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001116 /**
1117 * Clear state of this timer. Returns true if the timer is inactive
1118 * so can be completely dropped.
1119 */
Joe Onoratoabded112016-02-08 16:49:39 -08001120 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001121 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001122 mCount = mLoadedCount = mLastCount = 0;
1123 if (detachIfReset) {
1124 detach();
1125 }
1126 return true;
1127 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001128
Joe Onoratoabded112016-02-08 16:49:39 -08001129 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001130 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001131 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001132
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001133 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001134 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1135 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001136 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001139 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001142 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
1144
Adam Lesinskie08af192015-03-25 16:42:59 -07001145 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001148 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 + " old mUnpluggedTime=" + mUnpluggedTime
1150 + " old mUnpluggedCount=" + mUnpluggedCount);
1151 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001152 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001153 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 if (DEBUG && mType < 0) {
1155 Log.v(TAG, "unplug #" + mType
1156 + ": new mUnpluggedTime=" + mUnpluggedTime
1157 + " new mUnpluggedCount=" + mUnpluggedCount);
1158 }
1159 }
1160
Adam Lesinskie08af192015-03-25 16:42:59 -07001161 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001162 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001163 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001164 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001165 + " old mTotalTime=" + mTotalTime);
1166 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001167 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001168 mCount = computeCurrentCountLocked();
1169 if (DEBUG && mType < 0) {
1170 Log.v(TAG, "plug #" + mType
1171 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 /**
1176 * Writes a possibly null Timer to a Parcel.
1177 *
1178 * @param out the Parcel to be written to.
1179 * @param timer a Timer, or null.
1180 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001181 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 if (timer == null) {
1183 out.writeInt(0); // indicates null
1184 return;
1185 }
1186 out.writeInt(1); // indicates non-null
1187
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001188 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190
1191 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001192 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001193 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1194 if (which == STATS_SINCE_UNPLUGGED) {
1195 val -= mUnpluggedTime;
1196 } else if (which != STATS_SINCE_CHARGED) {
1197 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199
1200 return val;
1201 }
1202
1203 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001204 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001205 int val = computeCurrentCountLocked();
1206 if (which == STATS_SINCE_UNPLUGGED) {
1207 val -= mUnpluggedCount;
1208 } else if (which != STATS_SINCE_CHARGED) {
1209 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211
1212 return val;
1213 }
1214
Adam Lesinskie08af192015-03-25 16:42:59 -07001215 @Override
1216 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1217 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1218 return val - mTimeBeforeMark;
1219 }
1220
1221 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001222 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001223 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1225 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001226 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001228 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001230 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001231
1232
Joe Onoratoabded112016-02-08 16:49:39 -08001233 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001234 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1235 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001236 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001237 }
1238
Joe Onoratoabded112016-02-08 16:49:39 -08001239 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001240 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001241 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001242 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001243 mUnpluggedTime = mTotalTime;
1244 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001245 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001246 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001247
1248 // When reading the summary, we set the mark to be the latest information.
1249 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001250 }
1251 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001252
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001253 /**
1254 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1255 * method. The state of the timer according to its {@link TimeBase} will determine how much
1256 * of the value is recorded.
1257 *
1258 * If the value being recorded resets, {@link #endSample()} can be called in order to
1259 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1260 * between calls, the {@link #endSample()} is automatically called and the new value is
1261 * expected to increase monotonically from that point on.
1262 */
Joe Onoratoabded112016-02-08 16:49:39 -08001263 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001264
Evan Millarc64edde2009-04-18 12:26:32 -07001265 /**
1266 * The most recent reported count from /proc/wakelocks.
1267 */
1268 int mCurrentReportedCount;
1269
1270 /**
1271 * The reported count from /proc/wakelocks when unplug() was last
1272 * called.
1273 */
1274 int mUnpluggedReportedCount;
1275
1276 /**
1277 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001278 */
Evan Millarc64edde2009-04-18 12:26:32 -07001279 long mCurrentReportedTotalTime;
1280
1281
1282 /**
1283 * The reported total_time from /proc/wakelocks when unplug() was last
1284 * called.
1285 */
1286 long mUnpluggedReportedTotalTime;
1287
1288 /**
1289 * Whether we are currently in a discharge cycle.
1290 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001291 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001292
1293 /**
1294 * Whether we are currently recording reported values.
1295 */
1296 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001297
Evan Millarc64edde2009-04-18 12:26:32 -07001298 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001299 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001300 */
1301 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001302
Adam Lesinski98f0d462016-04-19 16:46:20 -07001303 @VisibleForTesting
1304 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001305 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001306 mCurrentReportedCount = in.readInt();
1307 mUnpluggedReportedCount = in.readInt();
1308 mCurrentReportedTotalTime = in.readLong();
1309 mUnpluggedReportedTotalTime = in.readLong();
1310 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001311 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001312 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001313
Adam Lesinski98f0d462016-04-19 16:46:20 -07001314 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001315 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001316 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001317 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001318 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001319 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001320
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001321 /**
1322 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1323 * be less than the values used for a previous invocation.
1324 */
1325 public void endSample() {
1326 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1327 mCount = computeCurrentCountLocked();
1328 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1329 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001330 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001331
Evan Millarc64edde2009-04-18 12:26:32 -07001332 public void setUpdateVersion(int version) {
1333 mUpdateVersion = version;
1334 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001335
Evan Millarc64edde2009-04-18 12:26:32 -07001336 public int getUpdateVersion() {
1337 return mUpdateVersion;
1338 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001339
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001340 /**
1341 * Updates the current recorded values. These are meant to be monotonically increasing
1342 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1343 *
1344 * If the values being recorded have been reset, the monotonically increasing requirement
1345 * will be broken. In this case, {@link #endSample()} is automatically called and
1346 * the total value of totalTime and count are recorded, starting a new monotonically
1347 * increasing sample.
1348 *
1349 * @param totalTime total time of sample in microseconds.
1350 * @param count total number of times the event being sampled occurred.
1351 */
1352 public void update(long totalTime, int count) {
1353 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001354 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001355 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001356 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001357 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001358
1359 mTrackingReportedValues = true;
1360
1361 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1362 endSample();
1363 }
1364
1365 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001366 mCurrentReportedCount = count;
1367 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001368
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001369 /**
1370 * Adds deltaTime and deltaCount to the current sample.
1371 *
1372 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1373 * @param deltaCount additional number of times the event being sampled occurred.
1374 */
1375 public void add(long deltaTime, int deltaCount) {
1376 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001377 }
1378
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001379 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001380 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1381 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001382 if (mTrackingReportedValues) {
1383 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1384 mUnpluggedReportedCount = mCurrentReportedCount;
1385 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001386 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001387 }
1388
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001389 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001390 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1391 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1392 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001393 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001394
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001395 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001396 public void logState(Printer pw, String prefix) {
1397 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001398 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001399 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1400 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1401 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1402 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001403
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001404 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001405 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001406 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001407 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1408 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001409
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001410 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001411 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001412 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001413 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1414 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001415
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001416 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001417 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1418 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001419 out.writeInt(mCurrentReportedCount);
1420 out.writeInt(mUnpluggedReportedCount);
1421 out.writeLong(mCurrentReportedTotalTime);
1422 out.writeLong(mUnpluggedReportedTotalTime);
1423 out.writeInt(mTrackingReportedValues ? 1 : 0);
1424 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001425
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001426 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001427 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001428 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001429 mTrackingReportedValues = false;
1430 mUnpluggedReportedTotalTime = 0;
1431 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001432 return true;
1433 }
Evan Millarc64edde2009-04-18 12:26:32 -07001434 }
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
Joe Onorato92fd23f2016-07-25 11:18:42 -07001569
1570 /**
1571 * A StopwatchTimer that also tracks the total and max individual
1572 * time spent active according to the given timebase. Whereas
1573 * StopwatchTimer apportions the time amongst all in the pool,
1574 * the total and max durations are not apportioned.
1575 */
1576 public static class DurationTimer extends StopwatchTimer {
1577 /**
1578 * The time (in ms) that the timer was last acquired or the time base
1579 * last (re-)started. Increasing the nesting depth does not reset this time.
1580 *
1581 * -1 if the timer is currently not running or the time base is not running.
1582 *
1583 * If written to a parcel, the start time is reset, as is mNesting in the base class
1584 * StopwatchTimer.
1585 */
1586 long mStartTimeMs = -1;
1587
1588 /**
1589 * The longest time period (in ms) that the timer has been active.
1590 */
1591 long mMaxDurationMs;
1592
1593 /**
1594 * The total time (in ms) that that the timer has been active since reset().
1595 */
1596 long mCurrentDurationMs;
1597
1598 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1599 TimeBase timeBase, Parcel in) {
1600 super(clocks, uid, type, timerPool, timeBase, in);
1601 mMaxDurationMs = in.readLong();
1602 }
1603
1604 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1605 TimeBase timeBase) {
1606 super(clocks, uid, type, timerPool, timeBase);
1607 }
1608
1609 @Override
1610 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1611 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001612 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001613 }
1614
1615 /**
1616 * Write the summary to the parcel.
1617 *
1618 * Since the time base is probably meaningless after we come back, reading
1619 * from this will have the effect of stopping the timer. So here all we write
1620 * is the max duration.
1621 */
1622 @Override
1623 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1624 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001625 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001626 }
1627
1628 /**
1629 * Read the summary parcel.
1630 *
1631 * Has the side effect of stopping the timer.
1632 */
1633 @Override
1634 public void readSummaryFromParcelLocked(Parcel in) {
1635 super.readSummaryFromParcelLocked(in);
1636 mMaxDurationMs = in.readLong();
1637 mStartTimeMs = -1;
1638 mCurrentDurationMs = 0;
1639 }
1640
1641 /**
1642 * The TimeBase time started (again).
1643 *
1644 * If the timer is also running, store the start time.
1645 */
1646 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1647 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1648 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001649 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001650 }
1651 }
1652
1653 /**
1654 * The TimeBase stopped running.
1655 *
1656 * If the timer is running, add the duration into mCurrentDurationMs.
1657 */
1658 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001659 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1660 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001661 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001662 // baseRealtimeUs has already been converted to the timebase's realtime.
1663 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001664 }
1665 mStartTimeMs = -1;
1666 }
1667
1668 @Override
1669 public void logState(Printer pw, String prefix) {
1670 super.logState(pw, prefix);
1671 }
1672
1673 @Override
1674 public void startRunningLocked(long elapsedRealtimeMs) {
1675 super.startRunningLocked(elapsedRealtimeMs);
1676 if (mNesting == 1 && mTimeBase.isRunning()) {
1677 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001678 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001679 }
1680 }
1681
1682 /**
1683 * Decrements the mNesting ref-count on this timer.
1684 *
1685 * If it actually stopped (mNesting went to 0), then possibly update
1686 * mMaxDuration if the current duration was the longest ever.
1687 */
1688 @Override
1689 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001690 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001691 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1692 if (durationMs > mMaxDurationMs) {
1693 mMaxDurationMs = durationMs;
1694 }
1695 mStartTimeMs = -1;
1696 mCurrentDurationMs = 0;
1697 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001698 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1699 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1700 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001701 }
1702
1703 @Override
1704 public boolean reset(boolean detachIfReset) {
1705 boolean result = super.reset(detachIfReset);
1706 mMaxDurationMs = 0;
1707 mCurrentDurationMs = 0;
1708 if (mNesting > 0) {
1709 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
1710 } else {
1711 mStartTimeMs = -1;
1712 }
1713 return result;
1714 }
1715
1716 /**
1717 * Returns the max duration that this timer has ever seen.
1718 *
1719 * Note that this time is NOT split between the timers in the timer group that
1720 * this timer is attached to. It is the TOTAL time.
1721 */
1722 @Override
1723 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
1724 if (mNesting > 0) {
1725 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1726 if (durationMs > mMaxDurationMs) {
1727 return durationMs;
1728 }
1729 }
1730 return mMaxDurationMs;
1731 }
1732
1733 /**
1734 * Returns the time since the timer was started.
1735 *
1736 * Note that this time is NOT split between the timers in the timer group that
1737 * this timer is attached to. It is the TOTAL time.
1738 */
1739 @Override
1740 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
1741 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08001742 if (mNesting > 0 && mTimeBase.isRunning()) {
1743 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
1744 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001745 }
1746 return durationMs;
1747 }
1748 }
1749
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001750 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001751 * State for keeping track of timing information.
1752 */
Joe Onoratoabded112016-02-08 16:49:39 -08001753 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001754 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001755 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001756
Evan Millarc64edde2009-04-18 12:26:32 -07001757 int mNesting;
1758
Evan Millarc64edde2009-04-18 12:26:32 -07001759 /**
1760 * The last time at which we updated the timer. If mNesting is > 0,
1761 * subtract this from the current battery time to find the amount of
1762 * time we have been running since we last computed an update.
1763 */
1764 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001765
Evan Millarc64edde2009-04-18 12:26:32 -07001766 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001767 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001768 * was actually held for an interesting duration.
1769 */
1770 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001771
Amith Yamasanif37447b2009-10-08 18:28:01 -07001772 long mTimeout;
1773
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001774 /**
1775 * For partial wake locks, keep track of whether we are in the list
1776 * to consume CPU cycles.
1777 */
1778 boolean mInList;
1779
Joe Onoratoabded112016-02-08 16:49:39 -08001780 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001781 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001782 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001783 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001784 mTimerPool = timerPool;
1785 mUpdateTime = in.readLong();
1786 }
1787
Joe Onoratoabded112016-02-08 16:49:39 -08001788 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001789 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001790 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001791 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001792 mTimerPool = timerPool;
1793 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001794
Joe Onoratoabded112016-02-08 16:49:39 -08001795 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001796 mTimeout = timeout;
1797 }
1798
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001799 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1800 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001801 out.writeLong(mUpdateTime);
1802 }
1803
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001804 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001805 if (mNesting > 0) {
1806 if (DEBUG && mType < 0) {
1807 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1808 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001809 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1810 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001811 if (DEBUG && mType < 0) {
1812 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1813 }
1814 }
1815 }
1816
1817 public void logState(Printer pw, String prefix) {
1818 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001819 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 + " mAcquireTime=" + mAcquireTime);
1821 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001822
Joe Onoratoabded112016-02-08 16:49:39 -08001823 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001825 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001826 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 if (mTimerPool != null) {
1828 // Accumulate time to all currently active timers before adding
1829 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001830 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 // Add this timer to the active pool
1832 mTimerPool.add(this);
1833 }
1834 // Increment the count
1835 mCount++;
1836 mAcquireTime = mTotalTime;
1837 if (DEBUG && mType < 0) {
1838 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1839 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1840 + " mAcquireTime=" + mAcquireTime);
1841 }
1842 }
1843 }
1844
Joe Onoratoabded112016-02-08 16:49:39 -08001845 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001846 return mNesting > 0;
1847 }
1848
Joe Onoratoabded112016-02-08 16:49:39 -08001849 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 // Ignore attempt to stop a timer that isn't running
1851 if (mNesting == 0) {
1852 return;
1853 }
1854 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001855 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 if (mTimerPool != null) {
1857 // Accumulate time to all active counters, scaled by the total
1858 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001859 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 // Remove this timer from the active pool
1861 mTimerPool.remove(this);
1862 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 mNesting = 1;
1864 mTotalTime = computeRunTimeLocked(batteryRealtime);
1865 mNesting = 0;
1866 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 if (DEBUG && mType < 0) {
1869 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1870 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1871 + " mAcquireTime=" + mAcquireTime);
1872 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 if (mTotalTime == mAcquireTime) {
1875 // If there was no change in the time, then discard this
1876 // count. A somewhat cheezy strategy, but hey.
1877 mCount--;
1878 }
1879 }
1880 }
1881
Joe Onoratoabded112016-02-08 16:49:39 -08001882 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001883 if (mNesting > 0) {
1884 mNesting = 1;
1885 stopRunningLocked(elapsedRealtimeMs);
1886 }
1887 }
1888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 // Update the total time for all other running Timers with the same type as this Timer
1890 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001891 private static long refreshTimersLocked(long batteryRealtime,
1892 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001893 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 final int N = pool.size();
1895 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001896 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 long heldTime = batteryRealtime - t.mUpdateTime;
1898 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001899 final long myTime = heldTime / N;
1900 if (t == self) {
1901 selfTime = myTime;
1902 }
1903 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
1905 t.mUpdateTime = batteryRealtime;
1906 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001907 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
1909
Evan Millarc64edde2009-04-18 12:26:32 -07001910 @Override
1911 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001912 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1913 curBatteryRealtime = mUpdateTime + mTimeout;
1914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 return mTotalTime + (mNesting > 0
1916 ? (curBatteryRealtime - mUpdateTime)
1917 / (mTimerPool != null ? mTimerPool.size() : 1)
1918 : 0);
1919 }
1920
Evan Millarc64edde2009-04-18 12:26:32 -07001921 @Override
1922 protected int computeCurrentCountLocked() {
1923 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
1925
Adam Lesinskie08af192015-03-25 16:42:59 -07001926 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001927 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001928 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001929 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001930 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001931 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001932 }
1933 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001934 return canDetach;
1935 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001936
Adam Lesinskie08af192015-03-25 16:42:59 -07001937 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001938 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001939 super.detach();
1940 if (mTimerPool != null) {
1941 mTimerPool.remove(this);
1942 }
1943 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001944
Adam Lesinskie08af192015-03-25 16:42:59 -07001945 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001946 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001947 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 mNesting = 0;
1949 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001950
1951 /**
1952 * Set the mark so that we can query later for the total time the timer has
1953 * accumulated since this point. The timer can be running or not.
1954 *
1955 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1956 */
1957 public void setMark(long elapsedRealtimeMs) {
1958 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1959 if (mNesting > 0) {
1960 // We are running.
1961 if (mTimerPool != null) {
1962 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1963 } else {
1964 mTotalTime += batteryRealtime - mUpdateTime;
1965 mUpdateTime = batteryRealtime;
1966 }
1967 }
1968 mTimeBeforeMark = mTotalTime;
1969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001971
Dianne Hackbornd953c532014-08-16 18:17:38 -07001972 public abstract class OverflowArrayMap<T> {
1973 private static final String OVERFLOW_NAME = "*overflow*";
1974
Dianne Hackborn657153b2016-07-29 14:54:14 -07001975 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07001976 final ArrayMap<String, T> mMap = new ArrayMap<>();
1977 T mCurOverflow;
1978 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07001979 long mLastOverflowTime;
1980 long mLastOverflowFinishTime;
1981 long mLastClearTime;
1982 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07001983
Dianne Hackborn657153b2016-07-29 14:54:14 -07001984 public OverflowArrayMap(int uid) {
1985 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07001986 }
1987
1988 public ArrayMap<String, T> getMap() {
1989 return mMap;
1990 }
1991
1992 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07001993 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07001994 mMap.clear();
1995 mCurOverflow = null;
1996 mActiveOverflow = null;
1997 }
1998
1999 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002000 if (name == null) {
2001 name = "";
2002 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002003 mMap.put(name, obj);
2004 if (OVERFLOW_NAME.equals(name)) {
2005 mCurOverflow = obj;
2006 }
2007 }
2008
2009 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002010 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002011 if (mActiveOverflow != null) {
2012 if (mActiveOverflow.size() == 0) {
2013 mActiveOverflow = null;
2014 }
2015 }
2016 if (mActiveOverflow == null) {
2017 // There is no currently active overflow, so we should no longer have
2018 // an overflow entry.
2019 if (mMap.containsKey(OVERFLOW_NAME)) {
2020 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2021 + mMap.get(OVERFLOW_NAME));
2022 mMap.remove(OVERFLOW_NAME);
2023 }
2024 mCurOverflow = null;
2025 } else {
2026 // There is currently active overflow, so we should still have an overflow entry.
2027 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2028 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2029 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2030 }
2031 }
2032 }
2033
2034 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002035 if (name == null) {
2036 name = "";
2037 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002038 T obj = mMap.get(name);
2039 if (obj != null) {
2040 return obj;
2041 }
2042
2043 // No object exists for the given name, but do we currently have it
2044 // running as part of the overflow?
2045 if (mActiveOverflow != null) {
2046 MutableInt over = mActiveOverflow.get(name);
2047 if (over != null) {
2048 // We are already actively counting this name in the overflow object.
2049 obj = mCurOverflow;
2050 if (obj == null) {
2051 // Shouldn't be here, but we'll try to recover.
2052 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2053 obj = mCurOverflow = instantiateObject();
2054 mMap.put(OVERFLOW_NAME, obj);
2055 }
2056 over.value++;
2057 return obj;
2058 }
2059 }
2060
2061 // No object exists for given name nor in the overflow; we need to make
2062 // a new one.
2063 final int N = mMap.size();
2064 if (N >= MAX_WAKELOCKS_PER_UID) {
2065 // Went over the limit on number of objects to track; this one goes
2066 // in to the overflow.
2067 obj = mCurOverflow;
2068 if (obj == null) {
2069 // Need to start overflow now...
2070 obj = mCurOverflow = instantiateObject();
2071 mMap.put(OVERFLOW_NAME, obj);
2072 }
2073 if (mActiveOverflow == null) {
2074 mActiveOverflow = new ArrayMap<>();
2075 }
2076 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002077 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002078 return obj;
2079 }
2080
2081 // Normal case where we just need to make a new object.
2082 obj = instantiateObject();
2083 mMap.put(name, obj);
2084 return obj;
2085 }
2086
2087 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002088 if (name == null) {
2089 name = "";
2090 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002091 T obj = mMap.get(name);
2092 if (obj != null) {
2093 return obj;
2094 }
2095
2096 // No object exists for the given name, but do we currently have it
2097 // running as part of the overflow?
2098 if (mActiveOverflow != null) {
2099 MutableInt over = mActiveOverflow.get(name);
2100 if (over != null) {
2101 // We are already actively counting this name in the overflow object.
2102 obj = mCurOverflow;
2103 if (obj != null) {
2104 over.value--;
2105 if (over.value <= 0) {
2106 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002107 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002108 }
2109 return obj;
2110 }
2111 }
2112 }
2113
2114 // Huh, they are stopping an active operation but we can't find one!
2115 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002116 StringBuilder sb = new StringBuilder();
2117 sb.append("Unable to find object for ");
2118 sb.append(name);
2119 sb.append(" in uid ");
2120 sb.append(mUid);
2121 sb.append(" mapsize=");
2122 sb.append(mMap.size());
2123 sb.append(" activeoverflow=");
2124 sb.append(mActiveOverflow);
2125 sb.append(" curoverflow=");
2126 sb.append(mCurOverflow);
2127 long now = SystemClock.elapsedRealtime();
2128 if (mLastOverflowTime != 0) {
2129 sb.append(" lastOverflowTime=");
2130 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2131 }
2132 if (mLastOverflowFinishTime != 0) {
2133 sb.append(" lastOverflowFinishTime=");
2134 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2135 }
2136 if (mLastClearTime != 0) {
2137 sb.append(" lastClearTime=");
2138 TimeUtils.formatDuration(mLastClearTime-now, sb);
2139 }
2140 if (mLastCleanupTime != 0) {
2141 sb.append(" lastCleanupTime=");
2142 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2143 }
2144 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002145 return null;
2146 }
2147
2148 public abstract T instantiateObject();
2149 }
2150
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002151 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2152 implements Parcelable {
2153 private final LongSamplingCounter mIdleTimeMillis;
2154 private final LongSamplingCounter mRxTimeMillis;
2155 private final LongSamplingCounter[] mTxTimeMillis;
2156 private final LongSamplingCounter mPowerDrainMaMs;
2157
2158 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2159 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2160 mRxTimeMillis = new LongSamplingCounter(timeBase);
2161 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2162 for (int i = 0; i < numTxStates; i++) {
2163 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2164 }
2165 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2166 }
2167
2168 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2169 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2170 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2171 final int recordedTxStates = in.readInt();
2172 if (recordedTxStates != numTxStates) {
2173 throw new ParcelFormatException("inconsistent tx state lengths");
2174 }
2175
2176 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2177 for (int i = 0; i < numTxStates; i++) {
2178 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2179 }
2180 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2181 }
2182
2183 public void readSummaryFromParcel(Parcel in) {
2184 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2185 mRxTimeMillis.readSummaryFromParcelLocked(in);
2186 final int recordedTxStates = in.readInt();
2187 if (recordedTxStates != mTxTimeMillis.length) {
2188 throw new ParcelFormatException("inconsistent tx state lengths");
2189 }
2190 for (LongSamplingCounter counter : mTxTimeMillis) {
2191 counter.readSummaryFromParcelLocked(in);
2192 }
2193 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2194 }
2195
2196 @Override
2197 public int describeContents() {
2198 return 0;
2199 }
2200
2201 public void writeSummaryToParcel(Parcel dest) {
2202 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2203 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2204 dest.writeInt(mTxTimeMillis.length);
2205 for (LongSamplingCounter counter : mTxTimeMillis) {
2206 counter.writeSummaryFromParcelLocked(dest);
2207 }
2208 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2209 }
2210
2211 @Override
2212 public void writeToParcel(Parcel dest, int flags) {
2213 mIdleTimeMillis.writeToParcel(dest);
2214 mRxTimeMillis.writeToParcel(dest);
2215 dest.writeInt(mTxTimeMillis.length);
2216 for (LongSamplingCounter counter : mTxTimeMillis) {
2217 counter.writeToParcel(dest);
2218 }
2219 mPowerDrainMaMs.writeToParcel(dest);
2220 }
2221
2222 public void reset(boolean detachIfReset) {
2223 mIdleTimeMillis.reset(detachIfReset);
2224 mRxTimeMillis.reset(detachIfReset);
2225 for (LongSamplingCounter counter : mTxTimeMillis) {
2226 counter.reset(detachIfReset);
2227 }
2228 mPowerDrainMaMs.reset(detachIfReset);
2229 }
2230
2231 public void detach() {
2232 mIdleTimeMillis.detach();
2233 mRxTimeMillis.detach();
2234 for (LongSamplingCounter counter : mTxTimeMillis) {
2235 counter.detach();
2236 }
2237 mPowerDrainMaMs.detach();
2238 }
2239
2240 /**
2241 * @return a LongSamplingCounter, measuring time spent in the idle state in
2242 * milliseconds.
2243 */
2244 @Override
2245 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002246 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002247 }
2248
2249 /**
2250 * @return a LongSamplingCounter, measuring time spent in the receive state in
2251 * milliseconds.
2252 */
2253 @Override
2254 public LongSamplingCounter getRxTimeCounter() {
2255 return mRxTimeMillis;
2256 }
2257
2258 /**
2259 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2260 * milliseconds.
2261 */
2262 @Override
2263 public LongSamplingCounter[] getTxTimeCounters() {
2264 return mTxTimeMillis;
2265 }
2266
2267 /**
2268 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2269 */
2270 @Override
2271 public LongSamplingCounter getPowerCounter() {
2272 return mPowerDrainMaMs;
2273 }
2274 }
2275
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002276 /*
2277 * Get the wakeup reason counter, and create a new one if one
2278 * doesn't already exist.
2279 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002280 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2281 SamplingTimer timer = mWakeupReasonStats.get(name);
2282 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002283 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002284 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002285 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002286 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002287 }
2288
Evan Millarc64edde2009-04-18 12:26:32 -07002289 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002290 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002291 * doesn't already exist.
2292 */
2293 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2294 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2295 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002296 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002297 mKernelWakelockStats.put(name, kwlt);
2298 }
2299 return kwlt;
2300 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002301
James Carr3a226052016-07-01 14:49:52 -07002302 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2303 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2304 if (kmt == null) {
2305 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2306 mKernelMemoryStats.put(bucket, kmt);
2307 }
2308 return kmt;
2309 }
2310
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002311 private int writeHistoryTag(HistoryTag tag) {
2312 Integer idxObj = mHistoryTagPool.get(tag);
2313 int idx;
2314 if (idxObj != null) {
2315 idx = idxObj;
2316 } else {
2317 idx = mNextHistoryTagIdx;
2318 HistoryTag key = new HistoryTag();
2319 key.setTo(tag);
2320 tag.poolIdx = idx;
2321 mHistoryTagPool.put(key, idx);
2322 mNextHistoryTagIdx++;
2323 mNumHistoryTagChars += key.string.length() + 1;
2324 }
2325 return idx;
2326 }
2327
2328 private void readHistoryTag(int index, HistoryTag tag) {
2329 tag.string = mReadHistoryStrings[index];
2330 tag.uid = mReadHistoryUids[index];
2331 tag.poolIdx = index;
2332 }
2333
Adam Lesinski926969b2016-04-28 17:31:12 -07002334 /*
2335 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2336
2337 There is always the first token, which may contain the delta time, or an indicator of
2338 the length of the time (int or long) following this token.
2339
2340 First token: always present,
2341 31 23 15 7 0
2342 â–ˆ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â–ˆ
2343
2344 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2345 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2346 delta time.
2347 A: battery level changed and an int follows with battery data.
2348 B: state changed and an int follows with state change data.
2349 C: state2 has changed and an int follows with state2 change data.
2350 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2351 E: event data has changed and an event struct follows.
2352 F: battery charge in coulombs has changed and an int with the charge follows.
2353 G: state flag denoting that the mobile radio was active.
2354 H: state flag denoting that the wifi radio was active.
2355 I: state flag denoting that a wifi scan occurred.
2356 J: state flag denoting that a wifi full lock was held.
2357 K: state flag denoting that the gps was on.
2358 L: state flag denoting that a wakelock was held.
2359 M: state flag denoting that the cpu was running.
2360
2361 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2362 with the time delta.
2363
2364 Battery level int: if A in the first token is set,
2365 31 23 15 7 0
2366 â–ˆ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â–ˆ
2367
2368 D: indicates that extra history details follow.
2369 V: the battery voltage.
2370 T: the battery temperature.
2371 L: the battery level (out of 100).
2372
2373 State change int: if B in the first token is set,
2374 31 23 15 7 0
2375 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2376
2377 A: wifi multicast was on.
2378 B: battery was plugged in.
2379 C: screen was on.
2380 D: phone was scanning for signal.
2381 E: audio was on.
2382 F: a sensor was active.
2383
2384 State2 change int: if C in the first token is set,
2385 31 23 15 7 0
2386 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2387
2388 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2389 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2390 C: a bluetooth scan was active.
2391 D: the camera was active.
2392 E: bluetooth was on.
2393 F: a phone call was active.
2394 G: the device was charging.
2395 H: 2 bits indicating the device-idle (doze) state: off, light, full
2396 I: the flashlight was on.
2397 J: wifi was on.
2398 K: wifi was running.
2399 L: video was playing.
2400 M: power save mode was on.
2401
2402 Wakelock/wakereason struct: if D in the first token is set,
2403 TODO(adamlesinski): describe wakelock/wakereason struct.
2404
2405 Event struct: if E in the first token is set,
2406 TODO(adamlesinski): describe the event struct.
2407
2408 History step details struct: if D in the battery level int is set,
2409 TODO(adamlesinski): describe the history step details struct.
2410
2411 Battery charge int: if F in the first token is set, an int representing the battery charge
2412 in coulombs follows.
2413 */
2414
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002415 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002416 static final int DELTA_TIME_MASK = 0x7ffff;
2417 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2418 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2419 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002420 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002421 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002422 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002423 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002424 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002425 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002426 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002427 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002428 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002429 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002430 // Flag in delta int: contains the battery charge count in uAh.
2431 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002432 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002433 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002434
2435 // These are the pieces of battery state that are packed in to the upper bits of
2436 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002437 // in STATE_BATTERY_MASK.
2438 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002439 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2440 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2441 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2442 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2443 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2444 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002445
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002446 // We use the low bit of the battery state int to indicate that we have full details
2447 // from a battery level change.
2448 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2449
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002450 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002451 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002452 dest.writeInt(DELTA_TIME_ABS);
2453 cur.writeToParcel(dest, 0);
2454 return;
2455 }
2456
2457 final long deltaTime = cur.time - last.time;
2458 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2459 final int lastStateInt = buildStateInt(last);
2460
2461 int deltaTimeToken;
2462 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2463 deltaTimeToken = DELTA_TIME_LONG;
2464 } else if (deltaTime >= DELTA_TIME_ABS) {
2465 deltaTimeToken = DELTA_TIME_INT;
2466 } else {
2467 deltaTimeToken = (int)deltaTime;
2468 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002469 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002470 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2471 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2472 final boolean computeStepDetails = includeStepDetails != 0
2473 || mLastHistoryStepDetails == null;
2474 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002475 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2476 if (batteryLevelIntChanged) {
2477 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2478 }
2479 final int stateInt = buildStateInt(cur);
2480 final boolean stateIntChanged = stateInt != lastStateInt;
2481 if (stateIntChanged) {
2482 firstToken |= DELTA_STATE_FLAG;
2483 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002484 final boolean state2IntChanged = cur.states2 != last.states2;
2485 if (state2IntChanged) {
2486 firstToken |= DELTA_STATE2_FLAG;
2487 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002488 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002489 firstToken |= DELTA_WAKELOCK_FLAG;
2490 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002491 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2492 firstToken |= DELTA_EVENT_FLAG;
2493 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002494
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002495 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2496 if (batteryChargeChanged) {
2497 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002498 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002499 dest.writeInt(firstToken);
2500 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2501 + " deltaTime=" + deltaTime);
2502
2503 if (deltaTimeToken >= DELTA_TIME_INT) {
2504 if (deltaTimeToken == DELTA_TIME_INT) {
2505 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2506 dest.writeInt((int)deltaTime);
2507 } else {
2508 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2509 dest.writeLong(deltaTime);
2510 }
2511 }
2512 if (batteryLevelIntChanged) {
2513 dest.writeInt(batteryLevelInt);
2514 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2515 + Integer.toHexString(batteryLevelInt)
2516 + " batteryLevel=" + cur.batteryLevel
2517 + " batteryTemp=" + cur.batteryTemperature
2518 + " batteryVolt=" + (int)cur.batteryVoltage);
2519 }
2520 if (stateIntChanged) {
2521 dest.writeInt(stateInt);
2522 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2523 + Integer.toHexString(stateInt)
2524 + " batteryStatus=" + cur.batteryStatus
2525 + " batteryHealth=" + cur.batteryHealth
2526 + " batteryPlugType=" + cur.batteryPlugType
2527 + " states=0x" + Integer.toHexString(cur.states));
2528 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002529 if (state2IntChanged) {
2530 dest.writeInt(cur.states2);
2531 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2532 + Integer.toHexString(cur.states2));
2533 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002534 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2535 int wakeLockIndex;
2536 int wakeReasonIndex;
2537 if (cur.wakelockTag != null) {
2538 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2539 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2540 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2541 } else {
2542 wakeLockIndex = 0xffff;
2543 }
2544 if (cur.wakeReasonTag != null) {
2545 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2546 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2547 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2548 } else {
2549 wakeReasonIndex = 0xffff;
2550 }
2551 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002552 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002553 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002554 int index = writeHistoryTag(cur.eventTag);
2555 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002556 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002557 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2558 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2559 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002560 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002561 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002562 if (mPlatformIdleStateCallback != null) {
2563 mCurHistoryStepDetails.statPlatformIdleState =
2564 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2565 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2566 mCurHistoryStepDetails.statPlatformIdleState);
2567 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002568 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2569 if (includeStepDetails != 0) {
2570 mCurHistoryStepDetails.writeToParcel(dest);
2571 }
2572 cur.stepDetails = mCurHistoryStepDetails;
2573 mLastHistoryStepDetails = mCurHistoryStepDetails;
2574 } else {
2575 cur.stepDetails = null;
2576 }
2577 if (mLastHistoryStepLevel < cur.batteryLevel) {
2578 mLastHistoryStepDetails = null;
2579 }
2580 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002581
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002582 if (batteryChargeChanged) {
2583 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2584 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002585 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002586 }
2587
2588 private int buildBatteryLevelInt(HistoryItem h) {
2589 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002590 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2591 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2592 }
2593
2594 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2595 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2596 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2597 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002598 }
2599
2600 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002601 int plugType = 0;
2602 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2603 plugType = 1;
2604 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2605 plugType = 2;
2606 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2607 plugType = 3;
2608 }
2609 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2610 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2611 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002612 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002613 }
2614
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002615 private void computeHistoryStepDetails(final HistoryStepDetails out,
2616 final HistoryStepDetails last) {
2617 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2618
2619 // Perform a CPU update right after we do this collection, so we have started
2620 // collecting good data for the next step.
2621 requestImmediateCpuUpdate();
2622
2623 if (last == null) {
2624 // We are not generating a delta, so all we need to do is reset the stats
2625 // we will later be doing a delta from.
2626 final int NU = mUidStats.size();
2627 for (int i=0; i<NU; i++) {
2628 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2629 uid.mLastStepUserTime = uid.mCurStepUserTime;
2630 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2631 }
2632 mLastStepCpuUserTime = mCurStepCpuUserTime;
2633 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2634 mLastStepStatUserTime = mCurStepStatUserTime;
2635 mLastStepStatSystemTime = mCurStepStatSystemTime;
2636 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2637 mLastStepStatIrqTime = mCurStepStatIrqTime;
2638 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2639 mLastStepStatIdleTime = mCurStepStatIdleTime;
2640 tmp.clear();
2641 return;
2642 }
2643 if (DEBUG) {
2644 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2645 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2646 + " irq=" + mLastStepStatIrqTime + " sirq="
2647 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2648 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2649 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2650 + " irq=" + mCurStepStatIrqTime + " sirq="
2651 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2652 }
2653 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2654 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2655 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2656 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2657 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2658 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2659 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2660 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2661 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2662 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2663 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2664 final int NU = mUidStats.size();
2665 for (int i=0; i<NU; i++) {
2666 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2667 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2668 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2669 final int totalTime = totalUTime + totalSTime;
2670 uid.mLastStepUserTime = uid.mCurStepUserTime;
2671 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2672 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2673 continue;
2674 }
2675 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2676 out.appCpuUid3 = uid.mUid;
2677 out.appCpuUTime3 = totalUTime;
2678 out.appCpuSTime3 = totalSTime;
2679 } else {
2680 out.appCpuUid3 = out.appCpuUid2;
2681 out.appCpuUTime3 = out.appCpuUTime2;
2682 out.appCpuSTime3 = out.appCpuSTime2;
2683 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2684 out.appCpuUid2 = uid.mUid;
2685 out.appCpuUTime2 = totalUTime;
2686 out.appCpuSTime2 = totalSTime;
2687 } else {
2688 out.appCpuUid2 = out.appCpuUid1;
2689 out.appCpuUTime2 = out.appCpuUTime1;
2690 out.appCpuSTime2 = out.appCpuSTime1;
2691 out.appCpuUid1 = uid.mUid;
2692 out.appCpuUTime1 = totalUTime;
2693 out.appCpuSTime1 = totalSTime;
2694 }
2695 }
2696 }
2697 mLastStepCpuUserTime = mCurStepCpuUserTime;
2698 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2699 mLastStepStatUserTime = mCurStepStatUserTime;
2700 mLastStepStatSystemTime = mCurStepStatSystemTime;
2701 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2702 mLastStepStatIrqTime = mCurStepStatIrqTime;
2703 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2704 mLastStepStatIdleTime = mCurStepStatIdleTime;
2705 }
2706
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002707 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2708 int firstToken = src.readInt();
2709 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002710 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002711 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002712 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2713 + " deltaTimeToken=" + deltaTimeToken);
2714
2715 if (deltaTimeToken < DELTA_TIME_ABS) {
2716 cur.time += deltaTimeToken;
2717 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2718 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002719 cur.numReadInts += 2;
2720 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002721 cur.readFromParcel(src);
2722 return;
2723 } else if (deltaTimeToken == DELTA_TIME_INT) {
2724 int delta = src.readInt();
2725 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002726 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002727 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2728 } else {
2729 long delta = src.readLong();
2730 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2731 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002732 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002733 }
2734
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002735 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002736 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002737 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002738 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002739 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002740 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2741 + Integer.toHexString(batteryLevelInt)
2742 + " batteryLevel=" + cur.batteryLevel
2743 + " batteryTemp=" + cur.batteryTemperature
2744 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002745 } else {
2746 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002747 }
2748
2749 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2750 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002751 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002752 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2753 & STATE_BATTERY_STATUS_MASK);
2754 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2755 & STATE_BATTERY_HEALTH_MASK);
2756 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2757 & STATE_BATTERY_PLUG_MASK);
2758 switch (cur.batteryPlugType) {
2759 case 1:
2760 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2761 break;
2762 case 2:
2763 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2764 break;
2765 case 3:
2766 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2767 break;
2768 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002769 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002770 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2771 + Integer.toHexString(stateInt)
2772 + " batteryStatus=" + cur.batteryStatus
2773 + " batteryHealth=" + cur.batteryHealth
2774 + " batteryPlugType=" + cur.batteryPlugType
2775 + " states=0x" + Integer.toHexString(cur.states));
2776 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002777 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002778 }
2779
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002780 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2781 cur.states2 = src.readInt();
2782 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2783 + Integer.toHexString(cur.states2));
2784 }
2785
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002786 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002787 int indexes = src.readInt();
2788 int wakeLockIndex = indexes&0xffff;
2789 int wakeReasonIndex = (indexes>>16)&0xffff;
2790 if (wakeLockIndex != 0xffff) {
2791 cur.wakelockTag = cur.localWakelockTag;
2792 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2793 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2794 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2795 } else {
2796 cur.wakelockTag = null;
2797 }
2798 if (wakeReasonIndex != 0xffff) {
2799 cur.wakeReasonTag = cur.localWakeReasonTag;
2800 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2801 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2802 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2803 } else {
2804 cur.wakeReasonTag = null;
2805 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002806 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002807 } else {
2808 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002809 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002810 }
2811
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002812 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002813 cur.eventTag = cur.localEventTag;
2814 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002815 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002816 final int index = ((codeAndIndex>>16)&0xffff);
2817 readHistoryTag(index, cur.eventTag);
2818 cur.numReadInts += 1;
2819 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2820 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2821 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002822 } else {
2823 cur.eventCode = HistoryItem.EVENT_NONE;
2824 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002825
2826 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2827 cur.stepDetails = mReadHistoryStepDetails;
2828 cur.stepDetails.readFromParcel(src);
2829 } else {
2830 cur.stepDetails = null;
2831 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002832
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002833 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2834 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002835 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002836 }
2837
Dianne Hackbornfc064132014-06-02 12:42:12 -07002838 @Override
2839 public void commitCurrentHistoryBatchLocked() {
2840 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2841 }
2842
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002843 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002844 if (!mHaveBatteryLevel || !mRecordingHistory) {
2845 return;
2846 }
2847
Dianne Hackborn40c87252014-03-19 16:55:40 -07002848 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002849 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2850 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002851 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002852 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002853 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2854 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002855 + Integer.toHexString(lastDiffStates) + " diff2="
2856 + Integer.toHexString(diffStates2) + " lastDiff2="
2857 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002858 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002859 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002860 && (diffStates2&lastDiffStates2) == 0
2861 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2862 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002863 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002864 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002865 || cur.eventCode == HistoryItem.EVENT_NONE)
2866 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2867 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2868 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2869 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2870 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2871 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002872 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002873 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002874 // as long as no bit has changed both between now and the last entry, as
2875 // well as the last entry and the one before it (so we capture any toggles).
2876 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002877 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2878 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2879 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002880 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002881 // If the last written history had a wakelock tag, we need to retain it.
2882 // Note that the condition above made sure that we aren't in a case where
2883 // both it and the current history item have a wakelock tag.
2884 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002885 cur.wakelockTag = cur.localWakelockTag;
2886 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002887 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002888 // If the last written history had a wake reason tag, we need to retain it.
2889 // Note that the condition above made sure that we aren't in a case where
2890 // both it and the current history item have a wakelock tag.
2891 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002892 cur.wakeReasonTag = cur.localWakeReasonTag;
2893 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002894 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002895 // If the last written history had an event, we need to retain it.
2896 // Note that the condition above made sure that we aren't in a case where
2897 // both it and the current history item have an event.
2898 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002899 cur.eventCode = mHistoryLastWritten.eventCode;
2900 cur.eventTag = cur.localEventTag;
2901 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002902 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002903 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002904 }
2905
Adam Lesinski45489782016-12-15 23:45:17 -08002906 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002907 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08002908 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
2909 // Clients can't deal with history buffers this large. This only
2910 // really happens when the device is on charger and interacted with
2911 // for long periods of time, like in retail mode. Since the device is
2912 // most likely charged, when unplugged, stats would have reset anyways.
2913 // Reset the stats and mark that we overflowed.
2914 // b/32540341
2915 resetAllStatsLocked();
2916
2917 // Mark that we want to set *OVERFLOW* event and the RESET:START
2918 // events.
2919 recordResetDueToOverflow = true;
2920
2921 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002922 if (!mHistoryOverflow) {
2923 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002924 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2925 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002926 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002927 }
2928
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002929 // After overflow, we allow various bit-wise states to settle to 0.
2930 boolean writeAnyway = false;
2931 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2932 & mActiveHistoryStates;
2933 if (mHistoryLastWritten.states != curStates) {
2934 // mActiveHistoryStates keeps track of which bits in .states are now being
2935 // forced to 0.
2936 int old = mActiveHistoryStates;
2937 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2938 writeAnyway |= old != mActiveHistoryStates;
2939 }
2940 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2941 & mActiveHistoryStates2;
2942 if (mHistoryLastWritten.states2 != curStates2) {
2943 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2944 // forced to 0.
2945 int old = mActiveHistoryStates2;
2946 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2947 writeAnyway |= old != mActiveHistoryStates2;
2948 }
2949
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002950 // Once we've reached the maximum number of items, we only
2951 // record changes to the battery level and the most interesting states.
2952 // Once we've reached the maximum maximum number of items, we only
2953 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002954 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002955 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002956 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002957 & HistoryItem.MOST_INTERESTING_STATES) == 0
2958 || ((mHistoryLastWritten.states2^cur.states2)
2959 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002960 return;
2961 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002962
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002963 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002964 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002965 }
2966
Adam Lesinski45489782016-12-15 23:45:17 -08002967 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002968 // The history is currently empty; we need it to start with a time stamp.
2969 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08002970 if (recordResetDueToOverflow) {
2971 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
2972 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002973 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2974 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002975 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002976 }
2977
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002978 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2979 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002980 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002981 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002982 }
2983 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2984 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002985 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002986 mHistoryLastWritten.states &= mActiveHistoryStates;
2987 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002988 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002989 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002990 cur.wakelockTag = null;
2991 cur.wakeReasonTag = null;
2992 cur.eventCode = HistoryItem.EVENT_NONE;
2993 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002994 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2995 + " now " + mHistoryBuffer.dataPosition()
2996 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002997 }
2998
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002999 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003000 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003001
Dianne Hackborn40c87252014-03-19 16:55:40 -07003002 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003003 if (mTrackRunningHistoryElapsedRealtime != 0) {
3004 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3005 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3006 if (diffUptime < (diffElapsed-20)) {
3007 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3008 mHistoryAddTmp.setTo(mHistoryLastWritten);
3009 mHistoryAddTmp.wakelockTag = null;
3010 mHistoryAddTmp.wakeReasonTag = null;
3011 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3012 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3013 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3014 }
3015 }
3016 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3017 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3018 mTrackRunningHistoryUptime = uptimeMs;
3019 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3020 }
3021
3022 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3023 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003024
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003025 if (!USE_OLD_HISTORY) {
3026 return;
3027 }
3028
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003029 if (!mHaveBatteryLevel || !mRecordingHistory) {
3030 return;
3031 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003032
3033 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003034 // and no states have since the last recorded entry changed and
3035 // are now resetting back to their original value, then just collapse
3036 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003037 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003038 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003039 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3040 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003041 // If the current is the same as the one before, then we no
3042 // longer need the entry.
3043 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003044 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003045 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003046 mHistoryLastEnd.next = null;
3047 mHistoryEnd.next = mHistoryCache;
3048 mHistoryCache = mHistoryEnd;
3049 mHistoryEnd = mHistoryLastEnd;
3050 mHistoryLastEnd = null;
3051 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003052 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3053 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003054 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003055 }
3056 return;
3057 }
3058
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003059 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003060 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003061
3062 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3063 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003064 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003065 }
3066
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003067 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3068 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003069 // record changes to the battery level and the most interesting states.
3070 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003071 // record changes to the battery level.
3072 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003073 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003074 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003075 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003076 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003077 return;
3078 }
3079 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003080
Dianne Hackborn40c87252014-03-19 16:55:40 -07003081 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003082 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003083
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003084 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003085 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003086 mHistoryCur.eventCode = code;
3087 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3088 mHistoryCur.eventTag.string = name;
3089 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003090 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003091 }
3092
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003093 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003094 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003095 if (rec != null) {
3096 mHistoryCache = rec.next;
3097 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003098 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003099 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003100 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003101
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003102 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003103 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003104
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003105 void addHistoryRecordLocked(HistoryItem rec) {
3106 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003107 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003108 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003109 if (mHistoryEnd != null) {
3110 mHistoryEnd.next = rec;
3111 mHistoryEnd = rec;
3112 } else {
3113 mHistory = mHistoryEnd = rec;
3114 }
3115 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003116
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003117 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003118 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003119 if (USE_OLD_HISTORY) {
3120 if (mHistory != null) {
3121 mHistoryEnd.next = mHistoryCache;
3122 mHistoryCache = mHistory;
3123 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3124 }
3125 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003126 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003127
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003128 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003129 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003130 mTrackRunningHistoryElapsedRealtime = 0;
3131 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003132
3133 mHistoryBuffer.setDataSize(0);
3134 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003135 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003136 mHistoryLastLastWritten.clear();
3137 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003138 mHistoryTagPool.clear();
3139 mNextHistoryTagIdx = 0;
3140 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003141 mHistoryBufferLastPos = -1;
3142 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003143 mActiveHistoryStates = 0xffffffff;
3144 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003145 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003146
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003147 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
3148 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07003149 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003150
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003151 boolean unpluggedScreenOff = unplugged && screenOff;
3152 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
3153 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07003154 if (DEBUG_ENERGY_CPU) {
3155 Slog.d(TAG, "Updating cpu time because screen is now " +
3156 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003157 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003158 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003159 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 }
3161 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003162
Dianne Hackborn099bc622014-01-22 13:39:16 -08003163 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3164 mIsolatedUids.put(isolatedUid, appUid);
3165 }
3166
Adam Lesinski61db88f2015-07-01 15:05:07 -07003167 /**
3168 * Schedules a read of the latest cpu times before removing the isolated UID.
3169 * @see #removeIsolatedUidLocked(int)
3170 */
3171 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003172 int curUid = mIsolatedUids.get(isolatedUid, -1);
3173 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003174 if (mExternalSync != null) {
3175 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3176 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003177 }
3178 }
3179
Adam Lesinski61db88f2015-07-01 15:05:07 -07003180 /**
3181 * This should only be called after the cpu times have been read.
3182 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3183 */
3184 public void removeIsolatedUidLocked(int isolatedUid) {
3185 mIsolatedUids.delete(isolatedUid);
3186 mKernelUidCpuTimeReader.removeUid(isolatedUid);
3187 }
3188
Dianne Hackborn099bc622014-01-22 13:39:16 -08003189 public int mapUid(int uid) {
3190 int isolated = mIsolatedUids.get(uid, -1);
3191 return isolated > 0 ? isolated : uid;
3192 }
3193
3194 public void noteEventLocked(int code, String name, int uid) {
3195 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003196 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3197 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003198 }
Joe Onoratoabded112016-02-08 16:49:39 -08003199 final long elapsedRealtime = mClocks.elapsedRealtime();
3200 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003201 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003202 }
3203
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003204 boolean ensureStartClockTime(final long currentTime) {
3205 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3206 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3207 // If the start clock time has changed by more than a year, then presumably
3208 // the previous time was completely bogus. So we are going to figure out a
3209 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003210 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003211 return true;
3212 }
3213 return false;
3214 }
3215
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003216 public void noteCurrentTimeChangedLocked() {
3217 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003218 final long elapsedRealtime = mClocks.elapsedRealtime();
3219 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003220 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003221 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003222 }
3223
Dianne Hackborn61659e52014-07-09 16:13:01 -07003224 public void noteProcessStartLocked(String name, int uid) {
3225 uid = mapUid(uid);
3226 if (isOnBattery()) {
3227 Uid u = getUidStatsLocked(uid);
3228 u.getProcessStatsLocked(name).incStartsLocked();
3229 }
3230 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3231 return;
3232 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003233 if (!mRecordAllHistory) {
3234 return;
3235 }
Joe Onoratoabded112016-02-08 16:49:39 -08003236 final long elapsedRealtime = mClocks.elapsedRealtime();
3237 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003238 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3239 }
3240
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003241 public void noteProcessCrashLocked(String name, int uid) {
3242 uid = mapUid(uid);
3243 if (isOnBattery()) {
3244 Uid u = getUidStatsLocked(uid);
3245 u.getProcessStatsLocked(name).incNumCrashesLocked();
3246 }
3247 }
3248
3249 public void noteProcessAnrLocked(String name, int uid) {
3250 uid = mapUid(uid);
3251 if (isOnBattery()) {
3252 Uid u = getUidStatsLocked(uid);
3253 u.getProcessStatsLocked(name).incNumAnrsLocked();
3254 }
3255 }
3256
Dianne Hackborna8d10942015-11-19 17:55:19 -08003257 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003258 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003259 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003260 }
3261
3262 public void noteProcessFinishLocked(String name, int uid) {
3263 uid = mapUid(uid);
3264 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3265 return;
3266 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003267 if (!mRecordAllHistory) {
3268 return;
3269 }
Joe Onoratoabded112016-02-08 16:49:39 -08003270 final long elapsedRealtime = mClocks.elapsedRealtime();
3271 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003272 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003273 }
3274
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003275 public void noteSyncStartLocked(String name, int uid) {
3276 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003277 final long elapsedRealtime = mClocks.elapsedRealtime();
3278 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003279 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3280 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3281 return;
3282 }
3283 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3284 }
3285
3286 public void noteSyncFinishLocked(String name, int uid) {
3287 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003288 final long elapsedRealtime = mClocks.elapsedRealtime();
3289 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003290 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3291 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3292 return;
3293 }
3294 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3295 }
3296
3297 public void noteJobStartLocked(String name, int uid) {
3298 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003299 final long elapsedRealtime = mClocks.elapsedRealtime();
3300 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003301 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3302 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3303 return;
3304 }
3305 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3306 }
3307
3308 public void noteJobFinishLocked(String name, int uid) {
3309 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003310 final long elapsedRealtime = mClocks.elapsedRealtime();
3311 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003312 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3313 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3314 return;
3315 }
3316 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3317 }
3318
Dianne Hackborn1e383822015-04-10 14:02:33 -07003319 public void noteAlarmStartLocked(String name, int uid) {
3320 if (!mRecordAllHistory) {
3321 return;
3322 }
3323 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003324 final long elapsedRealtime = mClocks.elapsedRealtime();
3325 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003326 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3327 return;
3328 }
3329 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3330 }
3331
3332 public void noteAlarmFinishLocked(String name, int uid) {
3333 if (!mRecordAllHistory) {
3334 return;
3335 }
3336 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003337 final long elapsedRealtime = mClocks.elapsedRealtime();
3338 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003339 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3340 return;
3341 }
3342 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3343 }
3344
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003345 private void requestWakelockCpuUpdate() {
3346 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3347 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3348 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3349 }
3350 }
3351
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003352 private void requestImmediateCpuUpdate() {
3353 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3354 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3355 }
3356
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003357 public void setRecordAllHistoryLocked(boolean enabled) {
3358 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003359 if (!enabled) {
3360 // Clear out any existing state.
3361 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003362 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003363 // Record the currently running processes as stopping, now that we are no
3364 // longer tracking them.
3365 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3366 HistoryItem.EVENT_PROC);
3367 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003368 long mSecRealtime = mClocks.elapsedRealtime();
3369 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003370 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3371 SparseIntArray uids = ent.getValue();
3372 for (int j=0; j<uids.size(); j++) {
3373 addHistoryEventLocked(mSecRealtime, mSecUptime,
3374 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3375 }
3376 }
3377 }
3378 } else {
3379 // Record the currently running processes as starting, now that we are tracking them.
3380 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3381 HistoryItem.EVENT_PROC);
3382 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003383 long mSecRealtime = mClocks.elapsedRealtime();
3384 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003385 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3386 SparseIntArray uids = ent.getValue();
3387 for (int j=0; j<uids.size(); j++) {
3388 addHistoryEventLocked(mSecRealtime, mSecUptime,
3389 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3390 }
3391 }
3392 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003393 }
3394 }
3395
Dianne Hackborn9a755432014-05-15 17:05:22 -07003396 public void setNoAutoReset(boolean enabled) {
3397 mNoAutoReset = enabled;
3398 }
3399
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003400 public void setPretendScreenOff(boolean pretendScreenOff) {
3401 mPretendScreenOff = pretendScreenOff;
3402 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3403 }
3404
Dianne Hackborn9a755432014-05-15 17:05:22 -07003405 private String mInitialAcquireWakeName;
3406 private int mInitialAcquireWakeUid = -1;
3407
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003408 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003409 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003410 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003411 if (type == WAKE_TYPE_PARTIAL) {
3412 // Only care about partial wake locks, since full wake locks
3413 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003414 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003415 if (historyName == null) {
3416 historyName = name;
3417 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003418 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003419 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3420 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003421 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003422 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003423 }
3424 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003425 if (mWakeLockNesting == 0) {
3426 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3427 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3428 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003429 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003430 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003431 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003432 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003433 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003434 } else if (!mWakeLockImportant && !unimportantForLogging
3435 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003436 if (mHistoryLastWritten.wakelockTag != null) {
3437 // We'll try to update the last tag.
3438 mHistoryLastWritten.wakelockTag = null;
3439 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003440 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003441 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003442 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003443 }
3444 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003445 }
3446 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003447 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003448 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003449 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3450 // We only update the cpu time when a wake lock is acquired if the screen is off.
3451 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3452 if (DEBUG_ENERGY_CPU) {
3453 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3454 }
3455 requestWakelockCpuUpdate();
3456 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003457 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003458 }
3459 }
3460
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003461 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3462 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003463 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003464 if (type == WAKE_TYPE_PARTIAL) {
3465 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003466 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003467 if (historyName == null) {
3468 historyName = name;
3469 }
3470 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3471 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003472 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003473 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003474 }
3475 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003476 if (mWakeLockNesting == 0) {
3477 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3478 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3479 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003480 mInitialAcquireWakeName = null;
3481 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003482 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003483 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003484 }
3485 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003486 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3487 if (DEBUG_ENERGY_CPU) {
3488 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3489 }
3490 requestWakelockCpuUpdate();
3491 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003492 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003493 }
3494 }
3495
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003496 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3497 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003498 final long elapsedRealtime = mClocks.elapsedRealtime();
3499 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003500 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003501 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003502 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003503 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003504 }
3505 }
3506
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003507 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3508 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003509 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003510 final long elapsedRealtime = mClocks.elapsedRealtime();
3511 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003512 // For correct semantics, we start the need worksources first, so that we won't
3513 // make inappropriate history items as if all wake locks went away and new ones
3514 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003515 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003516 for (int i=0; i<NN; i++) {
3517 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003518 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003519 }
3520 final int NO = ws.size();
3521 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003522 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003523 }
3524 }
3525
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003526 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3527 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003528 final long elapsedRealtime = mClocks.elapsedRealtime();
3529 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003530 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003531 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003532 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003533 }
3534 }
3535
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003536 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3537 uid = mapUid(uid);
3538 final long elapsedRealtime = mClocks.elapsedRealtime();
3539 final long uptime = mClocks.uptimeMillis();
3540 if (historyName == null) {
3541 historyName = name;
3542 }
3543 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3544 0)) {
3545 return;
3546 }
3547 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3548 historyName, uid);
3549 }
3550
3551 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3552 uid = mapUid(uid);
3553 final long elapsedRealtime = mClocks.elapsedRealtime();
3554 final long uptime = mClocks.uptimeMillis();
3555 if (historyName == null) {
3556 historyName = name;
3557 }
3558 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3559 0)) {
3560 return;
3561 }
3562 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3563 historyName, uid);
3564 }
3565
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003566 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3567 if (mLastWakeupReason != null) {
3568 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003569 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003570 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003571 mLastWakeupReason = null;
3572 }
3573 }
3574
3575 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003576 final long elapsedRealtime = mClocks.elapsedRealtime();
3577 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003578 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003579 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003580 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003581 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3582 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003583 mHistoryCur.wakeReasonTag.uid = 0;
3584 mLastWakeupReason = reason;
3585 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003586 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003587 }
3588
Adam Lesinski72478f02015-06-17 15:39:43 -07003589 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003590 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003591 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003592 }
3593
Adam Lesinski72478f02015-06-17 15:39:43 -07003594 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3595 int statSystemTime, int statIOWaitTime, int statIrqTime,
3596 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003597 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3598 + " user=" + statUserTime + " sys=" + statSystemTime
3599 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3600 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3601 mCurStepCpuUserTime += totalUTime;
3602 mCurStepCpuSystemTime += totalSTime;
3603 mCurStepStatUserTime += statUserTime;
3604 mCurStepStatSystemTime += statSystemTime;
3605 mCurStepStatIOWaitTime += statIOWaitTime;
3606 mCurStepStatIrqTime += statIrqTime;
3607 mCurStepStatSoftIrqTime += statSoftIrqTime;
3608 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003609 }
3610
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003611 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003612 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003613 Uid u = mUidStats.get(uid);
3614 if (u != null) {
3615 u.mPids.remove(pid);
3616 }
3617 }
3618
3619 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003620 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003621 Uid u = mUidStats.get(uid);
3622 if (u != null) {
3623 Uid.Pid p = u.mPids.get(pid);
3624 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003625 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003626 }
3627 }
3628 return 0;
3629 }
3630
3631 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003632 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003633 Uid u = mUidStats.get(uid);
3634 if (u != null) {
3635 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3636 }
3637 }
3638
Dianne Hackborn287952c2010-09-22 22:34:31 -07003639 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003640 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003641 Uid u = mUidStats.get(uid);
3642 if (u != null) {
3643 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3644 }
3645 }
3646
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003647 int mSensorNesting;
3648
3649 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003650 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003651 final long elapsedRealtime = mClocks.elapsedRealtime();
3652 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003653 if (mSensorNesting == 0) {
3654 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3655 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3656 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003657 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003658 }
3659 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003660 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003661 }
3662
3663 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003664 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003665 final long elapsedRealtime = mClocks.elapsedRealtime();
3666 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003667 mSensorNesting--;
3668 if (mSensorNesting == 0) {
3669 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3670 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3671 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003672 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003673 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003674 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003675 }
3676
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003677 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003678
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003679 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003680 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003681 final long elapsedRealtime = mClocks.elapsedRealtime();
3682 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003683 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003684 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003685 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3686 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003687 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003688 }
3689 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003690 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003692
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003693 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003694 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003695 final long elapsedRealtime = mClocks.elapsedRealtime();
3696 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003697 mGpsNesting--;
3698 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003699 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003700 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3701 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003702 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003703 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003704 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003706
Jeff Browne95c3cd2014-05-02 16:59:26 -07003707 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003708 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08003709
3710 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
3711 // original 4 are mapped to one of the originals.
3712 if (state > MAX_TRACKED_SCREEN_STATE) {
3713 switch (state) {
3714 case Display.STATE_VR:
3715 state = Display.STATE_ON;
3716 break;
3717 default:
3718 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
3719 break;
3720 }
3721 }
3722
Jeff Browne95c3cd2014-05-02 16:59:26 -07003723 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003724 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003725 final int oldState = mScreenState;
3726 mScreenState = state;
3727 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3728 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003729
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003730 if (state != Display.STATE_UNKNOWN) {
3731 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08003732 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
3733 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3734 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003735 } else {
3736 Slog.wtf(TAG, "Unexpected screen state: " + state);
3737 }
3738 }
3739
Jeff Browne95c3cd2014-05-02 16:59:26 -07003740 if (state == Display.STATE_ON) {
3741 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003742 final long elapsedRealtime = mClocks.elapsedRealtime();
3743 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003744 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3745 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3746 + Integer.toHexString(mHistoryCur.states));
3747 addHistoryRecordLocked(elapsedRealtime, uptime);
3748 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3749 if (mScreenBrightnessBin >= 0) {
3750 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3751 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003752
Jeff Browne95c3cd2014-05-02 16:59:26 -07003753 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003754 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003755
Jeff Browne95c3cd2014-05-02 16:59:26 -07003756 // Fake a wake lock, so we consider the device waked as long
3757 // as the screen is on.
3758 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3759 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003760
Jeff Browne95c3cd2014-05-02 16:59:26 -07003761 // Update discharge amounts.
3762 if (mOnBatteryInternal) {
3763 updateDischargeScreenLevelsLocked(false, true);
3764 }
3765 } else if (oldState == Display.STATE_ON) {
3766 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003767 final long elapsedRealtime = mClocks.elapsedRealtime();
3768 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003769 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3770 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3771 + Integer.toHexString(mHistoryCur.states));
3772 addHistoryRecordLocked(elapsedRealtime, uptime);
3773 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3774 if (mScreenBrightnessBin >= 0) {
3775 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3776 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003777
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003778 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003779 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003780
Jeff Browne95c3cd2014-05-02 16:59:26 -07003781 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003782 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003783
Jeff Browne95c3cd2014-05-02 16:59:26 -07003784 // Update discharge amounts.
3785 if (mOnBatteryInternal) {
3786 updateDischargeScreenLevelsLocked(true, false);
3787 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003788 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003789 }
3790 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003791
Dianne Hackborn617f8772009-03-31 15:04:46 -07003792 public void noteScreenBrightnessLocked(int brightness) {
3793 // Bin the brightness.
3794 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3795 if (bin < 0) bin = 0;
3796 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3797 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003798 final long elapsedRealtime = mClocks.elapsedRealtime();
3799 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003800 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3801 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003802 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3803 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003804 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003805 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003806 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003807 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003808 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003809 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003810 }
3811 mScreenBrightnessBin = bin;
3812 }
3813 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003814
Dianne Hackborn617f8772009-03-31 15:04:46 -07003815 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003816 if (mOnBatteryInternal) {
3817 uid = mapUid(uid);
3818 getUidStatsLocked(uid).noteUserActivityLocked(event);
3819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003821
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003822 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003823 final long elapsedRealtime = mClocks.elapsedRealtime();
3824 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003825 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3826 reason, reasonUid);
3827 }
3828
Jeff Browne95c3cd2014-05-02 16:59:26 -07003829 public void noteInteractiveLocked(boolean interactive) {
3830 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003831 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003832 mInteractive = interactive;
3833 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3834 if (interactive) {
3835 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3836 } else {
3837 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3838 }
3839 }
3840 }
3841
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003842 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003843 final long elapsedRealtime = mClocks.elapsedRealtime();
3844 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003845 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3846 extra, type);
3847 mNumConnectivityChange++;
3848 }
3849
Adam Lesinski5f056f62016-07-14 16:56:08 -07003850 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
3851 final long uptimeMillis, int uid) {
3852 uid = mapUid(uid);
3853 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
3854 uid);
3855 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
3856 }
3857
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -07003858 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003859 final long elapsedRealtime = mClocks.elapsedRealtime();
3860 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003861 if (mMobileRadioPowerState != powerState) {
3862 long realElapsedRealtimeMs;
3863 final boolean active =
3864 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3865 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3866 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07003867 if (uid > 0) {
3868 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
3869 }
3870
Adam Lesinski9acfd812016-04-19 18:29:50 -07003871 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003872 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3873 } else {
3874 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003875 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003876 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3877 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3878 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003879 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003880 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3881 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3882 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003883 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003884 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3885 }
3886 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3887 + Integer.toHexString(mHistoryCur.states));
3888 addHistoryRecordLocked(elapsedRealtime, uptime);
3889 mMobileRadioPowerState = powerState;
3890 if (active) {
3891 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3892 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3893 } else {
3894 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003895 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003896 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003897 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003898 }
3899 }
3900
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003901 public void notePowerSaveMode(boolean enabled) {
3902 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003903 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3904 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3905 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08003906 final long elapsedRealtime = mClocks.elapsedRealtime();
3907 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003908 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003909 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003910 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3911 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003912 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003913 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003914 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003915 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3916 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003917 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003918 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003919 }
3920 addHistoryRecordLocked(elapsedRealtime, uptime);
3921 }
3922 }
3923
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003924 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003925 final long elapsedRealtime = mClocks.elapsedRealtime();
3926 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003927 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003928 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003929 // We don't go out of general idling mode until explicitly taken out of
3930 // device idle through going active or significant motion.
3931 nowIdling = true;
3932 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003933 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
3934 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
3935 // We don't go out of general light idling mode until explicitly taken out of
3936 // device idle through going active or significant motion.
3937 nowLightIdling = true;
3938 }
3939 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
3940 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3941 activeReason, activeUid);
3942 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003943 if (mDeviceIdling != nowIdling) {
3944 mDeviceIdling = nowIdling;
3945 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3946 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3947 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003948 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003949 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3950 } else {
3951 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3952 }
3953 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003954 if (mDeviceLightIdling != nowLightIdling) {
3955 mDeviceLightIdling = nowLightIdling;
3956 if (nowLightIdling) {
3957 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003958 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003959 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003960 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003961 }
3962 if (mDeviceIdleMode != mode) {
3963 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
3964 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
3965 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
3966 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003967 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003968 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
3969 mLastIdleTimeStart = elapsedRealtime;
3970 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
3971 if (lastDuration > mLongestLightIdleTime) {
3972 mLongestLightIdleTime = lastDuration;
3973 }
3974 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003975 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003976 if (lastDuration > mLongestFullIdleTime) {
3977 mLongestFullIdleTime = lastDuration;
3978 }
3979 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3980 }
3981 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3982 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003983 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003984 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
3985 }
3986 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003987 }
3988 }
3989
3990 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08003991 final long elapsedRealtime = mClocks.elapsedRealtime();
3992 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003993 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3994 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003995 PackageChange pc = new PackageChange();
3996 pc.mPackageName = pkgName;
3997 pc.mUpdate = true;
3998 pc.mVersionCode = versionCode;
3999 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004000 }
4001
4002 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004003 final long elapsedRealtime = mClocks.elapsedRealtime();
4004 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004005 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4006 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004007 PackageChange pc = new PackageChange();
4008 pc.mPackageName = pkgName;
4009 pc.mUpdate = true;
4010 addPackageChange(pc);
4011 }
4012
4013 private void addPackageChange(PackageChange pc) {
4014 if (mDailyPackageChanges == null) {
4015 mDailyPackageChanges = new ArrayList<>();
4016 }
4017 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004018 }
4019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 public void notePhoneOnLocked() {
4021 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004022 final long elapsedRealtime = mClocks.elapsedRealtime();
4023 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004024 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004025 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4026 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004027 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004029 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 }
4031 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 public void notePhoneOffLocked() {
4034 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004035 final long elapsedRealtime = mClocks.elapsedRealtime();
4036 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004037 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004038 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4039 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004040 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004042 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 }
4044 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004045
Dianne Hackborn3251b902014-06-20 14:40:53 -07004046 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004047 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004048 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004049 if (i == except) {
4050 continue;
4051 }
4052 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004053 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004054 }
4055 }
4056 }
4057
Dianne Hackborne4a59512010-12-07 11:08:07 -08004058 private int fixPhoneServiceState(int state, int signalBin) {
4059 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4060 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4061 // to infer that we are scanning from other data.
4062 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004063 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004064 state = ServiceState.STATE_IN_SERVICE;
4065 }
4066 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004067
Dianne Hackborne4a59512010-12-07 11:08:07 -08004068 return state;
4069 }
4070
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004071 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004072 boolean scanning = false;
4073 boolean newHistory = false;
4074
4075 mPhoneServiceStateRaw = state;
4076 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004077 mPhoneSignalStrengthBinRaw = strengthBin;
4078
Joe Onoratoabded112016-02-08 16:49:39 -08004079 final long elapsedRealtime = mClocks.elapsedRealtime();
4080 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004081
4082 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4083 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4084 // to infer that we are scanning from other data.
4085 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004086 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004087 state = ServiceState.STATE_IN_SERVICE;
4088 }
4089 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004090
4091 // If the phone is powered off, stop all timers.
4092 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004093 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004094
Dianne Hackborne4a59512010-12-07 11:08:07 -08004095 // If we are in service, make sure the correct signal string timer is running.
4096 } else if (state == ServiceState.STATE_IN_SERVICE) {
4097 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004098
4099 // If we're out of service, we are in the lowest signal strength
4100 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004101 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004102 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004103 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004104 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004105 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004106 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004107 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4108 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004109 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004110 }
4111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004112
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004113 if (!scanning) {
4114 // If we are no longer scanning, then stop the scanning timer.
4115 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4116 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4117 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4118 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004119 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004120 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004121 }
4122 }
4123
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004124 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004125 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4126 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004127 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004128 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004129 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004130 mPhoneServiceState = state;
4131 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004132
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004133 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004134 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004135 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004136 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004137 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004138 if (strengthBin >= 0) {
4139 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004140 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004141 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004142 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4143 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004144 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004145 + Integer.toHexString(mHistoryCur.states));
4146 newHistory = true;
4147 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004148 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004149 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004150 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004151 }
4152
4153 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004154 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004155 }
4156 }
4157
4158 /**
4159 * Telephony stack updates the phone state.
4160 * @param state phone state from ServiceState.getState()
4161 */
4162 public void notePhoneStateLocked(int state, int simState) {
4163 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004164 }
4165
Wink Savillee9b06d72009-05-18 21:47:50 -07004166 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004167 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004168 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004169 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004170 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004171
Dianne Hackborn627bba72009-03-24 22:32:56 -07004172 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4173 int bin = DATA_CONNECTION_NONE;
4174 if (hasData) {
4175 switch (dataType) {
4176 case TelephonyManager.NETWORK_TYPE_EDGE:
4177 bin = DATA_CONNECTION_EDGE;
4178 break;
4179 case TelephonyManager.NETWORK_TYPE_GPRS:
4180 bin = DATA_CONNECTION_GPRS;
4181 break;
4182 case TelephonyManager.NETWORK_TYPE_UMTS:
4183 bin = DATA_CONNECTION_UMTS;
4184 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004185 case TelephonyManager.NETWORK_TYPE_CDMA:
4186 bin = DATA_CONNECTION_CDMA;
4187 break;
4188 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4189 bin = DATA_CONNECTION_EVDO_0;
4190 break;
4191 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4192 bin = DATA_CONNECTION_EVDO_A;
4193 break;
4194 case TelephonyManager.NETWORK_TYPE_1xRTT:
4195 bin = DATA_CONNECTION_1xRTT;
4196 break;
4197 case TelephonyManager.NETWORK_TYPE_HSDPA:
4198 bin = DATA_CONNECTION_HSDPA;
4199 break;
4200 case TelephonyManager.NETWORK_TYPE_HSUPA:
4201 bin = DATA_CONNECTION_HSUPA;
4202 break;
4203 case TelephonyManager.NETWORK_TYPE_HSPA:
4204 bin = DATA_CONNECTION_HSPA;
4205 break;
4206 case TelephonyManager.NETWORK_TYPE_IDEN:
4207 bin = DATA_CONNECTION_IDEN;
4208 break;
4209 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4210 bin = DATA_CONNECTION_EVDO_B;
4211 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004212 case TelephonyManager.NETWORK_TYPE_LTE:
4213 bin = DATA_CONNECTION_LTE;
4214 break;
4215 case TelephonyManager.NETWORK_TYPE_EHRPD:
4216 bin = DATA_CONNECTION_EHRPD;
4217 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004218 case TelephonyManager.NETWORK_TYPE_HSPAP:
4219 bin = DATA_CONNECTION_HSPAP;
4220 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004221 default:
4222 bin = DATA_CONNECTION_OTHER;
4223 break;
4224 }
4225 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004226 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004227 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004228 final long elapsedRealtime = mClocks.elapsedRealtime();
4229 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004230 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4231 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004232 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4233 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004234 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004235 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004236 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004237 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004238 }
4239 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004240 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004241 }
4242 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004243
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004244 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004245 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004246 final long elapsedRealtime = mClocks.elapsedRealtime();
4247 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004248 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004249 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4250 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004251 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004252 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004253 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004254 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004255 }
4256 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004257
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004258 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004259 final long elapsedRealtime = mClocks.elapsedRealtime();
4260 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004261 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004262 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004263 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4264 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004265 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004266 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004267 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004268 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004269 }
4270 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004271
4272 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004273 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004274 final long elapsedRealtime = mClocks.elapsedRealtime();
4275 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004276 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004277 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004278 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4279 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004280 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004281 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004282 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004283 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004284 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004285 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004286
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004287 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004288 if (mAudioOnNesting == 0) {
4289 return;
4290 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004291 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004292 final long elapsedRealtime = mClocks.elapsedRealtime();
4293 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004294 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004295 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004296 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4297 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004298 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004299 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004300 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004301 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004302 }
4303
4304 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004305 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004306 final long elapsedRealtime = mClocks.elapsedRealtime();
4307 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004308 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004309 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004310 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4311 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004312 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004313 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004314 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004315 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004316 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004317 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004318
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004319 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004320 if (mVideoOnNesting == 0) {
4321 return;
4322 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004323 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004324 final long elapsedRealtime = mClocks.elapsedRealtime();
4325 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004326 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004327 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004328 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4329 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004330 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004331 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004332 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004333 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004334 }
4335
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004336 public void noteResetAudioLocked() {
4337 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004338 final long elapsedRealtime = mClocks.elapsedRealtime();
4339 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004340 mAudioOnNesting = 0;
4341 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4342 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4343 + Integer.toHexString(mHistoryCur.states));
4344 addHistoryRecordLocked(elapsedRealtime, uptime);
4345 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4346 for (int i=0; i<mUidStats.size(); i++) {
4347 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4348 uid.noteResetAudioLocked(elapsedRealtime);
4349 }
4350 }
4351 }
4352
4353 public void noteResetVideoLocked() {
4354 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004355 final long elapsedRealtime = mClocks.elapsedRealtime();
4356 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004357 mAudioOnNesting = 0;
4358 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4359 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4360 + Integer.toHexString(mHistoryCur.states));
4361 addHistoryRecordLocked(elapsedRealtime, uptime);
4362 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4363 for (int i=0; i<mUidStats.size(); i++) {
4364 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4365 uid.noteResetVideoLocked(elapsedRealtime);
4366 }
4367 }
4368 }
4369
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004370 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004371 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004372 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004373 }
4374
4375 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004376 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004377 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004378 }
4379
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004380 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004381 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004382 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4383 }
4384
4385 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004386 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004387 getUidStatsLocked(uid).noteVibratorOffLocked();
4388 }
4389
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004390 public void noteFlashlightOnLocked(int uid) {
4391 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004392 final long elapsedRealtime = mClocks.elapsedRealtime();
4393 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004394 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004395 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4396 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004397 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004398 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004399 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4400 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004401 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4402 }
4403
4404 public void noteFlashlightOffLocked(int uid) {
4405 if (mFlashlightOnNesting == 0) {
4406 return;
4407 }
4408 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004409 final long elapsedRealtime = mClocks.elapsedRealtime();
4410 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004411 if (--mFlashlightOnNesting == 0) {
4412 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4413 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4414 + Integer.toHexString(mHistoryCur.states2));
4415 addHistoryRecordLocked(elapsedRealtime, uptime);
4416 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4417 }
4418 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4419 }
4420
4421 public void noteCameraOnLocked(int uid) {
4422 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004423 final long elapsedRealtime = mClocks.elapsedRealtime();
4424 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004425 if (mCameraOnNesting++ == 0) {
4426 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4427 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4428 + Integer.toHexString(mHistoryCur.states2));
4429 addHistoryRecordLocked(elapsedRealtime, uptime);
4430 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4431 }
4432 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4433 }
4434
4435 public void noteCameraOffLocked(int uid) {
4436 if (mCameraOnNesting == 0) {
4437 return;
4438 }
4439 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004440 final long elapsedRealtime = mClocks.elapsedRealtime();
4441 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004442 if (--mCameraOnNesting == 0) {
4443 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4444 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4445 + Integer.toHexString(mHistoryCur.states2));
4446 addHistoryRecordLocked(elapsedRealtime, uptime);
4447 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4448 }
4449 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4450 }
4451
4452 public void noteResetCameraLocked() {
4453 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004454 final long elapsedRealtime = mClocks.elapsedRealtime();
4455 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004456 mCameraOnNesting = 0;
4457 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4458 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4459 + Integer.toHexString(mHistoryCur.states2));
4460 addHistoryRecordLocked(elapsedRealtime, uptime);
4461 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4462 for (int i=0; i<mUidStats.size(); i++) {
4463 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4464 uid.noteResetCameraLocked(elapsedRealtime);
4465 }
4466 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004467 }
4468
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004469 public void noteResetFlashlightLocked() {
4470 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004471 final long elapsedRealtime = mClocks.elapsedRealtime();
4472 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004473 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004474 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4475 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004476 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004477 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004478 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4479 for (int i=0; i<mUidStats.size(); i++) {
4480 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4481 uid.noteResetFlashlightLocked(elapsedRealtime);
4482 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004483 }
4484 }
4485
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004486 private void noteBluetoothScanStartedLocked(int uid) {
4487 uid = mapUid(uid);
4488 final long elapsedRealtime = SystemClock.elapsedRealtime();
4489 final long uptime = SystemClock.uptimeMillis();
4490 if (mBluetoothScanNesting == 0) {
4491 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4492 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4493 + Integer.toHexString(mHistoryCur.states2));
4494 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004495 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004496 }
4497 mBluetoothScanNesting++;
4498 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4499 }
4500
4501 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4502 final int N = ws.size();
4503 for (int i = 0; i < N; i++) {
4504 noteBluetoothScanStartedLocked(ws.get(i));
4505 }
4506 }
4507
4508 private void noteBluetoothScanStoppedLocked(int uid) {
4509 uid = mapUid(uid);
4510 final long elapsedRealtime = SystemClock.elapsedRealtime();
4511 final long uptime = SystemClock.uptimeMillis();
4512 mBluetoothScanNesting--;
4513 if (mBluetoothScanNesting == 0) {
4514 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4515 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4516 + Integer.toHexString(mHistoryCur.states2));
4517 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004518 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004519 }
4520 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4521 }
4522
4523 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4524 final int N = ws.size();
4525 for (int i = 0; i < N; i++) {
4526 noteBluetoothScanStoppedLocked(ws.get(i));
4527 }
4528 }
4529
4530 public void noteResetBluetoothScanLocked() {
4531 if (mBluetoothScanNesting > 0) {
4532 final long elapsedRealtime = SystemClock.elapsedRealtime();
4533 final long uptime = SystemClock.uptimeMillis();
4534 mBluetoothScanNesting = 0;
4535 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4536 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4537 + Integer.toHexString(mHistoryCur.states2));
4538 addHistoryRecordLocked(elapsedRealtime, uptime);
4539 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4540 for (int i=0; i<mUidStats.size(); i++) {
4541 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4542 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4543 }
4544 }
4545 }
4546
Adam Lesinski5f056f62016-07-14 16:56:08 -07004547 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4548 final long uptimeMillis, int uid) {
4549 uid = mapUid(uid);
4550 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4551 uid);
4552 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4553 }
4554
4555 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004556 final long elapsedRealtime = mClocks.elapsedRealtime();
4557 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004558 if (mWifiRadioPowerState != powerState) {
4559 final boolean active =
4560 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4561 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4562 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004563 if (uid > 0) {
4564 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4565 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004566 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4567 } else {
4568 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4569 }
4570 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4571 + Integer.toHexString(mHistoryCur.states));
4572 addHistoryRecordLocked(elapsedRealtime, uptime);
4573 mWifiRadioPowerState = powerState;
4574 }
4575 }
4576
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004577 public void noteWifiRunningLocked(WorkSource ws) {
4578 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004579 final long elapsedRealtime = mClocks.elapsedRealtime();
4580 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004581 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004582 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4583 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004584 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004585 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004586 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004587 int N = ws.size();
4588 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004589 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004590 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004591 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004592 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004593 } else {
4594 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004595 }
4596 }
4597
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004598 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4599 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004600 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004601 int N = oldWs.size();
4602 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004603 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004604 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004605 }
4606 N = newWs.size();
4607 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004608 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004609 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004610 }
4611 } else {
4612 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4613 }
4614 }
4615
4616 public void noteWifiStoppedLocked(WorkSource ws) {
4617 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004618 final long elapsedRealtime = mClocks.elapsedRealtime();
4619 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004620 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004621 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4622 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004623 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004624 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004625 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004626 int N = ws.size();
4627 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004628 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004629 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004630 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004631 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004632 } else {
4633 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004634 }
4635 }
4636
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004637 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4638 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4639 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004640 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004641 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004642 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004643 }
4644 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004645 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004646 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004647 }
4648 }
4649
Dianne Hackborn3251b902014-06-20 14:40:53 -07004650 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4651 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4652 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004653 final long elapsedRealtime = mClocks.elapsedRealtime();
4654 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004655 if (mWifiSupplState >= 0) {
4656 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4657 }
4658 mWifiSupplState = supplState;
4659 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4660 mHistoryCur.states2 =
4661 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4662 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4663 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4664 + Integer.toHexString(mHistoryCur.states2));
4665 addHistoryRecordLocked(elapsedRealtime, uptime);
4666 }
4667 }
4668
4669 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004670 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004671 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4672 if (i == except) {
4673 continue;
4674 }
4675 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4676 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4677 }
4678 }
4679 }
4680
4681 public void noteWifiRssiChangedLocked(int newRssi) {
4682 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4683 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4684 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004685 final long elapsedRealtime = mClocks.elapsedRealtime();
4686 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004687 if (mWifiSignalStrengthBin >= 0) {
4688 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4689 elapsedRealtime);
4690 }
4691 if (strengthBin >= 0) {
4692 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4693 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4694 }
4695 mHistoryCur.states2 =
4696 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4697 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4698 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4699 + Integer.toHexString(mHistoryCur.states2));
4700 addHistoryRecordLocked(elapsedRealtime, uptime);
4701 } else {
4702 stopAllWifiSignalStrengthTimersLocked(-1);
4703 }
4704 mWifiSignalStrengthBin = strengthBin;
4705 }
4706 }
4707
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004708 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004709
The Android Open Source Project10592532009-03-18 17:39:46 -07004710 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004711 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004712 final long elapsedRealtime = mClocks.elapsedRealtime();
4713 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004714 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004715 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004716 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4717 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004718 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004719 }
4720 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004721 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004722 }
4723
4724 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004725 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004726 final long elapsedRealtime = mClocks.elapsedRealtime();
4727 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004728 mWifiFullLockNesting--;
4729 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004730 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004731 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4732 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004733 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004734 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004735 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004736 }
4737
Nick Pelly6ccaa542012-06-15 15:22:47 -07004738 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004739
Nick Pelly6ccaa542012-06-15 15:22:47 -07004740 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004741 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004742 final long elapsedRealtime = mClocks.elapsedRealtime();
4743 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004744 if (mWifiScanNesting == 0) {
4745 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4746 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004747 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004748 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004749 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004750 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004751 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004752 }
4753
Nick Pelly6ccaa542012-06-15 15:22:47 -07004754 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004755 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004756 final long elapsedRealtime = mClocks.elapsedRealtime();
4757 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004758 mWifiScanNesting--;
4759 if (mWifiScanNesting == 0) {
4760 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4761 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004762 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004763 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004764 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004765 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004766 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004767
Robert Greenwalta029ea12013-09-25 16:38:12 -07004768 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004769 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004770 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004771 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004772 }
4773
4774 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004775 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004776 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004777 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004778 }
4779
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004780 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004781
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004782 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004783 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004784 final long elapsedRealtime = mClocks.elapsedRealtime();
4785 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004786 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004787 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004788 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4789 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004790 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004791 }
4792 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004793 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004794 }
4795
4796 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004797 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004798 final long elapsedRealtime = mClocks.elapsedRealtime();
4799 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004800 mWifiMulticastNesting--;
4801 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004802 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004803 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4804 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004805 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004806 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004807 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004808 }
4809
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004810 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4811 int N = ws.size();
4812 for (int i=0; i<N; i++) {
4813 noteFullWifiLockAcquiredLocked(ws.get(i));
4814 }
4815 }
4816
4817 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4818 int N = ws.size();
4819 for (int i=0; i<N; i++) {
4820 noteFullWifiLockReleasedLocked(ws.get(i));
4821 }
4822 }
4823
Nick Pelly6ccaa542012-06-15 15:22:47 -07004824 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004825 int N = ws.size();
4826 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004827 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004828 }
4829 }
4830
Nick Pelly6ccaa542012-06-15 15:22:47 -07004831 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004832 int N = ws.size();
4833 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004834 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004835 }
4836 }
4837
Robert Greenwalta029ea12013-09-25 16:38:12 -07004838 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4839 int N = ws.size();
4840 for (int i=0; i<N; i++) {
4841 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4842 }
4843 }
4844
4845 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4846 int N = ws.size();
4847 for (int i=0; i<N; i++) {
4848 noteWifiBatchedScanStoppedLocked(ws.get(i));
4849 }
4850 }
4851
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004852 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4853 int N = ws.size();
4854 for (int i=0; i<N; i++) {
4855 noteWifiMulticastEnabledLocked(ws.get(i));
4856 }
4857 }
4858
4859 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4860 int N = ws.size();
4861 for (int i=0; i<N; i++) {
4862 noteWifiMulticastDisabledLocked(ws.get(i));
4863 }
4864 }
4865
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004866 private static String[] includeInStringArray(String[] array, String str) {
4867 if (ArrayUtils.indexOf(array, str) >= 0) {
4868 return array;
4869 }
4870 String[] newArray = new String[array.length+1];
4871 System.arraycopy(array, 0, newArray, 0, array.length);
4872 newArray[array.length] = str;
4873 return newArray;
4874 }
4875
4876 private static String[] excludeFromStringArray(String[] array, String str) {
4877 int index = ArrayUtils.indexOf(array, str);
4878 if (index >= 0) {
4879 String[] newArray = new String[array.length-1];
4880 if (index > 0) {
4881 System.arraycopy(array, 0, newArray, 0, index);
4882 }
4883 if (index < array.length-1) {
4884 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4885 }
4886 return newArray;
4887 }
4888 return array;
4889 }
4890
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004891 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004892 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004893 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004894 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4895 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004896 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004897 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4898 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004899 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004900 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004901 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4902 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004903 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004904 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4905 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004906 }
4907 }
4908
4909 public void noteNetworkStatsEnabledLocked() {
4910 // During device boot, qtaguid isn't enabled until after the inital
4911 // loading of battery stats. Now that they're enabled, take our initial
4912 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08004913 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004914 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004915 }
4916
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004917 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4918 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004920
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004921 @Override public int getScreenOnCount(int which) {
4922 return mScreenOnTimer.getCountLocked(which);
4923 }
4924
Dianne Hackborn617f8772009-03-31 15:04:46 -07004925 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004926 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004927 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004928 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004929 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004930
Jeff Browne95c3cd2014-05-02 16:59:26 -07004931 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4932 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004933 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004934
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004935 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4936 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004937 }
4938
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004939 @Override public int getPowerSaveModeEnabledCount(int which) {
4940 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4941 }
4942
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004943 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
4944 int which) {
4945 switch (mode) {
4946 case DEVICE_IDLE_MODE_LIGHT:
4947 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004948 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004949 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4950 }
4951 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004952 }
4953
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004954 @Override public int getDeviceIdleModeCount(int mode, int which) {
4955 switch (mode) {
4956 case DEVICE_IDLE_MODE_LIGHT:
4957 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004958 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004959 return mDeviceIdleModeFullTimer.getCountLocked(which);
4960 }
4961 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004962 }
4963
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004964 @Override public long getLongestDeviceIdleModeTime(int mode) {
4965 switch (mode) {
4966 case DEVICE_IDLE_MODE_LIGHT:
4967 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004968 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004969 return mLongestFullIdleTime;
4970 }
4971 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004972 }
4973
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004974 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
4975 switch (mode) {
4976 case DEVICE_IDLE_MODE_LIGHT:
4977 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004978 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004979 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4980 }
4981 return 0;
4982 }
4983
4984 @Override public int getDeviceIdlingCount(int mode, int which) {
4985 switch (mode) {
4986 case DEVICE_IDLE_MODE_LIGHT:
4987 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004988 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004989 return mDeviceIdlingTimer.getCountLocked(which);
4990 }
4991 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004992 }
4993
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004994 @Override public int getNumConnectivityChange(int which) {
4995 int val = mNumConnectivityChange;
4996 if (which == STATS_CURRENT) {
4997 val -= mLoadedNumConnectivityChange;
4998 } else if (which == STATS_SINCE_UNPLUGGED) {
4999 val -= mUnpluggedNumConnectivityChange;
5000 }
5001 return val;
5002 }
5003
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005004 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5005 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005007
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005008 @Override public int getPhoneOnCount(int which) {
5009 return mPhoneOnTimer.getCountLocked(which);
5010 }
5011
Dianne Hackborn627bba72009-03-24 22:32:56 -07005012 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005013 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005014 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005015 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005016 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005017
5018 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005019 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005020 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005021 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005022 }
5023
Catherine Liufb900812012-07-17 14:12:56 -05005024 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5025 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005026 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005027
Dianne Hackborn627bba72009-03-24 22:32:56 -07005028 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005029 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005030 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005031 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005032 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005033
Dianne Hackborn617f8772009-03-31 15:04:46 -07005034 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005035 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005036 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005037
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005038 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5039 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005040 }
5041
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005042 @Override public int getMobileRadioActiveCount(int which) {
5043 return mMobileRadioActiveTimer.getCountLocked(which);
5044 }
5045
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005046 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5047 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5048 }
5049
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005050 @Override public long getMobileRadioActiveUnknownTime(int which) {
5051 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5052 }
5053
5054 @Override public int getMobileRadioActiveUnknownCount(int which) {
5055 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5056 }
5057
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005058 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5059 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005060 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005061
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005062 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5063 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005064 }
5065
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005066 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005067 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005068 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005069 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005070 }
5071
5072 @Override public int getWifiStateCount(int wifiState, int which) {
5073 return mWifiStateTimer[wifiState].getCountLocked(which);
5074 }
5075
Dianne Hackborn3251b902014-06-20 14:40:53 -07005076 @Override public long getWifiSupplStateTime(int state,
5077 long elapsedRealtimeUs, int which) {
5078 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5079 elapsedRealtimeUs, which);
5080 }
5081
5082 @Override public int getWifiSupplStateCount(int state, int which) {
5083 return mWifiSupplStateTimer[state].getCountLocked(which);
5084 }
5085
5086 @Override public long getWifiSignalStrengthTime(int strengthBin,
5087 long elapsedRealtimeUs, int which) {
5088 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5089 elapsedRealtimeUs, which);
5090 }
5091
5092 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5093 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5094 }
5095
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005096 @Override
5097 public ControllerActivityCounter getBluetoothControllerActivity() {
5098 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005099 }
5100
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005101 @Override
5102 public ControllerActivityCounter getWifiControllerActivity() {
5103 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005104 }
5105
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005106 @Override
5107 public ControllerActivityCounter getModemControllerActivity() {
5108 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005109 }
5110
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005111 @Override
5112 public boolean hasBluetoothActivityReporting() {
5113 return mHasBluetoothReporting;
5114 }
5115
5116 @Override
5117 public boolean hasWifiActivityReporting() {
5118 return mHasWifiReporting;
5119 }
5120
5121 @Override
5122 public boolean hasModemActivityReporting() {
5123 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005124 }
5125
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005126 @Override
5127 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005128 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5129 }
5130
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005131 @Override
5132 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005133 return mFlashlightOnTimer.getCountLocked(which);
5134 }
5135
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005136 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005137 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5138 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5139 }
5140
5141 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005142 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5143 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5144 }
5145
5146 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005147 public long getNetworkActivityBytes(int type, int which) {
5148 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5149 return mNetworkByteActivityCounters[type].getCountLocked(which);
5150 } else {
5151 return 0;
5152 }
5153 }
5154
5155 @Override
5156 public long getNetworkActivityPackets(int type, int which) {
5157 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5158 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005159 } else {
5160 return 0;
5161 }
5162 }
5163
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005164 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005165 final long currentTime = System.currentTimeMillis();
5166 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005167 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5168 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005169 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005170 return mStartClockTime;
5171 }
5172
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005173 @Override public String getStartPlatformVersion() {
5174 return mStartPlatformVersion;
5175 }
5176
5177 @Override public String getEndPlatformVersion() {
5178 return mEndPlatformVersion;
5179 }
5180
5181 @Override public int getParcelVersion() {
5182 return VERSION;
5183 }
5184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 @Override public boolean getIsOnBattery() {
5186 return mOnBattery;
5187 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5190 return mUidStats;
5191 }
5192
Adam Lesinski5f056f62016-07-14 16:56:08 -07005193 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5194 if (timer != null) {
5195 timer.detach();
5196 }
5197 }
5198
5199 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5200 boolean detachIfReset) {
5201 if (timer != null) {
5202 return timer.reset(detachIfReset);
5203 }
5204 return true;
5205 }
5206
5207 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5208 if (counter != null) {
5209 counter.detach();
5210 }
5211 }
5212
5213 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5214 boolean detachIfReset) {
5215 if (counter != null) {
5216 counter.reset(detachIfReset);
5217 }
5218 }
5219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 /**
5221 * The statistics associated with a particular uid.
5222 */
Joe Onoratoabded112016-02-08 16:49:39 -08005223 public static class Uid extends BatteryStats.Uid {
5224 /**
5225 * BatteryStatsImpl that we are associated with.
5226 */
5227 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005230
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005231 boolean mWifiRunning;
5232 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005233
The Android Open Source Project10592532009-03-18 17:39:46 -07005234 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005235 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005236
Nick Pelly6ccaa542012-06-15 15:22:47 -07005237 boolean mWifiScanStarted;
5238 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005239
Dianne Hackborn61659e52014-07-09 16:13:01 -07005240 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005241 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5242 StopwatchTimer[] mWifiBatchedScanTimer;
5243
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005244 boolean mWifiMulticastEnabled;
5245 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005246
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005247 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005248 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005249 StopwatchTimer mFlashlightTurnedOnTimer;
5250 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005251 StopwatchTimer mForegroundActivityTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005252 StopwatchTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005253
Dianne Hackborna8d10942015-11-19 17:55:19 -08005254 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005255 StopwatchTimer[] mProcessStateTimer;
5256
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005257 BatchTimer mVibratorOnTimer;
5258
Dianne Hackborn617f8772009-03-31 15:04:46 -07005259 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005260
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005261 LongSamplingCounter[] mNetworkByteActivityCounters;
5262 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005263 LongSamplingCounter mMobileRadioActiveTime;
5264 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005267 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5268 */
5269 private LongSamplingCounter mMobileRadioApWakeupCount;
5270
5271 /**
5272 * How many times this UID woke up the Application Processor due to a Wifi packet.
5273 */
5274 private LongSamplingCounter mWifiRadioApWakeupCount;
5275
5276 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005277 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005278 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005279 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005280 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005281
5282 /**
5283 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005284 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005285 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005286 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5287
5288 /**
5289 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5290 * Can be null if the UID has had no such activity.
5291 */
5292 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005293
5294 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005295 * The CPU times we had at the last history details update.
5296 */
5297 long mLastStepUserTime;
5298 long mLastStepSystemTime;
5299 long mCurStepUserTime;
5300 long mCurStepSystemTime;
5301
Joe Onoratoabded112016-02-08 16:49:39 -08005302 LongSamplingCounter mUserCpuTime;
5303 LongSamplingCounter mSystemCpuTime;
5304 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07005305 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005306
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 * The statistics we have collected for this uid's wake locks.
5309 */
Joe Onoratoabded112016-02-08 16:49:39 -08005310 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311
5312 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005313 * The statistics we have collected for this uid's syncs.
5314 */
Joe Onoratoabded112016-02-08 16:49:39 -08005315 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005316
5317 /**
5318 * The statistics we have collected for this uid's jobs.
5319 */
Joe Onoratoabded112016-02-08 16:49:39 -08005320 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005321
5322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 * The statistics we have collected for this uid's sensor activations.
5324 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005325 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326
5327 /**
5328 * The statistics we have collected for this uid's processes.
5329 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005330 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331
5332 /**
5333 * The statistics we have collected for this uid's processes.
5334 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005335 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005336
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005337 /**
5338 * The transient wake stats we have collected for this uid's pids.
5339 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005340 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005341
Joe Onoratoabded112016-02-08 16:49:39 -08005342 public Uid(BatteryStatsImpl bsi, int uid) {
5343 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005345
5346 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5347 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5348 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5349
Dianne Hackborn657153b2016-07-29 14:54:14 -07005350 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005351 @Override public Wakelock instantiateObject() {
5352 return new Wakelock(mBsi, Uid.this);
5353 }
5354 };
Dianne Hackborn657153b2016-07-29 14:54:14 -07005355 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005356 @Override public StopwatchTimer instantiateObject() {
5357 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
5358 mBsi.mOnBatteryTimeBase);
5359 }
5360 };
Dianne Hackborn657153b2016-07-29 14:54:14 -07005361 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005362 @Override public StopwatchTimer instantiateObject() {
5363 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5364 mBsi.mOnBatteryTimeBase);
5365 }
5366 };
5367
5368 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5369 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5370 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5371 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
5372 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_SCAN,
5373 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005374 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005375 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5376 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005377 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 }
5379
5380 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005381 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005382 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 }
5384
5385 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005386 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005387 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005388 }
5389
5390 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005391 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005392 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005393 }
5394
5395 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005396 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005397 return mSensorStats;
5398 }
5399
5400 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005401 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 return mProcessStats;
5403 }
5404
5405 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005406 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 return mPackageStats;
5408 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005409
5410 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005411 public int getUid() {
5412 return mUid;
5413 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005414
5415 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005416 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005417 if (!mWifiRunning) {
5418 mWifiRunning = true;
5419 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005420 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5421 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005422 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005423 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005424 }
5425 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005426
Dianne Hackborn617f8772009-03-31 15:04:46 -07005427 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005428 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005429 if (mWifiRunning) {
5430 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005431 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005432 }
5433 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005434
Dianne Hackborn617f8772009-03-31 15:04:46 -07005435 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005436 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005437 if (!mFullWifiLockOut) {
5438 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005439 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005440 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5441 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005442 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005443 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005444 }
5445 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005446
The Android Open Source Project10592532009-03-18 17:39:46 -07005447 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005448 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005449 if (mFullWifiLockOut) {
5450 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005451 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005452 }
5453 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005454
The Android Open Source Project10592532009-03-18 17:39:46 -07005455 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005456 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005457 if (!mWifiScanStarted) {
5458 mWifiScanStarted = true;
5459 if (mWifiScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005460 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5461 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005462 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005463 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005464 }
5465 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005466
The Android Open Source Project10592532009-03-18 17:39:46 -07005467 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005468 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005469 if (mWifiScanStarted) {
5470 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005471 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005472 }
5473 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005474
5475 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005476 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005477 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005478 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005479 csph = csph >> 3;
5480 bin++;
5481 }
5482
5483 if (mWifiBatchedScanBinStarted == bin) return;
5484
5485 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5486 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005487 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005488 }
5489 mWifiBatchedScanBinStarted = bin;
5490 if (mWifiBatchedScanTimer[bin] == null) {
5491 makeWifiBatchedScanBin(bin, null);
5492 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005493 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005494 }
5495
5496 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005497 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005498 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5499 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005500 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005501 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5502 }
5503 }
5504
5505 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005506 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005507 if (!mWifiMulticastEnabled) {
5508 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005509 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005510 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5511 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005512 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005513 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005514 }
5515 }
5516
5517 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005518 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005519 if (mWifiMulticastEnabled) {
5520 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005521 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005522 }
5523 }
5524
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005525 @Override
5526 public ControllerActivityCounter getWifiControllerActivity() {
5527 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005528 }
5529
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005530 @Override
5531 public ControllerActivityCounter getBluetoothControllerActivity() {
5532 return mBluetoothControllerActivity;
5533 }
5534
5535 @Override
5536 public ControllerActivityCounter getModemControllerActivity() {
5537 return mModemControllerActivity;
5538 }
5539
5540 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5541 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005542 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005543 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005544 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005545 return mWifiControllerActivity;
5546 }
5547
5548 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5549 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005550 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005551 NUM_BT_TX_LEVELS);
5552 }
5553 return mBluetoothControllerActivity;
5554 }
5555
5556 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5557 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005558 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005559 ModemActivityInfo.TX_POWER_LEVELS);
5560 }
5561 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005562 }
5563
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005564 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5565 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005566 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5567 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005568 }
5569 return mAudioTurnedOnTimer;
5570 }
5571
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005572 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005573 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5574 }
5575
5576 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5577 if (mAudioTurnedOnTimer != null) {
5578 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005579 }
5580 }
5581
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005582 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5583 if (mAudioTurnedOnTimer != null) {
5584 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005585 }
5586 }
5587
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005588 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5589 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005590 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5591 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005592 }
5593 return mVideoTurnedOnTimer;
5594 }
5595
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005596 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005597 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5598 }
5599
5600 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5601 if (mVideoTurnedOnTimer != null) {
5602 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005603 }
5604 }
5605
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005606 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5607 if (mVideoTurnedOnTimer != null) {
5608 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005609 }
5610 }
5611
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005612 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5613 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005614 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5615 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005616 }
5617 return mFlashlightTurnedOnTimer;
5618 }
5619
5620 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5621 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5622 }
5623
5624 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5625 if (mFlashlightTurnedOnTimer != null) {
5626 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5627 }
5628 }
5629
5630 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5631 if (mFlashlightTurnedOnTimer != null) {
5632 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5633 }
5634 }
5635
5636 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5637 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005638 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5639 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005640 }
5641 return mCameraTurnedOnTimer;
5642 }
5643
5644 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5645 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5646 }
5647
5648 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5649 if (mCameraTurnedOnTimer != null) {
5650 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5651 }
5652 }
5653
5654 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5655 if (mCameraTurnedOnTimer != null) {
5656 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5657 }
5658 }
5659
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005660 public StopwatchTimer createForegroundActivityTimerLocked() {
5661 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005662 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5663 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005664 }
5665 return mForegroundActivityTimer;
5666 }
5667
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005668 public StopwatchTimer createBluetoothScanTimerLocked() {
5669 if (mBluetoothScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005670 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5671 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005672 }
5673 return mBluetoothScanTimer;
5674 }
5675
5676 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5677 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5678 }
5679
5680 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5681 if (mBluetoothScanTimer != null) {
5682 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5683 }
5684 }
5685
5686 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5687 if (mBluetoothScanTimer != null) {
5688 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5689 }
5690 }
5691
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005692 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005693 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005694 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005695 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005696 }
5697
5698 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005699 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005700 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005701 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005702 }
5703 }
5704
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005705 public BatchTimer createVibratorOnTimerLocked() {
5706 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005707 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5708 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005709 }
5710 return mVibratorOnTimer;
5711 }
5712
5713 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005714 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005715 }
5716
5717 public void noteVibratorOffLocked() {
5718 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005719 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005720 }
5721 }
5722
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005723 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005724 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005725 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005726 return 0;
5727 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005728 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005729 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005730
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005731 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005732 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005733 if (mFullWifiLockTimer == null) {
5734 return 0;
5735 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005736 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005737 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005738
5739 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005740 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005741 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005742 return 0;
5743 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005744 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005745 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005746
5747 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005748 public int getWifiScanCount(int which) {
5749 if (mWifiScanTimer == null) {
5750 return 0;
5751 }
5752 return mWifiScanTimer.getCountLocked(which);
5753 }
5754
5755 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005756 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005757 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5758 if (mWifiBatchedScanTimer[csphBin] == null) {
5759 return 0;
5760 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005761 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005762 }
5763
5764 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005765 public int getWifiBatchedScanCount(int csphBin, int which) {
5766 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5767 if (mWifiBatchedScanTimer[csphBin] == null) {
5768 return 0;
5769 }
5770 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5771 }
5772
5773 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005774 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005775 if (mWifiMulticastTimer == null) {
5776 return 0;
5777 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005778 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005779 }
5780
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005781 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005782 public Timer getAudioTurnedOnTimer() {
5783 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005784 }
5785
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005786 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005787 public Timer getVideoTurnedOnTimer() {
5788 return mVideoTurnedOnTimer;
5789 }
5790
5791 @Override
5792 public Timer getFlashlightTurnedOnTimer() {
5793 return mFlashlightTurnedOnTimer;
5794 }
5795
5796 @Override
5797 public Timer getCameraTurnedOnTimer() {
5798 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005799 }
5800
Dianne Hackborn617f8772009-03-31 15:04:46 -07005801 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005802 public Timer getForegroundActivityTimer() {
5803 return mForegroundActivityTimer;
5804 }
5805
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005806 @Override
5807 public Timer getBluetoothScanTimer() {
5808 return mBluetoothScanTimer;
5809 }
5810
Dianne Hackborn61659e52014-07-09 16:13:01 -07005811 void makeProcessState(int i, Parcel in) {
5812 if (i < 0 || i >= NUM_PROCESS_STATE) return;
5813
5814 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005815 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5816 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005817 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005818 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5819 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005820 }
5821 }
5822
5823 @Override
5824 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
5825 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
5826 if (mProcessStateTimer[state] == null) {
5827 return 0;
5828 }
5829 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
5830 }
5831
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005832 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08005833 public Timer getProcessStateTimer(int state) {
5834 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
5835 return mProcessStateTimer[state];
5836 }
5837
5838 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005839 public Timer getVibratorOnTimer() {
5840 return mVibratorOnTimer;
5841 }
5842
5843 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005844 public void noteUserActivityLocked(int type) {
5845 if (mUserActivityCounters == null) {
5846 initUserActivityLocked();
5847 }
Jeff Browndf693de2012-07-27 12:03:38 -07005848 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5849 mUserActivityCounters[type].stepAtomic();
5850 } else {
5851 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5852 new Throwable());
5853 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005854 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005855
Dianne Hackborn617f8772009-03-31 15:04:46 -07005856 @Override
5857 public boolean hasUserActivity() {
5858 return mUserActivityCounters != null;
5859 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005860
Dianne Hackborn617f8772009-03-31 15:04:46 -07005861 @Override
5862 public int getUserActivityCount(int type, int which) {
5863 if (mUserActivityCounters == null) {
5864 return 0;
5865 }
Evan Millarc64edde2009-04-18 12:26:32 -07005866 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005867 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005868
Robert Greenwalta029ea12013-09-25 16:38:12 -07005869 void makeWifiBatchedScanBin(int i, Parcel in) {
5870 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5871
Joe Onoratoabded112016-02-08 16:49:39 -08005872 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005873 if (collected == null) {
5874 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005875 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005876 }
5877 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005878 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5879 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005880 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005881 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5882 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005883 }
5884 }
5885
5886
Dianne Hackborn617f8772009-03-31 15:04:46 -07005887 void initUserActivityLocked() {
5888 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5889 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005890 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005891 }
5892 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005893
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005894 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5895 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005896 initNetworkActivityLocked();
5897 }
5898 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005899 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5900 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005901 } else {
5902 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5903 new Throwable());
5904 }
5905 }
5906
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005907 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5908 if (mNetworkByteActivityCounters == null) {
5909 initNetworkActivityLocked();
5910 }
5911 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5912 mMobileRadioActiveCount.addCountLocked(1);
5913 }
5914
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005915 @Override
5916 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005917 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005918 }
5919
5920 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005921 public long getNetworkActivityBytes(int type, int which) {
5922 if (mNetworkByteActivityCounters != null && type >= 0
5923 && type < mNetworkByteActivityCounters.length) {
5924 return mNetworkByteActivityCounters[type].getCountLocked(which);
5925 } else {
5926 return 0;
5927 }
5928 }
5929
5930 @Override
5931 public long getNetworkActivityPackets(int type, int which) {
5932 if (mNetworkPacketActivityCounters != null && type >= 0
5933 && type < mNetworkPacketActivityCounters.length) {
5934 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005935 } else {
5936 return 0;
5937 }
5938 }
5939
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005940 @Override
5941 public long getMobileRadioActiveTime(int which) {
5942 return mMobileRadioActiveTime != null
5943 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5944 }
5945
5946 @Override
5947 public int getMobileRadioActiveCount(int which) {
5948 return mMobileRadioActiveCount != null
5949 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5950 }
5951
Adam Lesinskie08af192015-03-25 16:42:59 -07005952 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005953 public long getUserCpuTimeUs(int which) {
5954 return mUserCpuTime.getCountLocked(which);
5955 }
5956
5957 @Override
5958 public long getSystemCpuTimeUs(int which) {
5959 return mSystemCpuTime.getCountLocked(which);
5960 }
5961
5962 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005963 public long getCpuPowerMaUs(int which) {
5964 return mCpuPower.getCountLocked(which);
5965 }
5966
5967 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005968 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5969 if (mCpuClusterSpeed != null) {
5970 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5971 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5972 if (cpuSpeeds != null) {
5973 if (step >= 0 && step < cpuSpeeds.length) {
5974 final LongSamplingCounter c = cpuSpeeds[step];
5975 if (c != null) {
5976 return c.getCountLocked(which);
5977 }
5978 }
5979 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005980 }
5981 }
5982 return 0;
5983 }
5984
Adam Lesinski5f056f62016-07-14 16:56:08 -07005985 public void noteMobileRadioApWakeupLocked() {
5986 if (mMobileRadioApWakeupCount == null) {
5987 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5988 }
5989 mMobileRadioApWakeupCount.addCountLocked(1);
5990 }
5991
5992 @Override
5993 public long getMobileRadioApWakeupCount(int which) {
5994 if (mMobileRadioApWakeupCount != null) {
5995 return mMobileRadioApWakeupCount.getCountLocked(which);
5996 }
5997 return 0;
5998 }
5999
6000 public void noteWifiRadioApWakeupLocked() {
6001 if (mWifiRadioApWakeupCount == null) {
6002 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6003 }
6004 mWifiRadioApWakeupCount.addCountLocked(1);
6005 }
6006
6007 @Override
6008 public long getWifiRadioApWakeupCount(int which) {
6009 if (mWifiRadioApWakeupCount != null) {
6010 return mWifiRadioApWakeupCount.getCountLocked(which);
6011 }
6012 return 0;
6013 }
6014
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006015 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006016 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6017 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006018 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006019 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6020 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006021 }
Joe Onoratoabded112016-02-08 16:49:39 -08006022 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6023 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006025
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006026 /**
6027 * Clear all stats for this uid. Returns true if the uid is completely
6028 * inactive so can be dropped.
6029 */
6030 boolean reset() {
6031 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006032
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006033 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006034 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006035 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006036 }
6037 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006038 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006039 active |= mFullWifiLockOut;
6040 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006041 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006042 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006043 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006044 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006045 if (mWifiBatchedScanTimer != null) {
6046 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6047 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006048 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006049 }
6050 }
6051 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6052 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006053 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006054 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006055 active |= mWifiMulticastEnabled;
6056 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006057
6058 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6059 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6060 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6061 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6062 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
6063 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
6064
Dianne Hackborn61659e52014-07-09 16:13:01 -07006065 if (mProcessStateTimer != null) {
6066 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6067 if (mProcessStateTimer[i] != null) {
6068 active |= !mProcessStateTimer[i].reset(false);
6069 }
6070 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006071 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006072 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006073 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006074 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006075 mVibratorOnTimer.detach();
6076 mVibratorOnTimer = null;
6077 } else {
6078 active = true;
6079 }
6080 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006081
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006082 if (mUserActivityCounters != null) {
6083 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6084 mUserActivityCounters[i].reset(false);
6085 }
6086 }
6087
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006088 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006089 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006090 mNetworkByteActivityCounters[i].reset(false);
6091 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006092 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006093 mMobileRadioActiveTime.reset(false);
6094 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006095 }
6096
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006097 if (mWifiControllerActivity != null) {
6098 mWifiControllerActivity.reset(false);
6099 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006100
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006101 if (mBluetoothControllerActivity != null) {
6102 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006103 }
6104
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006105 if (mModemControllerActivity != null) {
6106 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006107 }
6108
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006109 mUserCpuTime.reset(false);
6110 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006111 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006112
6113 if (mCpuClusterSpeed != null) {
6114 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
6115 if (speeds != null) {
6116 for (LongSamplingCounter speed : speeds) {
6117 if (speed != null) {
6118 speed.reset(false);
6119 }
6120 }
6121 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006122 }
6123 }
6124
Adam Lesinski5f056f62016-07-14 16:56:08 -07006125 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6126 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6127
Dianne Hackbornd953c532014-08-16 18:17:38 -07006128 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6129 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6130 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006131 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006132 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006133 } else {
6134 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006135 }
6136 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006137 mWakelockStats.cleanup();
6138 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6139 for (int is=syncStats.size()-1; is>=0; is--) {
6140 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006141 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006142 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006143 timer.detach();
6144 } else {
6145 active = true;
6146 }
6147 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006148 mSyncStats.cleanup();
6149 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6150 for (int ij=jobStats.size()-1; ij>=0; ij--) {
6151 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006152 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006153 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006154 timer.detach();
6155 } else {
6156 active = true;
6157 }
6158 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006159 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006160 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6161 Sensor s = mSensorStats.valueAt(ise);
6162 if (s.reset()) {
6163 mSensorStats.removeAt(ise);
6164 } else {
6165 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006166 }
6167 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006168 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6169 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006170 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006171 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006172 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006173 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006174 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006175 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006176 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006177 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006178 } else {
6179 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006180 }
6181 }
6182 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006183 if (mPackageStats.size() > 0) {
6184 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6185 while (it.hasNext()) {
6186 Map.Entry<String, Pkg> pkgEntry = it.next();
6187 Pkg p = pkgEntry.getValue();
6188 p.detach();
6189 if (p.mServiceStats.size() > 0) {
6190 Iterator<Map.Entry<String, Pkg.Serv>> it2
6191 = p.mServiceStats.entrySet().iterator();
6192 while (it2.hasNext()) {
6193 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6194 servEntry.getValue().detach();
6195 }
6196 }
6197 }
6198 mPackageStats.clear();
6199 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006200
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006201 mLastStepUserTime = mLastStepSystemTime = 0;
6202 mCurStepUserTime = mCurStepSystemTime = 0;
6203
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006204 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006205 if (mWifiRunningTimer != null) {
6206 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006207 }
6208 if (mFullWifiLockTimer != null) {
6209 mFullWifiLockTimer.detach();
6210 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006211 if (mWifiScanTimer != null) {
6212 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006213 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006214 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6215 if (mWifiBatchedScanTimer[i] != null) {
6216 mWifiBatchedScanTimer[i].detach();
6217 }
6218 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006219 if (mWifiMulticastTimer != null) {
6220 mWifiMulticastTimer.detach();
6221 }
6222 if (mAudioTurnedOnTimer != null) {
6223 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006224 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006225 }
6226 if (mVideoTurnedOnTimer != null) {
6227 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006228 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006229 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006230 if (mFlashlightTurnedOnTimer != null) {
6231 mFlashlightTurnedOnTimer.detach();
6232 mFlashlightTurnedOnTimer = null;
6233 }
6234 if (mCameraTurnedOnTimer != null) {
6235 mCameraTurnedOnTimer.detach();
6236 mCameraTurnedOnTimer = null;
6237 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006238 if (mForegroundActivityTimer != null) {
6239 mForegroundActivityTimer.detach();
6240 mForegroundActivityTimer = null;
6241 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006242 if (mBluetoothScanTimer != null) {
6243 mBluetoothScanTimer.detach();
6244 mBluetoothScanTimer = null;
6245 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006246 if (mUserActivityCounters != null) {
6247 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6248 mUserActivityCounters[i].detach();
6249 }
6250 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006251 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006252 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006253 mNetworkByteActivityCounters[i].detach();
6254 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006255 }
6256 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006257
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006258 if (mWifiControllerActivity != null) {
6259 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006260 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006261
6262 if (mBluetoothControllerActivity != null) {
6263 mBluetoothControllerActivity.detach();
6264 }
6265
6266 if (mModemControllerActivity != null) {
6267 mModemControllerActivity.detach();
6268 }
6269
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006270 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006271
6272 mUserCpuTime.detach();
6273 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006274 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006275
6276 if (mCpuClusterSpeed != null) {
6277 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6278 if (cpuSpeeds != null) {
6279 for (LongSamplingCounter c : cpuSpeeds) {
6280 if (c != null) {
6281 c.detach();
6282 }
6283 }
6284 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006285 }
6286 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006287
6288 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6289 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006290 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006291
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006292 return !active;
6293 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006294
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006295 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006296 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6297 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006298 out.writeInt(NW);
6299 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006300 out.writeString(wakeStats.keyAt(iw));
6301 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006302 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 }
6304
Dianne Hackbornd953c532014-08-16 18:17:38 -07006305 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6306 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006307 out.writeInt(NS);
6308 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006309 out.writeString(syncStats.keyAt(is));
6310 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006311 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6312 }
6313
Dianne Hackbornd953c532014-08-16 18:17:38 -07006314 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6315 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006316 out.writeInt(NJ);
6317 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006318 out.writeString(jobStats.keyAt(ij));
6319 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006320 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6321 }
6322
Dianne Hackborn61659e52014-07-09 16:13:01 -07006323 int NSE = mSensorStats.size();
6324 out.writeInt(NSE);
6325 for (int ise=0; ise<NSE; ise++) {
6326 out.writeInt(mSensorStats.keyAt(ise));
6327 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006328 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 }
6330
Dianne Hackborn61659e52014-07-09 16:13:01 -07006331 int NP = mProcessStats.size();
6332 out.writeInt(NP);
6333 for (int ip=0; ip<NP; ip++) {
6334 out.writeString(mProcessStats.keyAt(ip));
6335 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 proc.writeToParcelLocked(out);
6337 }
6338
6339 out.writeInt(mPackageStats.size());
6340 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6341 out.writeString(pkgEntry.getKey());
6342 Uid.Pkg pkg = pkgEntry.getValue();
6343 pkg.writeToParcelLocked(out);
6344 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006345
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006346 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006347 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006348 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006349 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006350 out.writeInt(0);
6351 }
6352 if (mFullWifiLockTimer != null) {
6353 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006354 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006355 } else {
6356 out.writeInt(0);
6357 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006358 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006359 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006360 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006361 } else {
6362 out.writeInt(0);
6363 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006364 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6365 if (mWifiBatchedScanTimer[i] != null) {
6366 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006367 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006368 } else {
6369 out.writeInt(0);
6370 }
6371 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006372 if (mWifiMulticastTimer != null) {
6373 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006374 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006375 } else {
6376 out.writeInt(0);
6377 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006378
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006379 if (mAudioTurnedOnTimer != null) {
6380 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006381 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006382 } else {
6383 out.writeInt(0);
6384 }
6385 if (mVideoTurnedOnTimer != null) {
6386 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006387 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006388 } else {
6389 out.writeInt(0);
6390 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006391 if (mFlashlightTurnedOnTimer != null) {
6392 out.writeInt(1);
6393 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6394 } else {
6395 out.writeInt(0);
6396 }
6397 if (mCameraTurnedOnTimer != null) {
6398 out.writeInt(1);
6399 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6400 } else {
6401 out.writeInt(0);
6402 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006403 if (mForegroundActivityTimer != null) {
6404 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006405 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006406 } else {
6407 out.writeInt(0);
6408 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006409 if (mBluetoothScanTimer != null) {
6410 out.writeInt(1);
6411 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6412 } else {
6413 out.writeInt(0);
6414 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006415 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6416 if (mProcessStateTimer[i] != null) {
6417 out.writeInt(1);
6418 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6419 } else {
6420 out.writeInt(0);
6421 }
6422 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006423 if (mVibratorOnTimer != null) {
6424 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006425 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006426 } else {
6427 out.writeInt(0);
6428 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006429 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006430 out.writeInt(1);
6431 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6432 mUserActivityCounters[i].writeToParcel(out);
6433 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006434 } else {
6435 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006436 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006437 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006438 out.writeInt(1);
6439 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006440 mNetworkByteActivityCounters[i].writeToParcel(out);
6441 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006442 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006443 mMobileRadioActiveTime.writeToParcel(out);
6444 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006445 } else {
6446 out.writeInt(0);
6447 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006448
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006449 if (mWifiControllerActivity != null) {
6450 out.writeInt(1);
6451 mWifiControllerActivity.writeToParcel(out, 0);
6452 } else {
6453 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006454 }
6455
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006456 if (mBluetoothControllerActivity != null) {
6457 out.writeInt(1);
6458 mBluetoothControllerActivity.writeToParcel(out, 0);
6459 } else {
6460 out.writeInt(0);
6461 }
6462
6463 if (mModemControllerActivity != null) {
6464 out.writeInt(1);
6465 mModemControllerActivity.writeToParcel(out, 0);
6466 } else {
6467 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006468 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006469
6470 mUserCpuTime.writeToParcel(out);
6471 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006472 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006473
Adam Lesinski6832f392015-09-05 18:05:40 -07006474 if (mCpuClusterSpeed != null) {
6475 out.writeInt(1);
6476 out.writeInt(mCpuClusterSpeed.length);
6477 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6478 if (cpuSpeeds != null) {
6479 out.writeInt(1);
6480 out.writeInt(cpuSpeeds.length);
6481 for (LongSamplingCounter c : cpuSpeeds) {
6482 if (c != null) {
6483 out.writeInt(1);
6484 c.writeToParcel(out);
6485 } else {
6486 out.writeInt(0);
6487 }
6488 }
6489 } else {
6490 out.writeInt(0);
6491 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006492 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006493 } else {
6494 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006495 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006496
6497 if (mMobileRadioApWakeupCount != null) {
6498 out.writeInt(1);
6499 mMobileRadioApWakeupCount.writeToParcel(out);
6500 } else {
6501 out.writeInt(0);
6502 }
6503
6504 if (mWifiRadioApWakeupCount != null) {
6505 out.writeInt(1);
6506 mWifiRadioApWakeupCount.writeToParcel(out);
6507 } else {
6508 out.writeInt(0);
6509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 }
6511
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006512 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 int numWakelocks = in.readInt();
6514 mWakelockStats.clear();
6515 for (int j = 0; j < numWakelocks; j++) {
6516 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006517 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006518 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006519 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520 }
6521
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006522 int numSyncs = in.readInt();
6523 mSyncStats.clear();
6524 for (int j = 0; j < numSyncs; j++) {
6525 String syncName = in.readString();
6526 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006527 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006528 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006529 }
6530 }
6531
6532 int numJobs = in.readInt();
6533 mJobStats.clear();
6534 for (int j = 0; j < numJobs; j++) {
6535 String jobName = in.readString();
6536 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006537 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
6538 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006539 }
6540 }
6541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 int numSensors = in.readInt();
6543 mSensorStats.clear();
6544 for (int k = 0; k < numSensors; k++) {
6545 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006546 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
6547 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 mSensorStats.put(sensorNumber, sensor);
6549 }
6550
6551 int numProcs = in.readInt();
6552 mProcessStats.clear();
6553 for (int k = 0; k < numProcs; k++) {
6554 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006555 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 proc.readFromParcelLocked(in);
6557 mProcessStats.put(processName, proc);
6558 }
6559
6560 int numPkgs = in.readInt();
6561 mPackageStats.clear();
6562 for (int l = 0; l < numPkgs; l++) {
6563 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006564 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 pkg.readFromParcelLocked(in);
6566 mPackageStats.put(packageName, pkg);
6567 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006568
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006569 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006570 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006571 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6572 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006573 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006574 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006575 }
6576 mFullWifiLockOut = false;
6577 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006578 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6579 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006580 } else {
6581 mFullWifiLockTimer = null;
6582 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006583 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006584 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006585 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6586 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006587 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006588 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006589 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006590 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6591 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6592 if (in.readInt() != 0) {
6593 makeWifiBatchedScanBin(i, in);
6594 } else {
6595 mWifiBatchedScanTimer[i] = null;
6596 }
6597 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006598 mWifiMulticastEnabled = false;
6599 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006600 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6601 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006602 } else {
6603 mWifiMulticastTimer = null;
6604 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006605 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006606 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6607 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006608 } else {
6609 mAudioTurnedOnTimer = null;
6610 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006611 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006612 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6613 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006614 } else {
6615 mVideoTurnedOnTimer = null;
6616 }
6617 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006618 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6619 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006620 } else {
6621 mFlashlightTurnedOnTimer = null;
6622 }
6623 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006624 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6625 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006626 } else {
6627 mCameraTurnedOnTimer = null;
6628 }
6629 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006630 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6631 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006632 } else {
6633 mForegroundActivityTimer = null;
6634 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006635 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006636 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6637 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006638 } else {
6639 mBluetoothScanTimer = null;
6640 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006641 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006642 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6643 if (in.readInt() != 0) {
6644 makeProcessState(i, in);
6645 } else {
6646 mProcessStateTimer[i] = null;
6647 }
6648 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006649 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006650 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6651 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006652 } else {
6653 mVibratorOnTimer = null;
6654 }
6655 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006656 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6657 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006658 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006659 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006660 } else {
6661 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006662 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006663 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006664 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6665 mNetworkPacketActivityCounters
6666 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006667 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006668 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006669 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006670 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006671 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006672 }
Joe Onoratoabded112016-02-08 16:49:39 -08006673 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6674 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006675 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006676 mNetworkByteActivityCounters = null;
6677 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006678 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006679
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006680 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006681 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006682 NUM_WIFI_TX_LEVELS, in);
6683 } else {
6684 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006685 }
6686
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006687 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006688 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006689 NUM_BT_TX_LEVELS, in);
6690 } else {
6691 mBluetoothControllerActivity = null;
6692 }
6693
6694 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006695 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006696 ModemActivityInfo.TX_POWER_LEVELS, in);
6697 } else {
6698 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006699 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006700
Joe Onoratoabded112016-02-08 16:49:39 -08006701 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6702 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6703 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006704
Adam Lesinski6832f392015-09-05 18:05:40 -07006705 if (in.readInt() != 0) {
6706 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006707 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006708 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006709 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006710
6711 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6712 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6713 if (in.readInt() != 0) {
6714 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006715 if (mBsi.mPowerProfile != null &&
6716 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006717 throw new ParcelFormatException("Incompatible number of cpu speeds");
6718 }
6719
6720 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6721 mCpuClusterSpeed[cluster] = cpuSpeeds;
6722 for (int speed = 0; speed < numSpeeds; speed++) {
6723 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006724 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006725 }
6726 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006727 } else {
6728 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006729 }
6730 }
6731 } else {
6732 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006733 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006734
6735 if (in.readInt() != 0) {
6736 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6737 } else {
6738 mMobileRadioApWakeupCount = null;
6739 }
6740
6741 if (in.readInt() != 0) {
6742 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6743 } else {
6744 mWifiRadioApWakeupCount = null;
6745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 }
6747
6748 /**
6749 * The statistics associated with a particular wake lock.
6750 */
Joe Onoratoabded112016-02-08 16:49:39 -08006751 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6752 /**
6753 * BatteryStatsImpl that we are associated with.
6754 */
6755 protected BatteryStatsImpl mBsi;
6756
6757 /**
6758 * BatteryStatsImpl that we are associated with.
6759 */
6760 protected Uid mUid;
6761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 /**
6763 * How long (in ms) this uid has been keeping the device partially awake.
6764 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07006765 DurationTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766
6767 /**
6768 * How long (in ms) this uid has been keeping the device fully awake.
6769 */
Evan Millarc64edde2009-04-18 12:26:32 -07006770 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771
6772 /**
6773 * How long (in ms) this uid has had a window keeping the device awake.
6774 */
Evan Millarc64edde2009-04-18 12:26:32 -07006775 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776
6777 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006778 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006779 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006780 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006781
Joe Onoratoabded112016-02-08 16:49:39 -08006782 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6783 mBsi = bsi;
6784 mUid = uid;
6785 }
6786
Adam Lesinski9425fe22015-06-19 12:02:13 -07006787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6789 * proper timer pool from the given BatteryStatsImpl object.
6790 *
6791 * @param in the Parcel to be read from.
6792 * return a new Timer, or null.
6793 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07006794 private StopwatchTimer readStopwatchTimerFromParcel(int type,
6795 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 if (in.readInt() == 0) {
6797 return null;
6798 }
6799
Joe Onoratoabded112016-02-08 16:49:39 -08006800 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 }
6802
Joe Onorato92fd23f2016-07-25 11:18:42 -07006803 /**
6804 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6805 * proper timer pool from the given BatteryStatsImpl object.
6806 *
6807 * @param in the Parcel to be read from.
6808 * return a new Timer, or null.
6809 */
6810 private DurationTimer readDurationTimerFromParcel(int type,
6811 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
6812 if (in.readInt() == 0) {
6813 return null;
6814 }
6815
6816 return new DurationTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
6817 }
6818
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006819 boolean reset() {
6820 boolean wlactive = false;
6821 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006822 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006823 }
6824 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006825 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006826 }
6827 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006828 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006829 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006830 if (mTimerDraw != null) {
6831 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006832 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006833 if (!wlactive) {
6834 if (mTimerFull != null) {
6835 mTimerFull.detach();
6836 mTimerFull = null;
6837 }
6838 if (mTimerPartial != null) {
6839 mTimerPartial.detach();
6840 mTimerPartial = null;
6841 }
6842 if (mTimerWindow != null) {
6843 mTimerWindow.detach();
6844 mTimerWindow = null;
6845 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006846 if (mTimerDraw != null) {
6847 mTimerDraw.detach();
6848 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006849 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006850 }
6851 return !wlactive;
6852 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006853
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006854 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07006855 mTimerPartial = readDurationTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006856 mBsi.mPartialTimers, screenOffTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07006857 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
6858 mBsi.mFullTimers, timeBase, in);
6859 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
6860 mBsi.mWindowTimers, timeBase, in);
6861 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
6862 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006865 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6866 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6867 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6868 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006869 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 }
6871
6872 @Override
6873 public Timer getWakeTime(int type) {
6874 switch (type) {
6875 case WAKE_TYPE_FULL: return mTimerFull;
6876 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6877 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006878 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 default: throw new IllegalArgumentException("type = " + type);
6880 }
6881 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006882
6883 public StopwatchTimer getStopwatchTimer(int type) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006884 switch (type) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07006885 case WAKE_TYPE_PARTIAL: {
6886 DurationTimer t = mTimerPartial;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006887 if (t == null) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07006888 t = new DurationTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006889 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006890 mTimerPartial = t;
6891 }
6892 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006893 }
6894 case WAKE_TYPE_FULL: {
6895 StopwatchTimer t = mTimerFull;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006896 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006897 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6898 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006899 mTimerFull = t;
6900 }
6901 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006902 }
6903 case WAKE_TYPE_WINDOW: {
6904 StopwatchTimer t = mTimerWindow;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006905 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006906 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6907 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006908 mTimerWindow = t;
6909 }
6910 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006911 }
6912 case WAKE_TYPE_DRAW: {
6913 StopwatchTimer t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006914 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006915 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6916 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006917 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006918 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006919 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006920 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006921 default:
6922 throw new IllegalArgumentException("type=" + type);
6923 }
6924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 }
6926
Joe Onoratoabded112016-02-08 16:49:39 -08006927 public static class Sensor extends BatteryStats.Uid.Sensor {
6928 /**
6929 * BatteryStatsImpl that we are associated with.
6930 */
6931 protected BatteryStatsImpl mBsi;
6932
6933 /**
6934 * BatteryStatsImpl that we are associated with.
6935 */
6936 protected Uid mUid;
6937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006939 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006940
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006941 Counter mBgCounter;
6942
Joe Onoratoabded112016-02-08 16:49:39 -08006943 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6944 mBsi = bsi;
6945 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 mHandle = handle;
6947 }
6948
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006949 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 if (in.readInt() == 0) {
6951 return null;
6952 }
6953
Joe Onoratoabded112016-02-08 16:49:39 -08006954 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006956 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006957 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 }
Joe Onoratoabded112016-02-08 16:49:39 -08006959 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 }
6961
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006962 private Counter readCounterFromParcel(TimeBase timeBase, Parcel in) {
6963 if (in.readInt() == 0) {
6964 return null;
6965 }
6966 return new Counter(timeBase, in);
6967 }
6968
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006969 boolean reset() {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006970 if (mBgCounter != null) {
6971 mBgCounter.reset(true);
6972 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006973 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006974 mTimer = null;
6975 return true;
6976 }
6977 return false;
6978 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006979
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006980 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6981 mTimer = readTimerFromParcel(timeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006982 mBgCounter = readCounterFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 }
6984
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006985 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6986 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006987 Counter.writeCounterToParcel(out, mBgCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 }
6989
6990 @Override
6991 public Timer getSensorTime() {
6992 return mTimer;
6993 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006994
6995 @Override
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006996 public Counter getSensorBgCount() {
6997 return mBgCounter;
6998 }
6999
7000 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 public int getHandle() {
7002 return mHandle;
7003 }
7004 }
7005
7006 /**
7007 * The statistics associated with a particular process.
7008 */
Joe Onoratoabded112016-02-08 16:49:39 -08007009 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7010 /**
7011 * BatteryStatsImpl that we are associated with.
7012 */
7013 protected BatteryStatsImpl mBsi;
7014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007016 * The name of this process.
7017 */
7018 final String mName;
7019
7020 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007021 * Remains true until removed from the stats.
7022 */
7023 boolean mActive = true;
7024
7025 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007026 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 */
7028 long mUserTime;
7029
7030 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007031 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007032 */
7033 long mSystemTime;
7034
7035 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007036 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007037 */
7038 long mForegroundTime;
7039
7040 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007041 * Number of times the process has been started.
7042 */
7043 int mStarts;
7044
7045 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007046 * Number of times the process has crashed.
7047 */
7048 int mNumCrashes;
7049
7050 /**
7051 * Number of times the process has had an ANR.
7052 */
7053 int mNumAnrs;
7054
7055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 * The amount of user time loaded from a previous save.
7057 */
7058 long mLoadedUserTime;
7059
7060 /**
7061 * The amount of system time loaded from a previous save.
7062 */
7063 long mLoadedSystemTime;
7064
7065 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007066 * The amount of foreground time loaded from a previous save.
7067 */
7068 long mLoadedForegroundTime;
7069
7070 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007071 * The number of times the process has started from a previous save.
7072 */
7073 int mLoadedStarts;
7074
7075 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007076 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007078 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079
7080 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007081 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007083 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007084
7085 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 * The amount of user time when last unplugged.
7087 */
7088 long mUnpluggedUserTime;
7089
7090 /**
7091 * The amount of system time when last unplugged.
7092 */
7093 long mUnpluggedSystemTime;
7094
7095 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007096 * The amount of foreground time since unplugged.
7097 */
7098 long mUnpluggedForegroundTime;
7099
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007100 /**
7101 * The number of times the process has started before unplugged.
7102 */
7103 int mUnpluggedStarts;
7104
Dianne Hackborn61659e52014-07-09 16:13:01 -07007105 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007106 * Number of times the process has crashed before unplugged.
7107 */
7108 int mUnpluggedNumCrashes;
7109
7110 /**
7111 * Number of times the process has had an ANR before unplugged.
7112 */
7113 int mUnpluggedNumAnrs;
7114
Dianne Hackborn287952c2010-09-22 22:34:31 -07007115 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007116
Joe Onoratoabded112016-02-08 16:49:39 -08007117 public Proc(BatteryStatsImpl bsi, String name) {
7118 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007119 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007120 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007122
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007123 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 mUnpluggedUserTime = mUserTime;
7125 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007126 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007127 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007128 mUnpluggedNumCrashes = mNumCrashes;
7129 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007130 }
7131
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007132 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007134
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007135 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007136 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007137 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007138 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007139
Dianne Hackborn287952c2010-09-22 22:34:31 -07007140 public int countExcessivePowers() {
7141 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007142 }
7143
Dianne Hackborn287952c2010-09-22 22:34:31 -07007144 public ExcessivePower getExcessivePower(int i) {
7145 if (mExcessivePower != null) {
7146 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007147 }
7148 return null;
7149 }
7150
7151 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007152 if (mExcessivePower == null) {
7153 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007154 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007155 ExcessivePower ew = new ExcessivePower();
7156 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007157 ew.overTime = overTime;
7158 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007159 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007160 }
7161
Dianne Hackborn287952c2010-09-22 22:34:31 -07007162 public void addExcessiveCpu(long overTime, long usedTime) {
7163 if (mExcessivePower == null) {
7164 mExcessivePower = new ArrayList<ExcessivePower>();
7165 }
7166 ExcessivePower ew = new ExcessivePower();
7167 ew.type = ExcessivePower.TYPE_CPU;
7168 ew.overTime = overTime;
7169 ew.usedTime = usedTime;
7170 mExcessivePower.add(ew);
7171 }
7172
7173 void writeExcessivePowerToParcelLocked(Parcel out) {
7174 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007175 out.writeInt(0);
7176 return;
7177 }
7178
Dianne Hackborn287952c2010-09-22 22:34:31 -07007179 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007180 out.writeInt(N);
7181 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007182 ExcessivePower ew = mExcessivePower.get(i);
7183 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007184 out.writeLong(ew.overTime);
7185 out.writeLong(ew.usedTime);
7186 }
7187 }
7188
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007189 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007190 final int N = in.readInt();
7191 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007192 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007193 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007194 }
7195
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007196 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007197 throw new ParcelFormatException(
7198 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007199 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007200
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007201 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007202 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007203 ExcessivePower ew = new ExcessivePower();
7204 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007205 ew.overTime = in.readLong();
7206 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007207 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007208 }
7209 }
7210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 void writeToParcelLocked(Parcel out) {
7212 out.writeLong(mUserTime);
7213 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007214 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007215 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007216 out.writeInt(mNumCrashes);
7217 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 out.writeLong(mLoadedUserTime);
7219 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007220 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007221 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007222 out.writeInt(mLoadedNumCrashes);
7223 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 out.writeLong(mUnpluggedUserTime);
7225 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007226 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007228 out.writeInt(mUnpluggedNumCrashes);
7229 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007230 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 }
7232
7233 void readFromParcelLocked(Parcel in) {
7234 mUserTime = in.readLong();
7235 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007236 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007238 mNumCrashes = in.readInt();
7239 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 mLoadedUserTime = in.readLong();
7241 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007242 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007244 mLoadedNumCrashes = in.readInt();
7245 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007246 mUnpluggedUserTime = in.readLong();
7247 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007248 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007249 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007250 mUnpluggedNumCrashes = in.readInt();
7251 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007252 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 }
7254
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007255 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 mUserTime += utime;
7257 mSystemTime += stime;
7258 }
7259
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007260 public void addForegroundTimeLocked(long ttime) {
7261 mForegroundTime += ttime;
7262 }
7263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 public void incStartsLocked() {
7265 mStarts++;
7266 }
7267
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007268 public void incNumCrashesLocked() {
7269 mNumCrashes++;
7270 }
7271
7272 public void incNumAnrsLocked() {
7273 mNumAnrs++;
7274 }
7275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007277 public boolean isActive() {
7278 return mActive;
7279 }
7280
7281 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007283 long val = mUserTime;
7284 if (which == STATS_CURRENT) {
7285 val -= mLoadedUserTime;
7286 } else if (which == STATS_SINCE_UNPLUGGED) {
7287 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 }
7289 return val;
7290 }
7291
7292 @Override
7293 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007294 long val = mSystemTime;
7295 if (which == STATS_CURRENT) {
7296 val -= mLoadedSystemTime;
7297 } else if (which == STATS_SINCE_UNPLUGGED) {
7298 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007299 }
7300 return val;
7301 }
7302
7303 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007304 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007305 long val = mForegroundTime;
7306 if (which == STATS_CURRENT) {
7307 val -= mLoadedForegroundTime;
7308 } else if (which == STATS_SINCE_UNPLUGGED) {
7309 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007310 }
7311 return val;
7312 }
7313
7314 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007316 int val = mStarts;
7317 if (which == STATS_CURRENT) {
7318 val -= mLoadedStarts;
7319 } else if (which == STATS_SINCE_UNPLUGGED) {
7320 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 }
7322 return val;
7323 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007324
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007325 @Override
7326 public int getNumCrashes(int which) {
7327 int val = mNumCrashes;
7328 if (which == STATS_CURRENT) {
7329 val -= mLoadedNumCrashes;
7330 } else if (which == STATS_SINCE_UNPLUGGED) {
7331 val -= mUnpluggedNumCrashes;
7332 }
7333 return val;
7334 }
7335
7336 @Override
7337 public int getNumAnrs(int which) {
7338 int val = mNumAnrs;
7339 if (which == STATS_CURRENT) {
7340 val -= mLoadedNumAnrs;
7341 } else if (which == STATS_SINCE_UNPLUGGED) {
7342 val -= mUnpluggedNumAnrs;
7343 }
7344 return val;
7345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 }
7347
7348 /**
7349 * The statistics associated with a particular package.
7350 */
Joe Onoratoabded112016-02-08 16:49:39 -08007351 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7352 /**
7353 * BatteryStatsImpl that we are associated with.
7354 */
7355 protected BatteryStatsImpl mBsi;
7356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007358 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007360 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361
7362 /**
7363 * The statics we have collected for this package's services.
7364 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007365 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366
Joe Onoratoabded112016-02-08 16:49:39 -08007367 public Pkg(BatteryStatsImpl bsi) {
7368 mBsi = bsi;
7369 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007371
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007372 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 }
7374
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007375 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007377
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007378 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007379 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007380 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007383 int numWA = in.readInt();
7384 mWakeupAlarms.clear();
7385 for (int i=0; i<numWA; i++) {
7386 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007387 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389
7390 int numServs = in.readInt();
7391 mServiceStats.clear();
7392 for (int m = 0; m < numServs; m++) {
7393 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007394 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 mServiceStats.put(serviceName, serv);
7396
7397 serv.readFromParcelLocked(in);
7398 }
7399 }
7400
7401 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007402 int numWA = mWakeupAlarms.size();
7403 out.writeInt(numWA);
7404 for (int i=0; i<numWA; i++) {
7405 out.writeString(mWakeupAlarms.keyAt(i));
7406 mWakeupAlarms.valueAt(i).writeToParcel(out);
7407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007409 final int NS = mServiceStats.size();
7410 out.writeInt(NS);
7411 for (int i=0; i<NS; i++) {
7412 out.writeString(mServiceStats.keyAt(i));
7413 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 serv.writeToParcelLocked(out);
7415 }
7416 }
7417
7418 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007419 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7420 return mWakeupAlarms;
7421 }
7422
7423 public void noteWakeupAlarmLocked(String tag) {
7424 Counter c = mWakeupAlarms.get(tag);
7425 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007426 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007427 mWakeupAlarms.put(tag, c);
7428 }
7429 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 }
7431
7432 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007433 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
7434 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 }
7436
7437 /**
7438 * The statistics associated with a particular service.
7439 */
Joe Onoratoabded112016-02-08 16:49:39 -08007440 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
7441 /**
7442 * BatteryStatsImpl that we are associated with.
7443 */
7444 protected BatteryStatsImpl mBsi;
7445
7446 /**
7447 * The android package in which this service resides.
7448 */
7449 protected Pkg mPkg;
7450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451 /**
7452 * Total time (ms in battery uptime) the service has been left started.
7453 */
Joe Onoratoabded112016-02-08 16:49:39 -08007454 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455
7456 /**
7457 * If service has been started and not yet stopped, this is
7458 * when it was started.
7459 */
Joe Onoratoabded112016-02-08 16:49:39 -08007460 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007461
7462 /**
7463 * True if we are currently running.
7464 */
Joe Onoratoabded112016-02-08 16:49:39 -08007465 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466
7467 /**
7468 * Total number of times startService() has been called.
7469 */
Joe Onoratoabded112016-02-08 16:49:39 -08007470 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471
7472 /**
7473 * Total time (ms in battery uptime) the service has been left launched.
7474 */
Joe Onoratoabded112016-02-08 16:49:39 -08007475 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476
7477 /**
7478 * If service has been launched and not yet exited, this is
7479 * when it was launched (ms in battery uptime).
7480 */
Joe Onoratoabded112016-02-08 16:49:39 -08007481 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482
7483 /**
7484 * True if we are currently launched.
7485 */
Joe Onoratoabded112016-02-08 16:49:39 -08007486 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487
7488 /**
7489 * Total number times the service has been launched.
7490 */
Joe Onoratoabded112016-02-08 16:49:39 -08007491 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492
7493 /**
7494 * The amount of time spent started loaded from a previous save
7495 * (ms in battery uptime).
7496 */
Joe Onoratoabded112016-02-08 16:49:39 -08007497 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007498
7499 /**
7500 * The number of starts loaded from a previous save.
7501 */
Joe Onoratoabded112016-02-08 16:49:39 -08007502 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503
7504 /**
7505 * The number of launches loaded from a previous save.
7506 */
Joe Onoratoabded112016-02-08 16:49:39 -08007507 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508
7509 /**
7510 * The amount of time spent started as of the last run (ms
7511 * in battery uptime).
7512 */
Joe Onoratoabded112016-02-08 16:49:39 -08007513 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514
7515 /**
7516 * The number of starts as of the last run.
7517 */
Joe Onoratoabded112016-02-08 16:49:39 -08007518 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519
7520 /**
7521 * The number of launches as of the last run.
7522 */
Joe Onoratoabded112016-02-08 16:49:39 -08007523 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524
7525 /**
7526 * The amount of time spent started when last unplugged (ms
7527 * in battery uptime).
7528 */
Joe Onoratoabded112016-02-08 16:49:39 -08007529 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530
7531 /**
7532 * The number of starts when last unplugged.
7533 */
Joe Onoratoabded112016-02-08 16:49:39 -08007534 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535
7536 /**
7537 * The number of launches when last unplugged.
7538 */
Joe Onoratoabded112016-02-08 16:49:39 -08007539 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007540
Joe Onoratoabded112016-02-08 16:49:39 -08007541 /**
7542 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7543 */
7544 public Serv(BatteryStatsImpl bsi) {
7545 mBsi = bsi;
7546 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007548
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007549 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7550 long baseRealtime) {
7551 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007552 mUnpluggedStarts = mStarts;
7553 mUnpluggedLaunches = mLaunches;
7554 }
7555
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007556 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7557 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007559
Joe Onoratoabded112016-02-08 16:49:39 -08007560 /**
7561 * Remove this Serv as a listener from the time base.
7562 */
7563 public void detach() {
7564 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007565 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007566
Joe Onoratoabded112016-02-08 16:49:39 -08007567 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 mStartTime = in.readLong();
7569 mRunningSince = in.readLong();
7570 mRunning = in.readInt() != 0;
7571 mStarts = in.readInt();
7572 mLaunchedTime = in.readLong();
7573 mLaunchedSince = in.readLong();
7574 mLaunched = in.readInt() != 0;
7575 mLaunches = in.readInt();
7576 mLoadedStartTime = in.readLong();
7577 mLoadedStarts = in.readInt();
7578 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007579 mLastStartTime = 0;
7580 mLastStarts = 0;
7581 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 mUnpluggedStartTime = in.readLong();
7583 mUnpluggedStarts = in.readInt();
7584 mUnpluggedLaunches = in.readInt();
7585 }
7586
Joe Onoratoabded112016-02-08 16:49:39 -08007587 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 out.writeLong(mStartTime);
7589 out.writeLong(mRunningSince);
7590 out.writeInt(mRunning ? 1 : 0);
7591 out.writeInt(mStarts);
7592 out.writeLong(mLaunchedTime);
7593 out.writeLong(mLaunchedSince);
7594 out.writeInt(mLaunched ? 1 : 0);
7595 out.writeInt(mLaunches);
7596 out.writeLong(mLoadedStartTime);
7597 out.writeInt(mLoadedStarts);
7598 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 out.writeLong(mUnpluggedStartTime);
7600 out.writeInt(mUnpluggedStarts);
7601 out.writeInt(mUnpluggedLaunches);
7602 }
7603
Joe Onoratoabded112016-02-08 16:49:39 -08007604 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 if (!mLaunched) return mLaunchedTime;
7606 return mLaunchedTime + batteryUptime - mLaunchedSince;
7607 }
7608
Joe Onoratoabded112016-02-08 16:49:39 -08007609 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007610 if (!mRunning) return mStartTime;
7611 return mStartTime + batteryUptime - mRunningSince;
7612 }
7613
7614 public void startLaunchedLocked() {
7615 if (!mLaunched) {
7616 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007617 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 mLaunched = true;
7619 }
7620 }
7621
7622 public void stopLaunchedLocked() {
7623 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007624 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625 if (time > 0) {
7626 mLaunchedTime += time;
7627 } else {
7628 mLaunches--;
7629 }
7630 mLaunched = false;
7631 }
7632 }
7633
7634 public void startRunningLocked() {
7635 if (!mRunning) {
7636 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007637 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 mRunning = true;
7639 }
7640 }
7641
7642 public void stopRunningLocked() {
7643 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007644 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 if (time > 0) {
7646 mStartTime += time;
7647 } else {
7648 mStarts--;
7649 }
7650 mRunning = false;
7651 }
7652 }
7653
7654 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007655 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 }
7657
7658 @Override
7659 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007660 int val = mLaunches;
7661 if (which == STATS_CURRENT) {
7662 val -= mLoadedLaunches;
7663 } else if (which == STATS_SINCE_UNPLUGGED) {
7664 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 return val;
7667 }
7668
7669 @Override
7670 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007671 long val = getStartTimeToNowLocked(now);
7672 if (which == STATS_CURRENT) {
7673 val -= mLoadedStartTime;
7674 } else if (which == STATS_SINCE_UNPLUGGED) {
7675 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 return val;
7678 }
7679
7680 @Override
7681 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007682 int val = mStarts;
7683 if (which == STATS_CURRENT) {
7684 val -= mLoadedStarts;
7685 } else if (which == STATS_SINCE_UNPLUGGED) {
7686 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007687 }
7688
7689 return val;
7690 }
7691 }
7692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007694 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 }
7696 }
7697
7698 /**
7699 * Retrieve the statistics object for a particular process, creating
7700 * if needed.
7701 */
7702 public Proc getProcessStatsLocked(String name) {
7703 Proc ps = mProcessStats.get(name);
7704 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007705 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 mProcessStats.put(name, ps);
7707 }
7708
7709 return ps;
7710 }
7711
Dianne Hackborna8d10942015-11-19 17:55:19 -08007712 public void updateUidProcessStateLocked(int procState) {
7713 int uidRunningState;
7714 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7715 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7716 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7717 uidRunningState = PROCESS_STATE_TOP;
7718 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7719 // Persistent and other foreground states go here.
7720 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7721 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7722 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7723 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7724 // Persistent and other foreground states go here.
7725 uidRunningState = PROCESS_STATE_FOREGROUND;
7726 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7727 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007728 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007729 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007730 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007731
Dianne Hackborna8d10942015-11-19 17:55:19 -08007732 if (mProcessState == uidRunningState) return;
7733
Joe Onoratoabded112016-02-08 16:49:39 -08007734 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007735
7736 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7737 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7738 }
7739 mProcessState = uidRunningState;
7740 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7741 if (mProcessStateTimer[uidRunningState] == null) {
7742 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007743 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007744 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007745 }
7746 }
7747
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007748 public SparseArray<? extends Pid> getPidStats() {
7749 return mPids;
7750 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007751
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007752 public Pid getPidStatsLocked(int pid) {
7753 Pid p = mPids.get(pid);
7754 if (p == null) {
7755 p = new Pid();
7756 mPids.put(pid, p);
7757 }
7758 return p;
7759 }
7760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 /**
7762 * Retrieve the statistics object for a particular service, creating
7763 * if needed.
7764 */
7765 public Pkg getPackageStatsLocked(String name) {
7766 Pkg ps = mPackageStats.get(name);
7767 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007768 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 mPackageStats.put(name, ps);
7770 }
7771
7772 return ps;
7773 }
7774
7775 /**
7776 * Retrieve the statistics object for a particular service, creating
7777 * if needed.
7778 */
7779 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7780 Pkg ps = getPackageStatsLocked(pkg);
7781 Pkg.Serv ss = ps.mServiceStats.get(serv);
7782 if (ss == null) {
7783 ss = ps.newServiceStatsLocked();
7784 ps.mServiceStats.put(serv, ss);
7785 }
7786
7787 return ss;
7788 }
7789
Dianne Hackbornd953c532014-08-16 18:17:38 -07007790 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7791 StopwatchTimer timer = mSyncStats.instantiateObject();
7792 timer.readSummaryFromParcelLocked(in);
7793 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007794 }
7795
Dianne Hackbornd953c532014-08-16 18:17:38 -07007796 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7797 StopwatchTimer timer = mJobStats.instantiateObject();
7798 timer.readSummaryFromParcelLocked(in);
7799 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007800 }
7801
Dianne Hackbornd953c532014-08-16 18:17:38 -07007802 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007803 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007804 mWakelockStats.add(wlName, wl);
7805 if (in.readInt() != 0) {
7806 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007808 if (in.readInt() != 0) {
7809 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7810 }
7811 if (in.readInt() != 0) {
7812 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007814 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007815 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 }
7818
Evan Millarc64edde2009-04-18 12:26:32 -07007819 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 Sensor se = mSensorStats.get(sensor);
7821 if (se == null) {
7822 if (!create) {
7823 return null;
7824 }
Joe Onoratoabded112016-02-08 16:49:39 -08007825 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 mSensorStats.put(sensor, se);
7827 }
Evan Millarc64edde2009-04-18 12:26:32 -07007828 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 if (t != null) {
7830 return t;
7831 }
Joe Onoratoabded112016-02-08 16:49:39 -08007832 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007834 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007835 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 }
Joe Onoratoabded112016-02-08 16:49:39 -08007837 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7838 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 se.mTimer = t;
7840 return t;
7841 }
7842
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007843 public Counter getSensorBgCounterLocked(int sensor, boolean create) {
7844 Sensor se = mSensorStats.get(sensor);
7845 if (se == null) {
7846 if (!create) {
7847 return null;
7848 }
7849 se = new Sensor(mBsi, this, sensor);
7850 mSensorStats.put(sensor, se);
7851 }
7852 Counter c = se.mBgCounter;
7853 if (c != null) return c;
7854 c = new Counter(mBsi.mOnBatteryTimeBase);
7855 se.mBgCounter = c;
7856 return c;
7857 }
7858
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007859 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007860 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007861 if (t != null) {
7862 t.startRunningLocked(elapsedRealtimeMs);
7863 }
7864 }
7865
7866 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007867 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007868 if (t != null) {
7869 t.stopRunningLocked(elapsedRealtimeMs);
7870 }
7871 }
7872
7873 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007874 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007875 if (t != null) {
7876 t.startRunningLocked(elapsedRealtimeMs);
7877 }
7878 }
7879
7880 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007881 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007882 if (t != null) {
7883 t.stopRunningLocked(elapsedRealtimeMs);
7884 }
7885 }
7886
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007887 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007888 Wakelock wl = mWakelockStats.startObject(name);
7889 if (wl != null) {
7890 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007892 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007893 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007894 if (p.mWakeNesting++ == 0) {
7895 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007896 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898 }
7899
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007900 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007901 Wakelock wl = mWakelockStats.stopObject(name);
7902 if (wl != null) {
7903 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007905 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007906 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007907 if (p != null && p.mWakeNesting > 0) {
7908 if (p.mWakeNesting-- == 1) {
7909 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7910 p.mWakeStartMs = 0;
7911 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007912 }
7913 }
7914 }
7915
7916 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7917 Proc p = getProcessStatsLocked(proc);
7918 if (p != null) {
7919 p.addExcessiveWake(overTime, usedTime);
7920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007922
Dianne Hackborn287952c2010-09-22 22:34:31 -07007923 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7924 Proc p = getProcessStatsLocked(proc);
7925 if (p != null) {
7926 p.addExcessiveCpu(overTime, usedTime);
7927 }
7928 }
7929
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007930 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Amith Yamasani154d1242017-02-16 10:01:48 -08007931 StopwatchTimer t = getSensorTimerLocked(sensor, /* create= */ true);
7932 t.startRunningLocked(elapsedRealtimeMs);
7933
7934 Counter c = getSensorBgCounterLocked(sensor, /* create= */ true);
7935 if (mProcessState >= PROCESS_STATE_BACKGROUND && t.mNesting == 1) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007936 c.stepAtomic();
7937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 }
7939
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007940 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007942 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007944 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007947
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007948 public void noteStartGps(long elapsedRealtimeMs) {
Amith Yamasani154d1242017-02-16 10:01:48 -08007949 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, /* create= */ true);
7950 t.startRunningLocked(elapsedRealtimeMs);
7951
7952 Counter c = getSensorBgCounterLocked(Sensor.GPS, /* create= */ true);
7953 if (mProcessState >= PROCESS_STATE_BACKGROUND && t.mNesting == 1) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007954 c.stepAtomic();
7955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007957
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007958 public void noteStopGps(long elapsedRealtimeMs) {
Amith Yamasani154d1242017-02-16 10:01:48 -08007959 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007960 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007962 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007964 }
7965
7966 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007967 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 }
7969 }
7970
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007971 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007972 this(new SystemClocks(), systemDir, handler, externalSync, null);
7973 }
7974
7975 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
7976 PlatformIdleStateCallback cb) {
7977 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08007978 }
7979
7980 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007981 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08007982 init(clocks);
7983
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007984 if (systemDir != null) {
7985 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7986 new File(systemDir, "batterystats.bin.tmp"));
7987 } else {
7988 mFile = null;
7989 }
7990 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007991 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007992 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007993 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007994 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007995 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007996 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007997 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007998 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007999 }
Joe Onoratoabded112016-02-08 16:49:39 -08008000 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8001 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8002 mOnBatteryTimeBase);
8003 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8004 mOnBatteryTimeBase);
8005 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8006 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8007 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8008 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8009 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8010 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8011 mOnBatteryTimeBase);
8012 }
8013 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8014 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008015 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008016 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008017 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008018 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008019 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008020 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8021 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008022 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008023 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8024 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8025 NUM_BT_TX_LEVELS);
8026 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8027 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008028 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8029 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8030 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008031 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008032 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8033 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008034 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8035 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008036 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008037 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008038 mOnBatteryTimeBase);
8039 }
Joe Onoratoabded112016-02-08 16:49:39 -08008040 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8041 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8042 mOnBatteryTimeBase);
8043 }
8044 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8045 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8046 mOnBatteryTimeBase);
8047 }
8048 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8049 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8050 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8051 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8052 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008053 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8054 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008056 long uptime = mClocks.uptimeMillis() * 1000;
8057 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008058 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008059 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008060 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008061 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008062 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008063 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008064 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008065 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008066 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008067 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008068 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 }
8070
8071 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008072 this(new SystemClocks(), p);
8073 }
8074
8075 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8076 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008077 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008078 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008079 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008080 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008081 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008082 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008084 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008085 }
8086
Adam Lesinskie08af192015-03-25 16:42:59 -07008087 public void setPowerProfile(PowerProfile profile) {
8088 synchronized (this) {
8089 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008090
8091 // We need to initialize the KernelCpuSpeedReaders to read from
8092 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8093 // information.
8094 final int numClusters = mPowerProfile.getNumCpuClusters();
8095 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8096 int firstCpuOfCluster = 0;
8097 for (int i = 0; i < numClusters; i++) {
8098 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8099 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8100 numSpeedSteps);
8101 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8102 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008103
8104 if (mEstimatedBatteryCapacity == -1) {
8105 // Initialize the estimated battery capacity to a known preset one.
8106 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8107 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008108 }
8109 }
8110
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008111 public void setCallback(BatteryCallback cb) {
8112 mCallback = cb;
8113 }
8114
Amith Yamasanif37447b2009-10-08 18:28:01 -07008115 public void setRadioScanningTimeout(long timeout) {
8116 if (mPhoneSignalScanningTimer != null) {
8117 mPhoneSignalScanningTimer.setTimeout(timeout);
8118 }
8119 }
8120
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008121 public void updateDailyDeadlineLocked() {
8122 // Get the current time.
8123 long currentTime = mDailyStartTime = System.currentTimeMillis();
8124 Calendar calDeadline = Calendar.getInstance();
8125 calDeadline.setTimeInMillis(currentTime);
8126
8127 // Move time up to the next day, ranging from 1am to 3pm.
8128 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8129 calDeadline.set(Calendar.MILLISECOND, 0);
8130 calDeadline.set(Calendar.SECOND, 0);
8131 calDeadline.set(Calendar.MINUTE, 0);
8132 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8133 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8134 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8135 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8136 }
8137
8138 public void recordDailyStatsIfNeededLocked(boolean settled) {
8139 long currentTime = System.currentTimeMillis();
8140 if (currentTime >= mNextMaxDailyDeadline) {
8141 recordDailyStatsLocked();
8142 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8143 recordDailyStatsLocked();
8144 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8145 recordDailyStatsLocked();
8146 }
8147 }
8148
8149 public void recordDailyStatsLocked() {
8150 DailyItem item = new DailyItem();
8151 item.mStartTime = mDailyStartTime;
8152 item.mEndTime = System.currentTimeMillis();
8153 boolean hasData = false;
8154 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8155 hasData = true;
8156 item.mDischargeSteps = new LevelStepTracker(
8157 mDailyDischargeStepTracker.mNumStepDurations,
8158 mDailyDischargeStepTracker.mStepDurations);
8159 }
8160 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8161 hasData = true;
8162 item.mChargeSteps = new LevelStepTracker(
8163 mDailyChargeStepTracker.mNumStepDurations,
8164 mDailyChargeStepTracker.mStepDurations);
8165 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008166 if (mDailyPackageChanges != null) {
8167 hasData = true;
8168 item.mPackageChanges = mDailyPackageChanges;
8169 mDailyPackageChanges = null;
8170 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008171 mDailyDischargeStepTracker.init();
8172 mDailyChargeStepTracker.init();
8173 updateDailyDeadlineLocked();
8174
8175 if (hasData) {
8176 mDailyItems.add(item);
8177 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8178 mDailyItems.remove(0);
8179 }
8180 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8181 try {
8182 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008183 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008184 writeDailyItemsLocked(out);
8185 BackgroundThread.getHandler().post(new Runnable() {
8186 @Override
8187 public void run() {
8188 synchronized (mCheckinFile) {
8189 FileOutputStream stream = null;
8190 try {
8191 stream = mDailyFile.startWrite();
8192 memStream.writeTo(stream);
8193 stream.flush();
8194 FileUtils.sync(stream);
8195 stream.close();
8196 mDailyFile.finishWrite(stream);
8197 } catch (IOException e) {
8198 Slog.w("BatteryStats",
8199 "Error writing battery daily items", e);
8200 mDailyFile.failWrite(stream);
8201 }
8202 }
8203 }
8204 });
8205 } catch (IOException e) {
8206 }
8207 }
8208 }
8209
8210 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8211 StringBuilder sb = new StringBuilder(64);
8212 out.startDocument(null, true);
8213 out.startTag(null, "daily-items");
8214 for (int i=0; i<mDailyItems.size(); i++) {
8215 final DailyItem dit = mDailyItems.get(i);
8216 out.startTag(null, "item");
8217 out.attribute(null, "start", Long.toString(dit.mStartTime));
8218 out.attribute(null, "end", Long.toString(dit.mEndTime));
8219 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8220 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008221 if (dit.mPackageChanges != null) {
8222 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8223 PackageChange pc = dit.mPackageChanges.get(j);
8224 if (pc.mUpdate) {
8225 out.startTag(null, "upd");
8226 out.attribute(null, "pkg", pc.mPackageName);
8227 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8228 out.endTag(null, "upd");
8229 } else {
8230 out.startTag(null, "rem");
8231 out.attribute(null, "pkg", pc.mPackageName);
8232 out.endTag(null, "rem");
8233 }
8234 }
8235 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008236 out.endTag(null, "item");
8237 }
8238 out.endTag(null, "daily-items");
8239 out.endDocument();
8240 }
8241
8242 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
8243 StringBuilder tmpBuilder) throws IOException {
8244 if (steps != null) {
8245 out.startTag(null, tag);
8246 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
8247 for (int i=0; i<steps.mNumStepDurations; i++) {
8248 out.startTag(null, "s");
8249 tmpBuilder.setLength(0);
8250 steps.encodeEntryAt(i, tmpBuilder);
8251 out.attribute(null, "v", tmpBuilder.toString());
8252 out.endTag(null, "s");
8253 }
8254 out.endTag(null, tag);
8255 }
8256 }
8257
8258 public void readDailyStatsLocked() {
8259 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
8260 mDailyItems.clear();
8261 FileInputStream stream;
8262 try {
8263 stream = mDailyFile.openRead();
8264 } catch (FileNotFoundException e) {
8265 return;
8266 }
8267 try {
8268 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008269 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008270 readDailyItemsLocked(parser);
8271 } catch (XmlPullParserException e) {
8272 } finally {
8273 try {
8274 stream.close();
8275 } catch (IOException e) {
8276 }
8277 }
8278 }
8279
8280 private void readDailyItemsLocked(XmlPullParser parser) {
8281 try {
8282 int type;
8283 while ((type = parser.next()) != XmlPullParser.START_TAG
8284 && type != XmlPullParser.END_DOCUMENT) {
8285 ;
8286 }
8287
8288 if (type != XmlPullParser.START_TAG) {
8289 throw new IllegalStateException("no start tag found");
8290 }
8291
8292 int outerDepth = parser.getDepth();
8293 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8294 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8295 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8296 continue;
8297 }
8298
8299 String tagName = parser.getName();
8300 if (tagName.equals("item")) {
8301 readDailyItemTagLocked(parser);
8302 } else {
8303 Slog.w(TAG, "Unknown element under <daily-items>: "
8304 + parser.getName());
8305 XmlUtils.skipCurrentTag(parser);
8306 }
8307 }
8308
8309 } catch (IllegalStateException e) {
8310 Slog.w(TAG, "Failed parsing daily " + e);
8311 } catch (NullPointerException e) {
8312 Slog.w(TAG, "Failed parsing daily " + e);
8313 } catch (NumberFormatException e) {
8314 Slog.w(TAG, "Failed parsing daily " + e);
8315 } catch (XmlPullParserException e) {
8316 Slog.w(TAG, "Failed parsing daily " + e);
8317 } catch (IOException e) {
8318 Slog.w(TAG, "Failed parsing daily " + e);
8319 } catch (IndexOutOfBoundsException e) {
8320 Slog.w(TAG, "Failed parsing daily " + e);
8321 }
8322 }
8323
8324 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8325 XmlPullParserException, IOException {
8326 DailyItem dit = new DailyItem();
8327 String attr = parser.getAttributeValue(null, "start");
8328 if (attr != null) {
8329 dit.mStartTime = Long.parseLong(attr);
8330 }
8331 attr = parser.getAttributeValue(null, "end");
8332 if (attr != null) {
8333 dit.mEndTime = Long.parseLong(attr);
8334 }
8335 int outerDepth = parser.getDepth();
8336 int type;
8337 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8338 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8339 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8340 continue;
8341 }
8342
8343 String tagName = parser.getName();
8344 if (tagName.equals("dis")) {
8345 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8346 } else if (tagName.equals("chg")) {
8347 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008348 } else if (tagName.equals("upd")) {
8349 if (dit.mPackageChanges == null) {
8350 dit.mPackageChanges = new ArrayList<>();
8351 }
8352 PackageChange pc = new PackageChange();
8353 pc.mUpdate = true;
8354 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8355 String verStr = parser.getAttributeValue(null, "ver");
8356 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8357 dit.mPackageChanges.add(pc);
8358 XmlUtils.skipCurrentTag(parser);
8359 } else if (tagName.equals("rem")) {
8360 if (dit.mPackageChanges == null) {
8361 dit.mPackageChanges = new ArrayList<>();
8362 }
8363 PackageChange pc = new PackageChange();
8364 pc.mUpdate = false;
8365 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8366 dit.mPackageChanges.add(pc);
8367 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008368 } else {
8369 Slog.w(TAG, "Unknown element under <item>: "
8370 + parser.getName());
8371 XmlUtils.skipCurrentTag(parser);
8372 }
8373 }
8374 mDailyItems.add(dit);
8375 }
8376
8377 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8378 String tag)
8379 throws NumberFormatException, XmlPullParserException, IOException {
8380 final String numAttr = parser.getAttributeValue(null, "n");
8381 if (numAttr == null) {
8382 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
8383 XmlUtils.skipCurrentTag(parser);
8384 return;
8385 }
8386 final int num = Integer.parseInt(numAttr);
8387 LevelStepTracker steps = new LevelStepTracker(num);
8388 if (isCharge) {
8389 dit.mChargeSteps = steps;
8390 } else {
8391 dit.mDischargeSteps = steps;
8392 }
8393 int i = 0;
8394 int outerDepth = parser.getDepth();
8395 int type;
8396 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8397 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8398 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8399 continue;
8400 }
8401
8402 String tagName = parser.getName();
8403 if ("s".equals(tagName)) {
8404 if (i < num) {
8405 String valueAttr = parser.getAttributeValue(null, "v");
8406 if (valueAttr != null) {
8407 steps.decodeEntryAt(i, valueAttr);
8408 i++;
8409 }
8410 }
8411 } else {
8412 Slog.w(TAG, "Unknown element under <" + tag + ">: "
8413 + parser.getName());
8414 XmlUtils.skipCurrentTag(parser);
8415 }
8416 }
8417 steps.mNumStepDurations = i;
8418 }
8419
8420 @Override
8421 public DailyItem getDailyItemLocked(int daysAgo) {
8422 int index = mDailyItems.size()-1-daysAgo;
8423 return index >= 0 ? mDailyItems.get(index) : null;
8424 }
8425
8426 @Override
8427 public long getCurrentDailyStartTime() {
8428 return mDailyStartTime;
8429 }
8430
8431 @Override
8432 public long getNextMinDailyDeadline() {
8433 return mNextMinDailyDeadline;
8434 }
8435
8436 @Override
8437 public long getNextMaxDailyDeadline() {
8438 return mNextMaxDailyDeadline;
8439 }
8440
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008441 @Override
8442 public boolean startIteratingOldHistoryLocked() {
8443 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8444 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008445 if ((mHistoryIterator = mHistory) == null) {
8446 return false;
8447 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008448 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008449 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008450 mReadOverflow = false;
8451 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008452 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008453 }
8454
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008455 @Override
8456 public boolean getNextOldHistoryLocked(HistoryItem out) {
8457 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
8458 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008459 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008460 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008461 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008462 HistoryItem cur = mHistoryIterator;
8463 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008464 if (!mReadOverflow && !end) {
8465 Slog.w(TAG, "Old history ends before new history!");
8466 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008467 return false;
8468 }
8469 out.setTo(cur);
8470 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008471 if (!mReadOverflow) {
8472 if (end) {
8473 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008474 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008475 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008476 pw.println("Histories differ!");
8477 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008478 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008479 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008480 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8481 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008482 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008483 }
8484 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008485 return true;
8486 }
8487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008489 public void finishIteratingOldHistoryLocked() {
8490 mIteratingHistory = false;
8491 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008492 mHistoryIterator = null;
8493 }
8494
8495 public int getHistoryTotalSize() {
8496 return MAX_HISTORY_BUFFER;
8497 }
8498
8499 public int getHistoryUsedSize() {
8500 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008501 }
8502
8503 @Override
8504 public boolean startIteratingHistoryLocked() {
8505 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8506 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008507 if (mHistoryBuffer.dataSize() <= 0) {
8508 return false;
8509 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008510 mHistoryBuffer.setDataPosition(0);
8511 mReadOverflow = false;
8512 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008513 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8514 mReadHistoryUids = new int[mHistoryTagPool.size()];
8515 mReadHistoryChars = 0;
8516 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8517 final HistoryTag tag = ent.getKey();
8518 final int idx = ent.getValue();
8519 mReadHistoryStrings[idx] = tag.string;
8520 mReadHistoryUids[idx] = tag.uid;
8521 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008522 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008523 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008524 }
8525
8526 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008527 public int getHistoryStringPoolSize() {
8528 return mReadHistoryStrings.length;
8529 }
8530
8531 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008532 public int getHistoryStringPoolBytes() {
8533 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8534 // Each string character is 2 bytes.
8535 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8536 }
8537
8538 @Override
8539 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008540 return mReadHistoryStrings[index];
8541 }
8542
8543 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008544 public int getHistoryTagPoolUid(int index) {
8545 return mReadHistoryUids[index];
8546 }
8547
8548 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008549 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008550 final int pos = mHistoryBuffer.dataPosition();
8551 if (pos == 0) {
8552 out.clear();
8553 }
8554 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008555 if (end) {
8556 return false;
8557 }
8558
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008559 final long lastRealtime = out.time;
8560 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008561 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008562 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8563 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008564 out.currentTime = lastWalltime + (out.time - lastRealtime);
8565 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008566 return true;
8567 }
8568
8569 @Override
8570 public void finishIteratingHistoryLocked() {
8571 mIteratingHistory = false;
8572 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008573 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008574 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008575
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008576 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008577 public long getHistoryBaseTime() {
8578 return mHistoryBaseTime;
8579 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008580
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008581 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 public int getStartCount() {
8583 return mStartCount;
8584 }
8585
8586 public boolean isOnBattery() {
8587 return mOnBattery;
8588 }
8589
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008590 public boolean isCharging() {
8591 return mCharging;
8592 }
8593
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008594 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008595 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008596 }
8597
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008598 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008599 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008600 mOnBatteryTimeBase.init(uptime, realtime);
8601 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008602 mRealtime = 0;
8603 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008604 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008605 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008606 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008607
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008608 void initDischarge() {
8609 mLowDischargeAmountSinceCharge = 0;
8610 mHighDischargeAmountSinceCharge = 0;
8611 mDischargeAmountScreenOn = 0;
8612 mDischargeAmountScreenOnSinceCharge = 0;
8613 mDischargeAmountScreenOff = 0;
8614 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008615 mDischargeStepTracker.init();
8616 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008617 mDischargeScreenOffCounter.reset(false);
8618 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008619 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008620
8621 public void resetAllStatsCmdLocked() {
8622 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008623 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008624 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008625 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008626 long realtime = mSecRealtime * 1000;
8627 mDischargeStartLevel = mHistoryCur.batteryLevel;
8628 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008629 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008630 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8631 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008632 mOnBatteryTimeBase.reset(uptime, realtime);
8633 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8634 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008635 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008636 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8637 mDischargeScreenOffUnplugLevel = 0;
8638 } else {
8639 mDischargeScreenOnUnplugLevel = 0;
8640 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8641 }
8642 mDischargeAmountScreenOn = 0;
8643 mDischargeAmountScreenOff = 0;
8644 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008645 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008646 }
8647
8648 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008649 final long uptimeMillis = mClocks.uptimeMillis();
8650 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008651 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008652 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008653 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008654 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008655 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008656 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008657
8658 if (mPowerProfile != null) {
8659 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8660 } else {
8661 mEstimatedBatteryCapacity = -1;
8662 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008663 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008664 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008665 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008666 mLongestLightIdleTime = 0;
8667 mLongestFullIdleTime = 0;
8668 mDeviceIdleModeLightTimer.reset(false);
8669 mDeviceIdleModeFullTimer.reset(false);
8670 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008671 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008672 mPhoneOnTimer.reset(false);
8673 mAudioOnTimer.reset(false);
8674 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008675 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008676 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008677 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008678 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008679 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008680 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008681 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008682 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008683 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008684 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008685 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008686 mNetworkByteActivityCounters[i].reset(false);
8687 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008688 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008689 mMobileRadioActiveTimer.reset(false);
8690 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008691 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008692 mMobileRadioActiveUnknownTime.reset(false);
8693 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008694 mWifiOnTimer.reset(false);
8695 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008696 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008697 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008698 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008699 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8700 mWifiSupplStateTimer[i].reset(false);
8701 }
8702 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8703 mWifiSignalStrengthsTimer[i].reset(false);
8704 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008705 mWifiActivity.reset(false);
8706 mBluetoothActivity.reset(false);
8707 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008708 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008709
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008710 for (int i=0; i<mUidStats.size(); i++) {
8711 if (mUidStats.valueAt(i).reset()) {
8712 mUidStats.remove(mUidStats.keyAt(i));
8713 i--;
8714 }
8715 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008716
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008717 if (mKernelWakelockStats.size() > 0) {
8718 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008719 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008720 }
8721 mKernelWakelockStats.clear();
8722 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008723
James Carr3a226052016-07-01 14:49:52 -07008724 if (mKernelMemoryStats.size() > 0) {
8725 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
8726 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
8727 }
8728 mKernelMemoryStats.clear();
8729 }
8730
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008731 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008732 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8733 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008734 }
8735 mWakeupReasonStats.clear();
8736 }
8737
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008738 mLastHistoryStepDetails = null;
8739 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8740 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8741 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8742 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8743 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8744 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8745 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8746 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8747 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8748 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8749
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008750 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008751
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008752 clearHistoryLocked();
8753 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008754
Dianne Hackborn40c87252014-03-19 16:55:40 -07008755 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008756 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008757 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8758 // Not recording process starts/stops.
8759 continue;
8760 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008761 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008762 if (active == null) {
8763 continue;
8764 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008765 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8766 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008767 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008768 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8769 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008770 }
8771 }
8772 }
8773 }
8774
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008775 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008776 if (oldScreenOn) {
8777 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8778 if (diff > 0) {
8779 mDischargeAmountScreenOn += diff;
8780 mDischargeAmountScreenOnSinceCharge += diff;
8781 }
8782 } else {
8783 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8784 if (diff > 0) {
8785 mDischargeAmountScreenOff += diff;
8786 mDischargeAmountScreenOffSinceCharge += diff;
8787 }
8788 }
8789 if (newScreenOn) {
8790 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8791 mDischargeScreenOffUnplugLevel = 0;
8792 } else {
8793 mDischargeScreenOnUnplugLevel = 0;
8794 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8795 }
8796 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008797
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008798 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008799 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008800 final boolean screenOn = mScreenState == Display.STATE_ON;
8801 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008802 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008803 }
8804
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008805 private String[] mMobileIfaces = EmptyArray.STRING;
8806 private String[] mWifiIfaces = EmptyArray.STRING;
8807
8808 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8809
8810 private static final int NETWORK_STATS_LAST = 0;
8811 private static final int NETWORK_STATS_NEXT = 1;
8812 private static final int NETWORK_STATS_DELTA = 2;
8813
Joe Onoratoabded112016-02-08 16:49:39 -08008814 private NetworkStats[] mMobileNetworkStats;
8815 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008816
8817 /**
8818 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8819 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8820 */
8821 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8822 NetworkStats[] networkStatsBuffer)
8823 throws IOException {
8824 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8825 false)) {
8826 return null;
8827 }
8828
8829 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8830 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8831 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8832 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8833 networkStatsBuffer[NETWORK_STATS_DELTA]);
8834 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8835 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8836 return networkStatsBuffer[NETWORK_STATS_DELTA];
8837 }
8838
8839 /**
8840 * Distribute WiFi energy info and network traffic to apps.
8841 * @param info The energy information from the WiFi controller.
8842 */
8843 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008844 if (DEBUG_ENERGY) {
8845 Slog.d(TAG, "Updating wifi stats");
8846 }
8847
Joe Onoratoabded112016-02-08 16:49:39 -08008848 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008849 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008850 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008851 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8852 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8853 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008854 } catch (IOException e) {
8855 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8856 return;
8857 }
8858
8859 if (!mOnBatteryInternal) {
8860 return;
8861 }
8862
Adam Lesinskie08af192015-03-25 16:42:59 -07008863 SparseLongArray rxPackets = new SparseLongArray();
8864 SparseLongArray txPackets = new SparseLongArray();
8865 long totalTxPackets = 0;
8866 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008867 if (delta != null) {
8868 final int size = delta.size();
8869 for (int i = 0; i < size; i++) {
8870 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8871
Adam Lesinskie08af192015-03-25 16:42:59 -07008872 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008873 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008874 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8875 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008876 }
8877
Adam Lesinski6cca4142016-02-25 18:23:02 -08008878 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8879 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008880 continue;
8881 }
8882
8883 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008884 if (entry.rxBytes != 0) {
8885 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8886 entry.rxPackets);
8887 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8888 entry.rxBytes);
8889 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8890 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008891
Adam Lesinskiba88e682015-12-08 12:06:55 -08008892 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008893
Adam Lesinskiba88e682015-12-08 12:06:55 -08008894 // Sum the total number of packets so that the Rx Power can
8895 // be evenly distributed amongst the apps.
8896 totalRxPackets += entry.rxPackets;
8897 }
8898
8899 if (entry.txBytes != 0) {
8900 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8901 entry.txPackets);
8902 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8903 entry.txBytes);
8904 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8905 entry.txPackets);
8906
8907 txPackets.put(u.getUid(), entry.txPackets);
8908
8909 // Sum the total number of packets so that the Tx Power can
8910 // be evenly distributed amongst the apps.
8911 totalTxPackets += entry.txPackets;
8912 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008913 }
8914 }
8915
8916 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008917 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008918
Adam Lesinskie08af192015-03-25 16:42:59 -07008919 // Measured in mAms
8920 final long txTimeMs = info.getControllerTxTimeMillis();
8921 final long rxTimeMs = info.getControllerRxTimeMillis();
8922 final long idleTimeMs = info.getControllerIdleTimeMillis();
8923 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8924
8925 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008926 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008927
8928 if (DEBUG_ENERGY) {
8929 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8930 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8931 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8932 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8933 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8934 }
8935
8936 long totalWifiLockTimeMs = 0;
8937 long totalScanTimeMs = 0;
8938
8939 // On the first pass, collect some totals so that we can normalize power
8940 // calculations if we need to.
8941 final int uidStatsSize = mUidStats.size();
8942 for (int i = 0; i < uidStatsSize; i++) {
8943 final Uid uid = mUidStats.valueAt(i);
8944
8945 // Sum the total scan power for all apps.
8946 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8947 elapsedRealtimeMs * 1000) / 1000;
8948
8949 // Sum the total time holding wifi lock for all apps.
8950 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8951 elapsedRealtimeMs * 1000) / 1000;
8952 }
8953
8954 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8955 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8956 + rxTimeMs + " ms). Normalizing scan time.");
8957 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008958 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8959 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8960 + txTimeMs + " ms). Normalizing scan time.");
8961 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008962
8963 // Actually assign and distribute power usage to apps.
8964 for (int i = 0; i < uidStatsSize; i++) {
8965 final Uid uid = mUidStats.valueAt(i);
8966
8967 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8968 elapsedRealtimeMs * 1000) / 1000;
8969 if (scanTimeSinceMarkMs > 0) {
8970 // Set the new mark so that next time we get new data since this point.
8971 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8972
Mitchell Willsf9016492015-07-29 17:47:44 -07008973 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8974 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8975
8976 // Our total scan time is more than the reported Tx/Rx time.
8977 // This is possible because the cost of a scan is approximate.
8978 // Let's normalize the result so that we evenly blame each app
8979 // scanning.
8980 //
8981 // This means that we may have apps that transmitted/received packets not be
8982 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008983 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008984 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8985 totalScanTimeMs;
8986 }
8987 if (totalScanTimeMs > txTimeMs) {
8988 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8989 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008990 }
8991
8992 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008993 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8994 + scanRxTimeSinceMarkMs + " ms Tx:"
8995 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008996 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008997
8998 ControllerActivityCounterImpl activityCounter =
8999 uid.getOrCreateWifiControllerActivityLocked();
9000 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9001 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07009002 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9003 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009004 }
9005
9006 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9007 // lock.
9008 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9009 elapsedRealtimeMs * 1000) / 1000;
9010 if (wifiLockTimeSinceMarkMs > 0) {
9011 // Set the new mark so that next time we get new data since this point.
9012 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9013
9014 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9015 / totalWifiLockTimeMs;
9016 if (DEBUG_ENERGY) {
9017 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9018 + myIdleTimeMs + " ms");
9019 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009020 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9021 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009022 }
9023 }
9024
9025 if (DEBUG_ENERGY) {
9026 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07009027 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009028 }
9029
Mitchell Willsf9016492015-07-29 17:47:44 -07009030 // Distribute the remaining Tx power appropriately between all apps that transmitted
9031 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07009032 for (int i = 0; i < txPackets.size(); i++) {
9033 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07009034 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07009035 if (DEBUG_ENERGY) {
9036 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9037 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009038 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9039 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009040 }
9041
9042 // Distribute the remaining Rx power appropriately between all apps that received
9043 // packets.
9044 for (int i = 0; i < rxPackets.size(); i++) {
9045 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9046 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
9047 if (DEBUG_ENERGY) {
9048 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9049 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009050 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9051 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009052 }
9053
9054 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9055
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009056 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009057 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9058 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
9059 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07009060
Adam Lesinski8576cf92015-06-09 12:48:25 -07009061 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9062 final double opVolt = mPowerProfile.getAveragePower(
9063 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9064 if (opVolt != 0) {
9065 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009066 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07009067 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07009068 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009069 }
9070 }
9071
9072 /**
9073 * Distribute Cell radio energy info and network traffic to apps.
9074 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009075 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
9076 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009077 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009078 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009079 }
9080
Adam Lesinskie08af192015-03-25 16:42:59 -07009081 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009082 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009083 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
9084 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
9085 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009086 } catch (IOException e) {
9087 Slog.wtf(TAG, "Failed to get mobile network stats", e);
9088 return;
9089 }
9090
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009091 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009092 return;
9093 }
9094
Adam Lesinskie08af192015-03-25 16:42:59 -07009095 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9096 elapsedRealtimeMs * 1000);
9097 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009098
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009099 long totalRxPackets = 0;
9100 long totalTxPackets = 0;
9101 if (delta != null) {
9102 final int size = delta.size();
9103 for (int i = 0; i < size; i++) {
9104 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08009105 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009106 continue;
9107 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009108
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009109 if (DEBUG_ENERGY) {
9110 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9111 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9112 + " txPackets=" + entry.txPackets);
9113 }
9114
9115 totalRxPackets += entry.rxPackets;
9116 totalTxPackets += entry.txPackets;
9117
9118 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9119 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
9120 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
9121
9122 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9123 entry.rxBytes);
9124 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9125 entry.txBytes);
9126 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9127 entry.rxPackets);
9128 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9129 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009130 }
9131
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009132 // Now distribute proportional blame to the apps that did networking.
9133 long totalPackets = totalRxPackets + totalTxPackets;
9134 if (totalPackets > 0) {
9135 for (int i = 0; i < size; i++) {
9136 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9137 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9138 continue;
9139 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009140
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009141 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9142
9143 // Distribute total radio active time in to this app.
9144 final long appPackets = entry.rxPackets + entry.txPackets;
9145 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9146 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9147
9148 // Remove this app from the totals, so that we don't lose any time
9149 // due to rounding.
9150 radioTime -= appRadioTime;
9151 totalPackets -= appPackets;
9152
9153 if (activityInfo != null) {
9154 ControllerActivityCounterImpl activityCounter =
9155 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08009156 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009157 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9158 / totalRxPackets;
9159 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9160 }
9161
Adam Lesinskia4268172016-01-29 12:13:54 -08009162 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009163 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9164 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9165 txMs /= totalTxPackets;
9166 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9167 }
9168 }
9169 }
9170 }
9171 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009172
9173 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009174 // Whoops, there is some radio time we can't blame on an app!
9175 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9176 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009177 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009178 }
9179
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009180 if (activityInfo != null) {
9181 mHasModemReporting = true;
9182 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
9183 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9184 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9185 mModemActivity.getTxTimeCounters()[lvl]
9186 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9187 }
9188
9189 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9190 final double opVolt = mPowerProfile.getAveragePower(
9191 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9192 if (opVolt != 0) {
9193 // We store the power drain as mAms.
9194 mModemActivity.getPowerCounter().addCountLocked(
9195 (long) (activityInfo.getEnergyUsed() / opVolt));
9196 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009197 }
9198 }
9199
9200 /**
9201 * Distribute Bluetooth energy info and network traffic to apps.
9202 * @param info The energy information from the bluetooth controller.
9203 */
9204 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009205 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009206 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009207 }
9208
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009209 if (info == null || !mOnBatteryInternal) {
9210 return;
9211 }
Adam Lesinskie283d332015-04-16 12:29:25 -07009212
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009213 mHasBluetoothReporting = true;
9214
9215 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
9216 final long rxTimeMs = info.getControllerRxTimeMillis();
9217 final long txTimeMs = info.getControllerTxTimeMillis();
9218
9219 if (DEBUG_ENERGY) {
9220 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
9221 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9222 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9223 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
9224 }
9225
9226 long totalScanTimeMs = 0;
9227
9228 final int uidCount = mUidStats.size();
9229 for (int i = 0; i < uidCount; i++) {
9230 final Uid u = mUidStats.valueAt(i);
9231 if (u.mBluetoothScanTimer == null) {
9232 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07009233 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009234
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009235 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
9236 elapsedRealtimeMs * 1000) / 1000;
9237 }
9238
9239 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
9240 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
9241
9242 if (DEBUG_ENERGY) {
9243 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
9244 + " TX=" + normalizeScanTxTime);
9245 }
9246
9247 long leftOverRxTimeMs = rxTimeMs;
9248 long leftOverTxTimeMs = txTimeMs;
9249
9250 for (int i = 0; i < uidCount; i++) {
9251 final Uid u = mUidStats.valueAt(i);
9252 if (u.mBluetoothScanTimer == null) {
9253 continue;
9254 }
9255
9256 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
9257 elapsedRealtimeMs * 1000) / 1000;
9258 if (scanTimeSinceMarkMs > 0) {
9259 // Set the new mark so that next time we get new data since this point.
9260 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
9261
9262 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
9263 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
9264
9265 if (normalizeScanRxTime) {
9266 // Scan time is longer than the total rx time in the controller,
9267 // so distribute the scan time proportionately. This means regular traffic
9268 // will not blamed, but scans are more expensive anyways.
9269 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
9270 }
9271
9272 if (normalizeScanTxTime) {
9273 // Scan time is longer than the total tx time in the controller,
9274 // so distribute the scan time proportionately. This means regular traffic
9275 // will not blamed, but scans are more expensive anyways.
9276 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
9277 }
9278
9279 final ControllerActivityCounterImpl counter =
9280 u.getOrCreateBluetoothControllerActivityLocked();
9281 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
9282 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
9283
9284 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
9285 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
9286 }
9287 }
9288
9289 if (DEBUG_ENERGY) {
9290 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
9291 + " TX=" + leftOverTxTimeMs);
9292 }
9293
9294 //
9295 // Now distribute blame to apps that did bluetooth traffic.
9296 //
9297
9298 long totalTxBytes = 0;
9299 long totalRxBytes = 0;
9300
9301 final UidTraffic[] uidTraffic = info.getUidTraffic();
9302 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9303 for (int i = 0; i < numUids; i++) {
9304 final UidTraffic traffic = uidTraffic[i];
9305
9306 // Add to the global counters.
9307 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9308 traffic.getRxBytes());
9309 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9310 traffic.getTxBytes());
9311
9312 // Add to the UID counters.
9313 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9314 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9315 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9316
9317 // Calculate the total traffic.
9318 totalTxBytes += traffic.getTxBytes();
9319 totalRxBytes += traffic.getRxBytes();
9320 }
9321
9322 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9323 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009324 for (int i = 0; i < numUids; i++) {
9325 final UidTraffic traffic = uidTraffic[i];
9326
Adam Lesinski50e47602015-12-04 17:04:54 -08009327 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009328 final ControllerActivityCounterImpl counter =
9329 u.getOrCreateBluetoothControllerActivityLocked();
9330
9331 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
9332 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9333
9334 if (DEBUG_ENERGY) {
9335 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
9336 + " rx_time=" + timeRxMs);
9337 }
9338 counter.getRxTimeCounter().addCountLocked(timeRxMs);
9339 leftOverRxTimeMs -= timeRxMs;
9340 }
9341
9342 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
9343 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
9344
9345 if (DEBUG_ENERGY) {
9346 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
9347 + " tx_time=" + timeTxMs);
9348 }
9349
9350 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
9351 leftOverTxTimeMs -= timeTxMs;
9352 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009353 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009354 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009355
9356 mBluetoothActivity.getRxTimeCounter().addCountLocked(
9357 info.getControllerRxTimeMillis());
9358 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
9359 info.getControllerTxTimeMillis());
9360 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
9361 info.getControllerIdleTimeMillis());
9362
9363 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9364 final double opVolt = mPowerProfile.getAveragePower(
9365 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9366 if (opVolt != 0) {
9367 // We store the power drain as mAms.
9368 mBluetoothActivity.getPowerCounter().addCountLocked(
9369 (long) (info.getControllerEnergyUsed() / opVolt));
9370 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009371 }
9372
9373 /**
9374 * Read and distribute kernel wake lock use across apps.
9375 */
9376 public void updateKernelWakelocksLocked() {
9377 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
9378 mTmpWakelockStats);
9379 if (wakelockStats == null) {
9380 // Not crashing might make board bringup easier.
9381 Slog.w(TAG, "Couldn't get kernel wake lock stats");
9382 return;
9383 }
9384
9385 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
9386 String name = ent.getKey();
9387 KernelWakelockStats.Entry kws = ent.getValue();
9388
9389 SamplingTimer kwlt = mKernelWakelockStats.get(name);
9390 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009391 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009392 mKernelWakelockStats.put(name, kwlt);
9393 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07009394
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009395 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009396 kwlt.setUpdateVersion(kws.mVersion);
9397 }
9398
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009399 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07009400 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
9401 // this time.
9402 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9403 SamplingTimer st = ent.getValue();
9404 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
9405 st.endSample();
9406 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009407 }
9408 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009409
Adam Lesinskid84ad302016-05-17 18:31:02 -07009410 // Record whether we've seen a non-zero time (for debugging b/22716723).
9411 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009412 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
9413 }
9414
9415 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
9416 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
9417 wakelockStats.kernelWakelockVersion);
9418 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009419 }
9420
Adam Lesinski72478f02015-06-17 15:39:43 -07009421 // We use an anonymous class to access these variables,
9422 // so they can't live on the stack or they'd have to be
9423 // final MutableLong objects (more allocations).
9424 // Used in updateCpuTimeLocked().
9425 long mTempTotalCpuUserTimeUs;
9426 long mTempTotalCpuSystemTimeUs;
9427
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009428 /**
James Carr3a226052016-07-01 14:49:52 -07009429 * Reads the newest memory stats from the kernel.
9430 */
9431 public void updateKernelMemoryBandwidthLocked() {
9432 mKernelMemoryBandwidthStats.updateStats();
9433 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
9434 final int bandwidthEntryCount = bandwidthEntries.size();
9435 int index;
9436 for (int i = 0; i < bandwidthEntryCount; i++) {
9437 SamplingTimer timer;
9438 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
9439 timer = mKernelMemoryStats.valueAt(index);
9440 } else {
9441 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
9442 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
9443 }
9444 timer.update(bandwidthEntries.valueAt(i), 1);
9445 if (DEBUG_MEMORY) {
9446 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
9447 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
9448 mKernelMemoryStats.get(
9449 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
9450 mKernelMemoryStats.size()));
9451 }
9452 }
9453 }
9454
9455 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07009456 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
9457 * and we are on battery with screen off, we give more of the cpu time to those apps holding
9458 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009459 */
Adam Lesinski72478f02015-06-17 15:39:43 -07009460 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009461 if (mPowerProfile == null) {
9462 return;
9463 }
9464
Adam Lesinski72478f02015-06-17 15:39:43 -07009465 if (DEBUG_ENERGY_CPU) {
9466 Slog.d(TAG, "!Cpu updating!");
9467 }
9468
9469 // Holding a wakelock costs more than just using the cpu.
9470 // Currently, we assign only half the cpu time to an app that is running but
9471 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
9472 // If no app is holding a wakelock, then the distribution is normal.
9473 final int wakelockWeight = 50;
9474
Adam Lesinski6832f392015-09-05 18:05:40 -07009475 // Read the time spent for each cluster at various cpu frequencies.
9476 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
9477 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
9478 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
9479 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009480
9481 int numWakelocks = 0;
9482
9483 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
9484 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
9485 final int numPartialTimers = mPartialTimers.size();
9486 if (mOnBatteryScreenOffTimeBase.isRunning()) {
9487 for (int i = 0; i < numPartialTimers; i++) {
9488 final StopwatchTimer timer = mPartialTimers.get(i);
9489 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9490 // Since the collection and blaming of wakelocks can be scheduled to run after
9491 // some delay, the mPartialTimers list may have new entries. We can't blame
9492 // the newly added timer for past cpu time, so we only consider timers that
9493 // were present for one round of collection. Once a timer has gone through
9494 // a round of collection, its mInList field is set to true.
9495 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009496 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009497 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009498 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009499
9500 final int numWakelocksF = numWakelocks;
9501 mTempTotalCpuUserTimeUs = 0;
9502 mTempTotalCpuSystemTimeUs = 0;
9503
9504 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9505 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9506 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009507 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009508 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9509 new KernelUidCpuTimeReader.Callback() {
9510 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009511 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
9512 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009513 final Uid u = getUidStatsLocked(mapUid(uid));
9514
9515 // Accumulate the total system and user time.
9516 mTempTotalCpuUserTimeUs += userTimeUs;
9517 mTempTotalCpuSystemTimeUs += systemTimeUs;
9518
9519 StringBuilder sb = null;
9520 if (DEBUG_ENERGY_CPU) {
9521 sb = new StringBuilder();
9522 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9523 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9524 sb.append(" s=");
9525 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009526 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009527 }
9528
9529 if (numWakelocksF > 0) {
9530 // We have wakelocks being held, so only give a portion of the
9531 // time to the process. The rest will be distributed among wakelock
9532 // holders.
9533 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9534 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9535 }
9536
9537 if (sb != null) {
9538 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9539 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9540 sb.append(" s=");
9541 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009542 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009543 Slog.d(TAG, sb.toString());
9544 }
9545
9546 u.mUserCpuTime.addCountLocked(userTimeUs);
9547 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009548 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009549
9550 // Add the cpu speeds to this UID. These are used as a ratio
9551 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009552 final int numClusters = mPowerProfile.getNumCpuClusters();
9553 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9554 numClusters) {
9555 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009556 }
9557
9558 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009559 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9560 cluster);
9561 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9562 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009563 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009564 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009565 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009566
9567 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9568 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9569 if (cpuSpeeds[speed] == null) {
9570 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9571 }
9572 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9573 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009574 }
9575 }
9576 });
9577
Amith Yamasanid2450862017-02-07 15:58:24 -08009578 // TODO: STOPSHIP, remove the "true" below after b/34961340 is fixed
9579 if (DEBUG_ENERGY_CPU || true) {
Joe Onoratoabded112016-02-08 16:49:39 -08009580 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009581 " ms");
9582 }
9583
9584 if (mOnBatteryInternal && numWakelocks > 0) {
9585 // Distribute a portion of the total cpu time to wakelock holders.
9586 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9587 mTempTotalCpuSystemTimeUs =
9588 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9589
9590 for (int i = 0; i < numPartialTimers; i++) {
9591 final StopwatchTimer timer = mPartialTimers.get(i);
9592
9593 // The system does not share any blame, as it is usually holding the wakelock
9594 // on behalf of an app.
9595 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9596 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9597 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9598
9599 if (DEBUG_ENERGY_CPU) {
9600 StringBuilder sb = new StringBuilder();
9601 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9602 .append(": u=");
9603 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9604 sb.append(" s=");
9605 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9606 Slog.d(TAG, sb.toString());
9607 }
9608
9609 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9610 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9611
9612 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009613 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009614
9615 mTempTotalCpuUserTimeUs -= userTimeUs;
9616 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9617 numWakelocks--;
9618 }
9619 }
9620
9621 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9622 // Anything left over is given to the system.
9623 if (DEBUG_ENERGY_CPU) {
9624 StringBuilder sb = new StringBuilder();
9625 sb.append(" Distributing lost time to system: u=");
9626 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9627 sb.append(" s=");
9628 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9629 Slog.d(TAG, sb.toString());
9630 }
9631
9632 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9633 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9634 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9635
9636 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009637 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9638 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009639 }
9640 }
9641
9642 // See if there is a difference in wakelocks between this collection and the last
9643 // collection.
9644 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9645 // No difference, so each timer is now considered for the next collection.
9646 for (int i = 0; i < numPartialTimers; i++) {
9647 mPartialTimers.get(i).mInList = true;
9648 }
9649 } else {
9650 // The lists are different, meaning we added (or removed a timer) since the last
9651 // collection.
9652 final int numLastPartialTimers = mLastPartialTimers.size();
9653 for (int i = 0; i < numLastPartialTimers; i++) {
9654 mLastPartialTimers.get(i).mInList = false;
9655 }
9656 mLastPartialTimers.clear();
9657
9658 // Mark the current timers as gone through a collection.
9659 for (int i = 0; i < numPartialTimers; i++) {
9660 final StopwatchTimer timer = mPartialTimers.get(i);
9661 timer.mInList = true;
9662 mLastPartialTimers.add(timer);
9663 }
9664 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009665 }
9666
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009667 boolean setChargingLocked(boolean charging) {
9668 if (mCharging != charging) {
9669 mCharging = charging;
9670 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009671 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009672 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009673 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009674 }
9675 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9676 return true;
9677 }
9678 return false;
9679 }
9680
Dianne Hackborn40c87252014-03-19 16:55:40 -07009681 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009682 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009683 boolean doWrite = false;
9684 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9685 m.arg1 = onBattery ? 1 : 0;
9686 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009687
Dianne Hackborn40c87252014-03-19 16:55:40 -07009688 final long uptime = mSecUptime * 1000;
9689 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009690 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009691 if (onBattery) {
9692 // We will reset our status if we are unplugging after the
9693 // battery was last full, or the level is at 100, or
9694 // we have gone through a significant charge (from a very low
9695 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009696 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009697 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009698 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009699 || (mDischargeCurrentLevel < 20 && level >= 80)
9700 || (getHighDischargeAmountSinceCharge() >= 200
9701 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009702 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009703 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009704 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9705 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009706 // Before we write, collect a snapshot of the final aggregated
9707 // stats to be reported in the next checkin. Only do this if we have
9708 // a sufficient amount of data to make it interesting.
9709 if (getLowDischargeAmountSinceCharge() >= 20) {
9710 final Parcel parcel = Parcel.obtain();
9711 writeSummaryToParcel(parcel, true);
9712 BackgroundThread.getHandler().post(new Runnable() {
9713 @Override public void run() {
9714 synchronized (mCheckinFile) {
9715 FileOutputStream stream = null;
9716 try {
9717 stream = mCheckinFile.startWrite();
9718 stream.write(parcel.marshall());
9719 stream.flush();
9720 FileUtils.sync(stream);
9721 stream.close();
9722 mCheckinFile.finishWrite(stream);
9723 } catch (IOException e) {
9724 Slog.w("BatteryStats",
9725 "Error writing checkin battery statistics", e);
9726 mCheckinFile.failWrite(stream);
9727 } finally {
9728 parcel.recycle();
9729 }
9730 }
9731 }
9732 });
9733 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009734 doWrite = true;
9735 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -08009736 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009737 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -08009738 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009739 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009740 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009741 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009742 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009743 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009744 if (mCharging) {
9745 setChargingLocked(false);
9746 }
9747 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009748 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009749 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009750 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009751 mDischargeStepTracker.clearTime();
9752 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009753 mInitStepMode = mCurStepMode;
9754 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009755 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009756 mHistoryCur.batteryLevel = (byte)level;
9757 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9758 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9759 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009760 if (reset) {
9761 mRecordingHistory = true;
9762 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9763 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009764 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009765 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009766 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009767 mDischargeScreenOnUnplugLevel = level;
9768 mDischargeScreenOffUnplugLevel = 0;
9769 } else {
9770 mDischargeScreenOnUnplugLevel = 0;
9771 mDischargeScreenOffUnplugLevel = level;
9772 }
9773 mDischargeAmountScreenOn = 0;
9774 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009775 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009776 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009777 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009778 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009779 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009780 mHistoryCur.batteryLevel = (byte)level;
9781 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9782 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9783 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009784 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009785 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009786 if (level < mDischargeUnplugLevel) {
9787 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9788 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9789 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009790 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9791 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009792 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009793 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009794 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009795 mInitStepMode = mCurStepMode;
9796 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009797 }
9798 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9799 if (mFile != null) {
9800 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 }
9802 }
9803 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009804
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009805 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9806 boolean reset) {
9807 mRecordingHistory = true;
9808 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009809 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9810 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009811 mHistoryCur);
9812 mHistoryCur.currentTime = 0;
9813 if (reset) {
9814 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9815 }
9816 }
9817
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009818 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9819 final long uptimeMs) {
9820 if (mRecordingHistory) {
9821 mHistoryCur.currentTime = currentTime;
9822 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9823 mHistoryCur);
9824 mHistoryCur.currentTime = 0;
9825 }
9826 }
9827
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009828 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9829 if (mRecordingHistory) {
9830 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009831 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9832 mHistoryCur);
9833 mHistoryCur.currentTime = 0;
9834 }
9835 }
9836
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009837 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009838 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009839 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009840 }
9841 }
9842
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009843 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009844 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009845
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009846 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -08009847 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009848 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009849 final long uptime = mClocks.uptimeMillis();
9850 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009851 if (!mHaveBatteryLevel) {
9852 mHaveBatteryLevel = true;
9853 // We start out assuming that the device is plugged in (not
9854 // on battery). If our first report is now that we are indeed
9855 // plugged in, then twiddle our state to correctly reflect that
9856 // since we won't be going through the full setOnBattery().
9857 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009858 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009859 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009860 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009861 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009862 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009863 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009864 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009865 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009866 mHistoryCur.batteryStatus = (byte)status;
9867 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009868 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009869 mMaxChargeStepLevel = mMinDischargeStepLevel =
9870 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009871 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009872 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9873 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9874 }
9875 int oldStatus = mHistoryCur.batteryStatus;
9876 if (onBattery) {
9877 mDischargeCurrentLevel = level;
9878 if (!mRecordingHistory) {
9879 mRecordingHistory = true;
9880 startRecordingHistory(elapsedRealtime, uptime, true);
9881 }
9882 } else if (level < 96) {
9883 if (!mRecordingHistory) {
9884 mRecordingHistory = true;
9885 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009886 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009887 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009888 mCurrentBatteryLevel = level;
9889 if (mDischargePlugLevel < 0) {
9890 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009891 }
Adam Lesinski926969b2016-04-28 17:31:12 -07009892
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009893 if (onBattery != mOnBattery) {
9894 mHistoryCur.batteryLevel = (byte)level;
9895 mHistoryCur.batteryStatus = (byte)status;
9896 mHistoryCur.batteryHealth = (byte)health;
9897 mHistoryCur.batteryPlugType = (byte)plugType;
9898 mHistoryCur.batteryTemperature = (short)temp;
9899 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009900 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9901 // Only record discharges
9902 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9903 mDischargeCounter.addCountLocked(chargeDiff);
9904 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9905 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009906 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009907 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009908 } else {
9909 boolean changed = false;
9910 if (mHistoryCur.batteryLevel != level) {
9911 mHistoryCur.batteryLevel = (byte)level;
9912 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009913
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009914 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9915 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009916 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009917 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009918 if (mHistoryCur.batteryStatus != status) {
9919 mHistoryCur.batteryStatus = (byte)status;
9920 changed = true;
9921 }
9922 if (mHistoryCur.batteryHealth != health) {
9923 mHistoryCur.batteryHealth = (byte)health;
9924 changed = true;
9925 }
9926 if (mHistoryCur.batteryPlugType != plugType) {
9927 mHistoryCur.batteryPlugType = (byte)plugType;
9928 changed = true;
9929 }
9930 if (temp >= (mHistoryCur.batteryTemperature+10)
9931 || temp <= (mHistoryCur.batteryTemperature-10)) {
9932 mHistoryCur.batteryTemperature = (short)temp;
9933 changed = true;
9934 }
9935 if (volt > (mHistoryCur.batteryVoltage+20)
9936 || volt < (mHistoryCur.batteryVoltage-20)) {
9937 mHistoryCur.batteryVoltage = (char)volt;
9938 changed = true;
9939 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009940 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
9941 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009942 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9943 // Only record discharges
9944 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9945 mDischargeCounter.addCountLocked(chargeDiff);
9946 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9947 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009948 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07009949 changed = true;
9950 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009951 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9952 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9953 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9954 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009955 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009956 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9957 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9958 modeBits, elapsedRealtime);
9959 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9960 modeBits, elapsedRealtime);
9961 mLastDischargeStepLevel = level;
9962 mMinDischargeStepLevel = level;
9963 mInitStepMode = mCurStepMode;
9964 mModStepMode = 0;
9965 }
9966 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009967 if (level >= 90) {
9968 // If the battery level is at least 90%, always consider the device to be
9969 // charging even if it happens to go down a level.
9970 changed |= setChargingLocked(true);
9971 mLastChargeStepLevel = level;
9972 } if (!mCharging) {
9973 if (mLastChargeStepLevel < level) {
9974 // We have not reporting that we are charging, but the level has now
9975 // gone up, so consider the state to be charging.
9976 changed |= setChargingLocked(true);
9977 mLastChargeStepLevel = level;
9978 }
9979 } else {
9980 if (mLastChargeStepLevel > level) {
9981 // We had reported that the device was charging, but here we are with
9982 // power connected and the level going down. Looks like the current
9983 // power supplied isn't enough, so consider the device to now be
9984 // discharging.
9985 changed |= setChargingLocked(false);
9986 mLastChargeStepLevel = level;
9987 }
9988 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009989 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9990 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9991 modeBits, elapsedRealtime);
9992 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9993 modeBits, elapsedRealtime);
9994 mLastChargeStepLevel = level;
9995 mMaxChargeStepLevel = level;
9996 mInitStepMode = mCurStepMode;
9997 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009998 }
9999 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010000 if (changed) {
10001 addHistoryRecordLocked(elapsedRealtime, uptime);
10002 }
Evan Millarc64edde2009-04-18 12:26:32 -070010003 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010004 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10005 // We don't record history while we are plugged in and fully charged.
10006 // The next time we are unplugged, history will be cleared.
10007 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010008 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010009
10010 if (differsByMoreThan(chargeFullUAh, mLastChargeFullUAh, 100)) {
10011 mLastChargeFullUAh = chargeFullUAh;
10012 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ESTIMATED_BATTERY_CAP,
10013 "", chargeFullUAh / 1000);
10014 }
10015 }
10016
10017 private static boolean differsByMoreThan(int left, int right, int diff) {
10018 return Math.abs(left - right) > diff;
Adam Lesinski33dac552015-03-09 15:24:48 -070010019 }
10020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 public long getAwakeTimeBattery() {
10022 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
10023 }
10024
10025 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080010026 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027 }
10028
10029 @Override
10030 public long computeUptime(long curTime, int which) {
10031 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010032 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010033 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010034 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010035 }
10036 return 0;
10037 }
10038
10039 @Override
10040 public long computeRealtime(long curTime, int which) {
10041 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010042 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010044 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 }
10046 return 0;
10047 }
10048
10049 @Override
10050 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010051 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 }
10053
10054 @Override
10055 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010056 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010057 }
10058
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010059 @Override
10060 public long computeBatteryScreenOffUptime(long curTime, int which) {
10061 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
10062 }
10063
10064 @Override
10065 public long computeBatteryScreenOffRealtime(long curTime, int which) {
10066 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 }
10068
Dianne Hackborn260c5022014-04-29 11:23:16 -070010069 private long computeTimePerLevel(long[] steps, int numSteps) {
10070 // For now we'll do a simple average across all steps.
10071 if (numSteps <= 0) {
10072 return -1;
10073 }
10074 long total = 0;
10075 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010076 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010077 }
10078 return total / numSteps;
10079 /*
10080 long[] buckets = new long[numSteps];
10081 int numBuckets = 0;
10082 int numToAverage = 4;
10083 int i = 0;
10084 while (i < numSteps) {
10085 long totalTime = 0;
10086 int num = 0;
10087 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010088 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010089 num++;
10090 }
10091 buckets[numBuckets] = totalTime / num;
10092 numBuckets++;
10093 numToAverage *= 2;
10094 i += num;
10095 }
10096 if (numBuckets < 1) {
10097 return -1;
10098 }
10099 long averageTime = buckets[numBuckets-1];
10100 for (i=numBuckets-2; i>=0; i--) {
10101 averageTime = (averageTime + buckets[i]) / 2;
10102 }
10103 return averageTime;
10104 */
10105 }
10106
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010107 @Override
10108 public long computeBatteryTimeRemaining(long curTime) {
10109 if (!mOnBattery) {
10110 return -1;
10111 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010112 /* Simple implementation just looks at the average discharge per level across the
10113 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010114 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
10115 if (discharge < 2) {
10116 return -1;
10117 }
10118 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
10119 if (duration < 1000*1000) {
10120 return -1;
10121 }
10122 long usPerLevel = duration/discharge;
10123 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010124 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010125 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010126 return -1;
10127 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010128 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010129 if (msPerLevel <= 0) {
10130 return -1;
10131 }
10132 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010133 }
10134
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010135 @Override
10136 public LevelStepTracker getDischargeLevelStepTracker() {
10137 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010138 }
10139
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010140 @Override
10141 public LevelStepTracker getDailyDischargeLevelStepTracker() {
10142 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010143 }
10144
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010145 @Override
10146 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010147 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010148 // Not yet working.
10149 return -1;
10150 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010151 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010152 int curLevel = mCurrentBatteryLevel;
10153 int plugLevel = mDischargePlugLevel;
10154 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
10155 return -1;
10156 }
10157 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
10158 if (duration < 1000*1000) {
10159 return -1;
10160 }
10161 long usPerLevel = duration/(curLevel-plugLevel);
10162 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070010163 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010164 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010165 return -1;
10166 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010167 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010168 if (msPerLevel <= 0) {
10169 return -1;
10170 }
10171 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010172 }
10173
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010174 @Override
10175 public LevelStepTracker getChargeLevelStepTracker() {
10176 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010177 }
10178
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010179 @Override
10180 public LevelStepTracker getDailyChargeLevelStepTracker() {
10181 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010182 }
10183
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010184 @Override
10185 public ArrayList<PackageChange> getDailyPackageChanges() {
10186 return mDailyPackageChanges;
10187 }
10188
Joe Onoratoe1acd632016-02-23 13:25:10 -080010189 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010190 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 }
10192
10193 @Override
10194 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010195 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010196 }
10197
10198 @Override
10199 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010200 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010201 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070010202
The Android Open Source Project10592532009-03-18 17:39:46 -070010203 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010204 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010205 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010206 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010207 }
10208 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010209
Evan Millar633a1742009-04-02 16:36:33 -070010210 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010211 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010212 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010213
The Android Open Source Project10592532009-03-18 17:39:46 -070010214 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010215 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010216 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010217 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010218 }
10219 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010220
Evan Millar633a1742009-04-02 16:36:33 -070010221 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010222 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224
Amith Yamasanie43530a2009-08-21 13:11:37 -070010225 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010226 public int getLowDischargeAmountSinceCharge() {
10227 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010228 int val = mLowDischargeAmountSinceCharge;
10229 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10230 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
10231 }
10232 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010233 }
10234 }
10235
10236 @Override
10237 public int getHighDischargeAmountSinceCharge() {
10238 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010239 int val = mHighDischargeAmountSinceCharge;
10240 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10241 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
10242 }
10243 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010244 }
10245 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010246
10247 @Override
10248 public int getDischargeAmount(int which) {
10249 int dischargeAmount = which == STATS_SINCE_CHARGED
10250 ? getHighDischargeAmountSinceCharge()
10251 : (getDischargeStartLevel() - getDischargeCurrentLevel());
10252 if (dischargeAmount < 0) {
10253 dischargeAmount = 0;
10254 }
10255 return dischargeAmount;
10256 }
10257
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010258 public int getDischargeAmountScreenOn() {
10259 synchronized(this) {
10260 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010261 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010262 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10263 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10264 }
10265 return val;
10266 }
10267 }
10268
10269 public int getDischargeAmountScreenOnSinceCharge() {
10270 synchronized(this) {
10271 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010272 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010273 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10274 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10275 }
10276 return val;
10277 }
10278 }
10279
10280 public int getDischargeAmountScreenOff() {
10281 synchronized(this) {
10282 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010283 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010284 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10285 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10286 }
10287 return val;
10288 }
10289 }
10290
10291 public int getDischargeAmountScreenOffSinceCharge() {
10292 synchronized(this) {
10293 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010294 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010295 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10296 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10297 }
10298 return val;
10299 }
10300 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 /**
10303 * Retrieve the statistics object for a particular uid, creating if needed.
10304 */
10305 public Uid getUidStatsLocked(int uid) {
10306 Uid u = mUidStats.get(uid);
10307 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080010308 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 mUidStats.put(uid, u);
10310 }
10311 return u;
10312 }
10313
10314 /**
10315 * Remove the statistics object for a particular uid.
10316 */
10317 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070010318 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 mUidStats.remove(uid);
10320 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070010321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010322 /**
10323 * Retrieve the statistics object for a particular process, creating
10324 * if needed.
10325 */
10326 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010327 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 Uid u = getUidStatsLocked(uid);
10329 return u.getProcessStatsLocked(name);
10330 }
10331
10332 /**
10333 * Retrieve the statistics object for a particular process, creating
10334 * if needed.
10335 */
10336 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010337 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 Uid u = getUidStatsLocked(uid);
10339 return u.getPackageStatsLocked(pkg);
10340 }
10341
10342 /**
10343 * Retrieve the statistics object for a particular service, creating
10344 * if needed.
10345 */
10346 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010347 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 Uid u = getUidStatsLocked(uid);
10349 return u.getServiceStatsLocked(pkg, name);
10350 }
10351
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010352 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010353 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010354 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010355 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010356 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010357
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010358 Parcel mPendingWrite = null;
10359 final ReentrantLock mWriteLock = new ReentrantLock();
10360
10361 public void writeAsyncLocked() {
10362 writeLocked(false);
10363 }
10364
10365 public void writeSyncLocked() {
10366 writeLocked(true);
10367 }
10368
10369 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010370 if (mFile == null) {
10371 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 return;
10373 }
10374
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010375 if (mShuttingDown) {
10376 return;
10377 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010378
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010379 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010380 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080010381 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010382
10383 if (mPendingWrite != null) {
10384 mPendingWrite.recycle();
10385 }
10386 mPendingWrite = out;
10387
10388 if (sync) {
10389 commitPendingDataToDisk();
10390 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010391 BackgroundThread.getHandler().post(new Runnable() {
10392 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010393 commitPendingDataToDisk();
10394 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010395 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010396 }
10397 }
10398
10399 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010400 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010401 synchronized (this) {
10402 next = mPendingWrite;
10403 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010404 if (next == null) {
10405 return;
10406 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010407 }
10408
Amith Yamasanid2450862017-02-07 15:58:24 -080010409 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010411 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010412 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070010414 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010415 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010416 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010418 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010419 mFile.rollback();
10420 } finally {
10421 next.recycle();
10422 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070010423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 }
10425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010427 if (mDailyFile != null) {
10428 readDailyStatsLocked();
10429 }
10430
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010431 if (mFile == null) {
10432 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 return;
10434 }
10435
10436 mUidStats.clear();
10437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010438 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010439 File file = mFile.chooseForRead();
10440 if (!file.exists()) {
10441 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010443 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010445 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010446 Parcel in = Parcel.obtain();
10447 in.unmarshall(raw, 0, raw.length);
10448 in.setDataPosition(0);
10449 stream.close();
10450
10451 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080010452 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010453 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010454 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010456
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010457 mEndPlatformVersion = Build.ID;
10458
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010459 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010460 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080010461 final long elapsedRealtime = mClocks.elapsedRealtime();
10462 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010463 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010464 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010465 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010466 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
10467 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010468 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010469
10470 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010471 }
10472
10473 public int describeContents() {
10474 return 0;
10475 }
10476
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010477 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070010478 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010479
10480 mHistoryBuffer.setDataSize(0);
10481 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010482 mHistoryTagPool.clear();
10483 mNextHistoryTagIdx = 0;
10484 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010485
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010486 int numTags = in.readInt();
10487 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080010488 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010489 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010490 if (str == null) {
10491 throw new ParcelFormatException("null history tag string");
10492 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010493 int uid = in.readInt();
10494 HistoryTag tag = new HistoryTag();
10495 tag.string = str;
10496 tag.uid = uid;
10497 tag.poolIdx = idx;
10498 mHistoryTagPool.put(tag, idx);
10499 if (idx >= mNextHistoryTagIdx) {
10500 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010501 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010502 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010503 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010504
10505 int bufSize = in.readInt();
10506 int curPos = in.dataPosition();
10507 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010508 throw new ParcelFormatException("File corrupt: history data buffer too large " +
10509 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010510 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010511 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
10512 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010513 } else {
10514 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
10515 + " bytes at " + curPos);
10516 mHistoryBuffer.appendFrom(in, curPos, bufSize);
10517 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010518 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010519
Dianne Hackbornae384452011-06-28 12:33:48 -070010520 if (andOldHistory) {
10521 readOldHistory(in);
10522 }
10523
10524 if (DEBUG_HISTORY) {
10525 StringBuilder sb = new StringBuilder(128);
10526 sb.append("****************** OLD mHistoryBaseTime: ");
10527 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10528 Slog.i(TAG, sb.toString());
10529 }
10530 mHistoryBaseTime = historyBaseTime;
10531 if (DEBUG_HISTORY) {
10532 StringBuilder sb = new StringBuilder(128);
10533 sb.append("****************** NEW mHistoryBaseTime: ");
10534 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10535 Slog.i(TAG, sb.toString());
10536 }
10537
10538 // We are just arbitrarily going to insert 1 minute from the sample of
10539 // the last run until samples in this run.
10540 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080010541 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010542 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070010543 if (DEBUG_HISTORY) {
10544 StringBuilder sb = new StringBuilder(128);
10545 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
10546 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10547 Slog.i(TAG, sb.toString());
10548 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070010549 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010550 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010551
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010552 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010553 if (!USE_OLD_HISTORY) {
10554 return;
10555 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010556 mHistory = mHistoryEnd = mHistoryCache = null;
10557 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010558 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010559 HistoryItem rec = new HistoryItem(time, in);
10560 addHistoryRecordLocked(rec);
10561 }
10562 }
10563
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010564 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010565 if (DEBUG_HISTORY) {
10566 StringBuilder sb = new StringBuilder(128);
10567 sb.append("****************** WRITING mHistoryBaseTime: ");
10568 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010569 sb.append(" mLastHistoryElapsedRealtime: ");
10570 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010571 Slog.i(TAG, sb.toString());
10572 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010573 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010574 if (!inclData) {
10575 out.writeInt(0);
10576 out.writeInt(0);
10577 return;
10578 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010579 out.writeInt(mHistoryTagPool.size());
10580 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10581 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010582 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010583 out.writeString(tag.string);
10584 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010585 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010586 out.writeInt(mHistoryBuffer.dataSize());
10587 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10588 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10589 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010590
10591 if (andOldHistory) {
10592 writeOldHistory(out);
10593 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010594 }
10595
10596 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010597 if (!USE_OLD_HISTORY) {
10598 return;
10599 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010600 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010601 while (rec != null) {
10602 if (rec.time >= 0) rec.writeToParcel(out, 0);
10603 rec = rec.next;
10604 }
10605 out.writeLong(-1);
10606 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010607
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010608 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 final int version = in.readInt();
10610 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010611 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 + ", expected " + VERSION + "; erasing old stats");
10613 return;
10614 }
10615
Dianne Hackbornae384452011-06-28 12:33:48 -070010616 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010621 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010622 mStartPlatformVersion = in.readString();
10623 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010624 mOnBatteryTimeBase.readSummaryFromParcel(in);
10625 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010626 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010627 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010628 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010629 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010630 mEstimatedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010631 mLowDischargeAmountSinceCharge = in.readInt();
10632 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010633 mDischargeAmountScreenOnSinceCharge = in.readInt();
10634 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010635 mDischargeStepTracker.readFromParcel(in);
10636 mChargeStepTracker.readFromParcel(in);
10637 mDailyDischargeStepTracker.readFromParcel(in);
10638 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010639 mDischargeCounter.readSummaryFromParcelLocked(in);
10640 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010641 int NPKG = in.readInt();
10642 if (NPKG > 0) {
10643 mDailyPackageChanges = new ArrayList<>(NPKG);
10644 while (NPKG > 0) {
10645 NPKG--;
10646 PackageChange pc = new PackageChange();
10647 pc.mPackageName = in.readString();
10648 pc.mUpdate = in.readInt() != 0;
10649 pc.mVersionCode = in.readInt();
10650 mDailyPackageChanges.add(pc);
10651 }
10652 } else {
10653 mDailyPackageChanges = null;
10654 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010655 mDailyStartTime = in.readLong();
10656 mNextMinDailyDeadline = in.readLong();
10657 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010660
Jeff Browne95c3cd2014-05-02 16:59:26 -070010661 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010663 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10664 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10665 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010666 mInteractive = false;
10667 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010668 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010669 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010670 mLongestLightIdleTime = in.readLong();
10671 mLongestFullIdleTime = in.readLong();
10672 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10673 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10674 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010675 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010677 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010678 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10679 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010680 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010681 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10682 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10683 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010684 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010685 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10686 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010687 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010688 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010689 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010690 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010691 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010692 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10693 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010694 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010695 mWifiOn = false;
10696 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010697 mGlobalWifiRunning = false;
10698 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010699 for (int i=0; i<NUM_WIFI_STATES; i++) {
10700 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10701 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010702 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10703 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10704 }
10705 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10706 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10707 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010708 mWifiActivity.readSummaryFromParcel(in);
10709 mBluetoothActivity.readSummaryFromParcel(in);
10710 mModemActivity.readSummaryFromParcel(in);
10711 mHasWifiReporting = in.readInt() != 0;
10712 mHasBluetoothReporting = in.readInt() != 0;
10713 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010714
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010715 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010716 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010717 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010718 mCameraOnNesting = 0;
10719 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010720 mBluetoothScanNesting = 0;
10721 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722
Evan Millarc64edde2009-04-18 12:26:32 -070010723 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010724 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010725 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010726 }
Evan Millarc64edde2009-04-18 12:26:32 -070010727 for (int ikw = 0; ikw < NKW; ikw++) {
10728 if (in.readInt() != 0) {
10729 String kwltName = in.readString();
10730 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10731 }
10732 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010733
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010734 int NWR = in.readInt();
10735 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010736 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010737 }
10738 for (int iwr = 0; iwr < NWR; iwr++) {
10739 if (in.readInt() != 0) {
10740 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010741 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010742 }
10743 }
10744
James Carr3a226052016-07-01 14:49:52 -070010745 int NMS = in.readInt();
10746 for (int ims = 0; ims < NMS; ims++) {
10747 if (in.readInt() != 0) {
10748 long kmstName = in.readLong();
10749 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
10750 }
10751 }
10752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010754 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010755 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 for (int iu = 0; iu < NU; iu++) {
10758 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010759 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760 mUidStats.put(uid, u);
10761
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010762 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010763 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010764 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010765 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010766 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010767 if (in.readInt() != 0) {
10768 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10769 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010770 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010771 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010772 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010773 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010774 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10775 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10776 if (in.readInt() != 0) {
10777 u.makeWifiBatchedScanBin(i, null);
10778 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10779 }
10780 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010781 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010782 if (in.readInt() != 0) {
10783 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10784 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010785 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010786 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010787 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010788 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010789 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10790 }
10791 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010792 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10793 }
10794 if (in.readInt() != 0) {
10795 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10796 }
10797 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010798 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10799 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010800 if (in.readInt() != 0) {
10801 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10802 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010803 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010804 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10805 if (in.readInt() != 0) {
10806 u.makeProcessState(i, null);
10807 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10808 }
10809 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010810 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010811 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010812 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010813
Dianne Hackborn617f8772009-03-31 15:04:46 -070010814 if (in.readInt() != 0) {
10815 if (u.mUserActivityCounters == null) {
10816 u.initUserActivityLocked();
10817 }
10818 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10819 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10820 }
10821 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010822
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010823 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010824 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010825 u.initNetworkActivityLocked();
10826 }
10827 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010828 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10829 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010830 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010831 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10832 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010833 }
10834
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010835 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10836 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010837 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010838
Adam Lesinski6832f392015-09-05 18:05:40 -070010839 if (in.readInt() != 0) {
10840 final int numClusters = in.readInt();
10841 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10842 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010843 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010844
10845 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10846 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010847 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010848 final int NSB = in.readInt();
10849 if (mPowerProfile != null &&
10850 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10851 throw new ParcelFormatException("File corrupt: too many speed bins " +
10852 NSB);
10853 }
10854
Adam Lesinski6832f392015-09-05 18:05:40 -070010855 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10856 for (int speed = 0; speed < NSB; speed++) {
10857 if (in.readInt() != 0) {
10858 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10859 mOnBatteryTimeBase);
10860 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10861 }
10862 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010863 } else {
10864 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010865 }
10866 }
10867 } else {
10868 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010869 }
10870
Adam Lesinski5f056f62016-07-14 16:56:08 -070010871 if (in.readInt() != 0) {
10872 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10873 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
10874 } else {
10875 u.mMobileRadioApWakeupCount = null;
10876 }
10877
10878 if (in.readInt() != 0) {
10879 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10880 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
10881 } else {
10882 u.mWifiRadioApWakeupCount = null;
10883 }
10884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070010886 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010887 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 for (int iw = 0; iw < NW; iw++) {
10890 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010891 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 }
10893
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010894 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070010895 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010896 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010897 }
10898 for (int is = 0; is < NS; is++) {
10899 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010900 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010901 }
10902
10903 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070010904 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010905 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010906 }
10907 for (int ij = 0; ij < NJ; ij++) {
10908 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010909 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010910 }
10911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010913 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010914 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916 for (int is = 0; is < NP; is++) {
10917 int seNumber = in.readInt();
10918 if (in.readInt() != 0) {
10919 u.getSensorTimerLocked(seNumber, true)
10920 .readSummaryFromParcelLocked(in);
10921 }
10922 }
10923
10924 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010925 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010926 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928 for (int ip = 0; ip < NP; ip++) {
10929 String procName = in.readString();
10930 Uid.Proc p = u.getProcessStatsLocked(procName);
10931 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010933 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010935 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10936 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010937 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010938 }
10939
10940 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010941 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010942 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010944 for (int ip = 0; ip < NP; ip++) {
10945 String pkgName = in.readString();
10946 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010947 final int NWA = in.readInt();
10948 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010949 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010950 }
10951 p.mWakeupAlarms.clear();
10952 for (int iwa=0; iwa<NWA; iwa++) {
10953 String tag = in.readString();
10954 Counter c = new Counter(mOnBatteryTimeBase);
10955 c.readSummaryFromParcelLocked(in);
10956 p.mWakeupAlarms.put(tag, c);
10957 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010958 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010959 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010960 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 for (int is = 0; is < NS; is++) {
10963 String servName = in.readString();
10964 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10965 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010966 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010967 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 }
10969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010970 }
10971 }
10972
10973 /**
10974 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10975 * disk. This format does not allow a lossless round-trip.
10976 *
10977 * @param out the Parcel to be written to.
10978 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010979 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010980 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010981
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010982 // Pull the clock time. This may update the time and make a new history entry
10983 // if we had originally pulled a time before the RTC was set.
10984 long startClockTime = getStartClockTime();
10985
Joe Onoratoabded112016-02-08 16:49:39 -080010986 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10987 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010988
10989 out.writeInt(VERSION);
10990
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010991 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010993 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010994 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010995 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010996 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010997 out.writeString(mStartPlatformVersion);
10998 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010999 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11000 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011001 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011002 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011003 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011004 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011005 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011006 out.writeInt(getLowDischargeAmountSinceCharge());
11007 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011008 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11009 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011010 mDischargeStepTracker.writeToParcel(out);
11011 mChargeStepTracker.writeToParcel(out);
11012 mDailyDischargeStepTracker.writeToParcel(out);
11013 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011014 mDischargeCounter.writeSummaryFromParcelLocked(out);
11015 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011016 if (mDailyPackageChanges != null) {
11017 final int NPKG = mDailyPackageChanges.size();
11018 out.writeInt(NPKG);
11019 for (int i=0; i<NPKG; i++) {
11020 PackageChange pc = mDailyPackageChanges.get(i);
11021 out.writeString(pc.mPackageName);
11022 out.writeInt(pc.mUpdate ? 1 : 0);
11023 out.writeInt(pc.mVersionCode);
11024 }
11025 } else {
11026 out.writeInt(0);
11027 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011028 out.writeLong(mDailyStartTime);
11029 out.writeLong(mNextMinDailyDeadline);
11030 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011031
11032 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011033 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011034 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011035 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011036 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011037 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011038 out.writeLong(mLongestLightIdleTime);
11039 out.writeLong(mLongestFullIdleTime);
11040 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11041 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11042 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011043 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011044 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011045 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011046 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011047 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011048 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011049 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011050 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011051 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011052 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011053 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11054 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011055 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011056 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11057 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011058 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011059 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11060 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011061 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11062 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011063 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011064 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011065 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011066 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11067 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11068 }
11069 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11070 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11071 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011072 mWifiActivity.writeSummaryToParcel(out);
11073 mBluetoothActivity.writeSummaryToParcel(out);
11074 mModemActivity.writeSummaryToParcel(out);
11075 out.writeInt(mHasWifiReporting ? 1 : 0);
11076 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11077 out.writeInt(mHasModemReporting ? 1 : 0);
11078
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011079 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011080 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011081 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011082 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011083
Evan Millarc64edde2009-04-18 12:26:32 -070011084 out.writeInt(mKernelWakelockStats.size());
11085 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11086 Timer kwlt = ent.getValue();
11087 if (kwlt != null) {
11088 out.writeInt(1);
11089 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011090 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11091 } else {
11092 out.writeInt(0);
11093 }
11094 }
11095
11096 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011097 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11098 SamplingTimer timer = ent.getValue();
11099 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011100 out.writeInt(1);
11101 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011102 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070011103 } else {
11104 out.writeInt(0);
11105 }
11106 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011107
James Carr3a226052016-07-01 14:49:52 -070011108 out.writeInt(mKernelMemoryStats.size());
11109 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11110 Timer kmt = mKernelMemoryStats.valueAt(i);
11111 if (kmt != null) {
11112 out.writeInt(1);
11113 out.writeLong(mKernelMemoryStats.keyAt(i));
11114 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11115 } else {
11116 out.writeInt(0);
11117 }
11118 }
11119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 final int NU = mUidStats.size();
11121 out.writeInt(NU);
11122 for (int iu = 0; iu < NU; iu++) {
11123 out.writeInt(mUidStats.keyAt(iu));
11124 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011125
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011126 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011127 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011128 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011129 } else {
11130 out.writeInt(0);
11131 }
11132 if (u.mFullWifiLockTimer != null) {
11133 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011134 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011135 } else {
11136 out.writeInt(0);
11137 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011138 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011139 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011140 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011141 } else {
11142 out.writeInt(0);
11143 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011144 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11145 if (u.mWifiBatchedScanTimer[i] != null) {
11146 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011147 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070011148 } else {
11149 out.writeInt(0);
11150 }
11151 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011152 if (u.mWifiMulticastTimer != null) {
11153 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011154 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011155 } else {
11156 out.writeInt(0);
11157 }
11158 if (u.mAudioTurnedOnTimer != null) {
11159 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011160 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011161 } else {
11162 out.writeInt(0);
11163 }
11164 if (u.mVideoTurnedOnTimer != null) {
11165 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011166 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011167 } else {
11168 out.writeInt(0);
11169 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011170 if (u.mFlashlightTurnedOnTimer != null) {
11171 out.writeInt(1);
11172 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11173 } else {
11174 out.writeInt(0);
11175 }
11176 if (u.mCameraTurnedOnTimer != null) {
11177 out.writeInt(1);
11178 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11179 } else {
11180 out.writeInt(0);
11181 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011182 if (u.mForegroundActivityTimer != null) {
11183 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011184 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011185 } else {
11186 out.writeInt(0);
11187 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011188 if (u.mBluetoothScanTimer != null) {
11189 out.writeInt(1);
11190 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11191 } else {
11192 out.writeInt(0);
11193 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070011194 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11195 if (u.mProcessStateTimer[i] != null) {
11196 out.writeInt(1);
11197 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11198 } else {
11199 out.writeInt(0);
11200 }
11201 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011202 if (u.mVibratorOnTimer != null) {
11203 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011204 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011205 } else {
11206 out.writeInt(0);
11207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011208
Dianne Hackborn617f8772009-03-31 15:04:46 -070011209 if (u.mUserActivityCounters == null) {
11210 out.writeInt(0);
11211 } else {
11212 out.writeInt(1);
11213 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11214 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
11215 }
11216 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011217
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011218 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011219 out.writeInt(0);
11220 } else {
11221 out.writeInt(1);
11222 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011223 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11224 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011225 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011226 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
11227 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011228 }
11229
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011230 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
11231 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070011232 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011233
Adam Lesinski6832f392015-09-05 18:05:40 -070011234 if (u.mCpuClusterSpeed != null) {
11235 out.writeInt(1);
11236 out.writeInt(u.mCpuClusterSpeed.length);
11237 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
11238 if (cpuSpeeds != null) {
11239 out.writeInt(1);
11240 out.writeInt(cpuSpeeds.length);
11241 for (LongSamplingCounter c : cpuSpeeds) {
11242 if (c != null) {
11243 out.writeInt(1);
11244 c.writeSummaryFromParcelLocked(out);
11245 } else {
11246 out.writeInt(0);
11247 }
11248 }
11249 } else {
11250 out.writeInt(0);
11251 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011252 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011253 } else {
11254 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011255 }
11256
Adam Lesinski5f056f62016-07-14 16:56:08 -070011257 if (u.mMobileRadioApWakeupCount != null) {
11258 out.writeInt(1);
11259 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11260 } else {
11261 out.writeInt(0);
11262 }
11263
11264 if (u.mWifiRadioApWakeupCount != null) {
11265 out.writeInt(1);
11266 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11267 } else {
11268 out.writeInt(0);
11269 }
11270
Dianne Hackbornd953c532014-08-16 18:17:38 -070011271 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
11272 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011273 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011274 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011275 out.writeString(wakeStats.keyAt(iw));
11276 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011277 if (wl.mTimerFull != null) {
11278 out.writeInt(1);
11279 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11280 } else {
11281 out.writeInt(0);
11282 }
11283 if (wl.mTimerPartial != null) {
11284 out.writeInt(1);
11285 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11286 } else {
11287 out.writeInt(0);
11288 }
11289 if (wl.mTimerWindow != null) {
11290 out.writeInt(1);
11291 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11292 } else {
11293 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011294 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011295 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070011296 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011297 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070011298 } else {
11299 out.writeInt(0);
11300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011301 }
11302
Dianne Hackbornd953c532014-08-16 18:17:38 -070011303 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
11304 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011305 out.writeInt(NS);
11306 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011307 out.writeString(syncStats.keyAt(is));
11308 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011309 }
11310
Dianne Hackbornd953c532014-08-16 18:17:38 -070011311 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
11312 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011313 out.writeInt(NJ);
11314 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011315 out.writeString(jobStats.keyAt(ij));
11316 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011317 }
11318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011319 int NSE = u.mSensorStats.size();
11320 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011321 for (int ise=0; ise<NSE; ise++) {
11322 out.writeInt(u.mSensorStats.keyAt(ise));
11323 Uid.Sensor se = u.mSensorStats.valueAt(ise);
11324 if (se.mTimer != null) {
11325 out.writeInt(1);
11326 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11327 } else {
11328 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011329 }
11330 }
11331
11332 int NP = u.mProcessStats.size();
11333 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011334 for (int ip=0; ip<NP; ip++) {
11335 out.writeString(u.mProcessStats.keyAt(ip));
11336 Uid.Proc ps = u.mProcessStats.valueAt(ip);
11337 out.writeLong(ps.mUserTime);
11338 out.writeLong(ps.mSystemTime);
11339 out.writeLong(ps.mForegroundTime);
11340 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011341 out.writeInt(ps.mNumCrashes);
11342 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011343 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011344 }
11345
11346 NP = u.mPackageStats.size();
11347 out.writeInt(NP);
11348 if (NP > 0) {
11349 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
11350 : u.mPackageStats.entrySet()) {
11351 out.writeString(ent.getKey());
11352 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011353 final int NWA = ps.mWakeupAlarms.size();
11354 out.writeInt(NWA);
11355 for (int iwa=0; iwa<NWA; iwa++) {
11356 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
11357 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
11358 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011359 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011360 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011361 for (int is=0; is<NS; is++) {
11362 out.writeString(ps.mServiceStats.keyAt(is));
11363 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
11364 long time = ss.getStartTimeToNowLocked(
11365 mOnBatteryTimeBase.getUptime(NOW_SYS));
11366 out.writeLong(time);
11367 out.writeInt(ss.mStarts);
11368 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369 }
11370 }
11371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 }
11373 }
11374
11375 public void readFromParcel(Parcel in) {
11376 readFromParcelLocked(in);
11377 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011379 void readFromParcelLocked(Parcel in) {
11380 int magic = in.readInt();
11381 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011382 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011383 }
11384
Dianne Hackbornae384452011-06-28 12:33:48 -070011385 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011388 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011389 mStartPlatformVersion = in.readString();
11390 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011391 mUptime = in.readLong();
11392 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393 mRealtime = in.readLong();
11394 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011395 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011396 mEstimatedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011398 mOnBatteryTimeBase.readFromParcel(in);
11399 mOnBatteryScreenOffTimeBase.readFromParcel(in);
11400
Jeff Browne95c3cd2014-05-02 16:59:26 -070011401 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080011402 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011403 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011404 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
11405 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011406 }
Dianne Hackborn29325132014-05-21 15:01:03 -070011407 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011408 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011409 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011410 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
11411 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011412 mLongestLightIdleTime = in.readLong();
11413 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080011414 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
11415 mOnBatteryTimeBase, in);
11416 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
11417 mOnBatteryTimeBase, in);
11418 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
11419 mOnBatteryTimeBase, in);
11420 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
11421 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011422 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011423 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011424 null, mOnBatteryTimeBase, in);
11425 }
Joe Onoratoabded112016-02-08 16:49:39 -080011426 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
11427 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011428 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011429 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011430 null, mOnBatteryTimeBase, in);
11431 }
11432 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11433 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11434 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11435 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011436 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080011437 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
11438 mOnBatteryTimeBase, in);
11439 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
11440 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011441 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011442 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
11443 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011444 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011445 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011446 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011447 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011448 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
11449 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011450 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011451 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011452 null, mOnBatteryTimeBase, in);
11453 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011454 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011455 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011456 null, mOnBatteryTimeBase, in);
11457 }
11458 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011459 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011460 null, mOnBatteryTimeBase, in);
11461 }
Adam Lesinski33dac552015-03-09 15:24:48 -070011462
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011463 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11464 NUM_WIFI_TX_LEVELS, in);
11465 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11466 NUM_BT_TX_LEVELS, in);
11467 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11468 ModemActivityInfo.TX_POWER_LEVELS, in);
11469 mHasWifiReporting = in.readInt() != 0;
11470 mHasBluetoothReporting = in.readInt() != 0;
11471 mHasModemReporting = in.readInt() != 0;
11472
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011473 mNumConnectivityChange = in.readInt();
11474 mLoadedNumConnectivityChange = in.readInt();
11475 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011476 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011477 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011478 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011479 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011480 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011481 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011482 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011483 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011484 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011485 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011486 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011487 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011488 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011489 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011490 mLowDischargeAmountSinceCharge = in.readInt();
11491 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011492 mDischargeAmountScreenOn = in.readInt();
11493 mDischargeAmountScreenOnSinceCharge = in.readInt();
11494 mDischargeAmountScreenOff = in.readInt();
11495 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011496 mDischargeStepTracker.readFromParcel(in);
11497 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011498 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
11499 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011500 mLastWriteTime = in.readLong();
11501
Evan Millarc64edde2009-04-18 12:26:32 -070011502 mKernelWakelockStats.clear();
11503 int NKW = in.readInt();
11504 for (int ikw = 0; ikw < NKW; ikw++) {
11505 if (in.readInt() != 0) {
11506 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011507 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070011508 mKernelWakelockStats.put(wakelockName, kwlt);
11509 }
11510 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011511
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011512 mWakeupReasonStats.clear();
11513 int NWR = in.readInt();
11514 for (int iwr = 0; iwr < NWR; iwr++) {
11515 if (in.readInt() != 0) {
11516 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011517 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011518 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011519 }
11520 }
11521
James Carr3a226052016-07-01 14:49:52 -070011522 mKernelMemoryStats.clear();
11523 int nmt = in.readInt();
11524 for (int imt = 0; imt < nmt; imt++) {
11525 if (in.readInt() != 0) {
11526 Long bucket = in.readLong();
11527 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
11528 mKernelMemoryStats.put(bucket, kmt);
11529 }
11530 }
11531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 mPartialTimers.clear();
11533 mFullTimers.clear();
11534 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011535 mWifiRunningTimers.clear();
11536 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070011537 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070011538 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011539 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011540 mAudioTurnedOnTimers.clear();
11541 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011542 mFlashlightTurnedOnTimers.clear();
11543 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011544
11545 int numUids = in.readInt();
11546 mUidStats.clear();
11547 for (int i = 0; i < numUids; i++) {
11548 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011549 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011550 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011551 mUidStats.append(uid, u);
11552 }
11553 }
11554
11555 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011556 writeToParcelLocked(out, true, flags);
11557 }
11558
11559 public void writeToParcelWithoutUids(Parcel out, int flags) {
11560 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011562
11563 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011564 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011565 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011566 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011567
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011568 // Pull the clock time. This may update the time and make a new history entry
11569 // if we had originally pulled a time before the RTC was set.
11570 long startClockTime = getStartClockTime();
11571
Joe Onoratoabded112016-02-08 16:49:39 -080011572 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11573 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011574 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11575 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011577 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011578
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011579 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011582 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011583 out.writeString(mStartPlatformVersion);
11584 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011585 out.writeLong(mUptime);
11586 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011587 out.writeLong(mRealtime);
11588 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011589 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011590 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011591 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11592 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11593
11594 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11595 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11596 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11597 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011598 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011599 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011600 out.writeLong(mLongestLightIdleTime);
11601 out.writeLong(mLongestFullIdleTime);
11602 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11603 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11604 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011605 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011606 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11607 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11608 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11609 }
11610 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11611 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11612 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11613 }
11614 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11615 mNetworkByteActivityCounters[i].writeToParcel(out);
11616 mNetworkPacketActivityCounters[i].writeToParcel(out);
11617 }
11618 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11619 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011620 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011621 mMobileRadioActiveUnknownTime.writeToParcel(out);
11622 mMobileRadioActiveUnknownCount.writeToParcel(out);
11623 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11624 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11625 for (int i=0; i<NUM_WIFI_STATES; i++) {
11626 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11627 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011628 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11629 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11630 }
11631 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11632 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11633 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011634 mWifiActivity.writeToParcel(out, 0);
11635 mBluetoothActivity.writeToParcel(out, 0);
11636 mModemActivity.writeToParcel(out, 0);
11637 out.writeInt(mHasWifiReporting ? 1 : 0);
11638 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11639 out.writeInt(mHasModemReporting ? 1 : 0);
11640
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011641 out.writeInt(mNumConnectivityChange);
11642 out.writeInt(mLoadedNumConnectivityChange);
11643 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011644 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011645 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011646 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011647 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011648 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011649 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011650 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011651 out.writeInt(mLowDischargeAmountSinceCharge);
11652 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011653 out.writeInt(mDischargeAmountScreenOn);
11654 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11655 out.writeInt(mDischargeAmountScreenOff);
11656 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011657 mDischargeStepTracker.writeToParcel(out);
11658 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011659 mDischargeCounter.writeToParcel(out);
11660 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011661 out.writeLong(mLastWriteTime);
11662
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011663 if (inclUids) {
11664 out.writeInt(mKernelWakelockStats.size());
11665 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11666 SamplingTimer kwlt = ent.getValue();
11667 if (kwlt != null) {
11668 out.writeInt(1);
11669 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011670 kwlt.writeToParcel(out, uSecRealtime);
11671 } else {
11672 out.writeInt(0);
11673 }
11674 }
11675 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011676 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11677 SamplingTimer timer = ent.getValue();
11678 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011679 out.writeInt(1);
11680 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011681 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011682 } else {
11683 out.writeInt(0);
11684 }
Evan Millarc64edde2009-04-18 12:26:32 -070011685 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011686 } else {
11687 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011688 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011689
James Carr3a226052016-07-01 14:49:52 -070011690 out.writeInt(mKernelMemoryStats.size());
11691 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11692 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
11693 if (kmt != null) {
11694 out.writeInt(1);
11695 out.writeLong(mKernelMemoryStats.keyAt(i));
11696 kmt.writeToParcel(out, uSecRealtime);
11697 } else {
11698 out.writeInt(0);
11699 }
11700 }
11701
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011702 if (inclUids) {
11703 int size = mUidStats.size();
11704 out.writeInt(size);
11705 for (int i = 0; i < size; i++) {
11706 out.writeInt(mUidStats.keyAt(i));
11707 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011708
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011709 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011710 }
11711 } else {
11712 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011713 }
11714 }
11715
11716 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11717 new Parcelable.Creator<BatteryStatsImpl>() {
11718 public BatteryStatsImpl createFromParcel(Parcel in) {
11719 return new BatteryStatsImpl(in);
11720 }
11721
11722 public BatteryStatsImpl[] newArray(int size) {
11723 return new BatteryStatsImpl[size];
11724 }
11725 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011726
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011727 public void prepareForDumpLocked() {
11728 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011729 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011730
11731 // Pull the clock time. This may update the time and make a new history entry
11732 // if we had originally pulled a time before the RTC was set.
11733 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011734 }
11735
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011736 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011738 pw.println("mOnBatteryTimeBase:");
11739 mOnBatteryTimeBase.dump(pw, " ");
11740 pw.println("mOnBatteryScreenOffTimeBase:");
11741 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011742 Printer pr = new PrintWriterPrinter(pw);
11743 pr.println("*** Screen timer:");
11744 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011745 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011746 pr.println("*** Screen brightness #" + i + ":");
11747 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011748 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011749 pr.println("*** Interactive timer:");
11750 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011751 pr.println("*** Power save mode timer:");
11752 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011753 pr.println("*** Device idle mode light timer:");
11754 mDeviceIdleModeLightTimer.logState(pr, " ");
11755 pr.println("*** Device idle mode full timer:");
11756 mDeviceIdleModeFullTimer.logState(pr, " ");
11757 pr.println("*** Device light idling timer:");
11758 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011759 pr.println("*** Device idling timer:");
11760 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011761 pr.println("*** Phone timer:");
11762 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011763 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011764 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011765 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011766 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011767 pr.println("*** Signal scanning :");
11768 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011769 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011770 pr.println("*** Data connection type #" + i + ":");
11771 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011772 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011773 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011774 pr.println("*** Mobile network active timer:");
11775 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011776 pr.println("*** Mobile network active adjusted timer:");
11777 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011778 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011779 pr.println("*** Wifi timer:");
11780 mWifiOnTimer.logState(pr, " ");
11781 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011782 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011783 for (int i=0; i<NUM_WIFI_STATES; i++) {
11784 pr.println("*** Wifi state #" + i + ":");
11785 mWifiStateTimer[i].logState(pr, " ");
11786 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011787 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11788 pr.println("*** Wifi suppl state #" + i + ":");
11789 mWifiSupplStateTimer[i].logState(pr, " ");
11790 }
11791 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11792 pr.println("*** Wifi signal strength #" + i + ":");
11793 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11794 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011795 pr.println("*** Flashlight timer:");
11796 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011797 pr.println("*** Camera timer:");
11798 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011799 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011800 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011802}