blob: 6aa77665db510273e425aa2ac23d5dfb6f8f1668 [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 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006030 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
6031 public boolean reset() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006032 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006033
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006034 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006035 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006036 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006037 }
6038 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006039 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006040 active |= mFullWifiLockOut;
6041 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006042 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006043 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006044 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006045 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006046 if (mWifiBatchedScanTimer != null) {
6047 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6048 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006049 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006050 }
6051 }
6052 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6053 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006054 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006055 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006056 active |= mWifiMulticastEnabled;
6057 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006058
6059 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6060 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6061 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6062 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6063 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
6064 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
6065
Dianne Hackborn61659e52014-07-09 16:13:01 -07006066 if (mProcessStateTimer != null) {
6067 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6068 if (mProcessStateTimer[i] != null) {
6069 active |= !mProcessStateTimer[i].reset(false);
6070 }
6071 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006072 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006073 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006074 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006075 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006076 mVibratorOnTimer.detach();
6077 mVibratorOnTimer = null;
6078 } else {
6079 active = true;
6080 }
6081 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006082
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006083 if (mUserActivityCounters != null) {
6084 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6085 mUserActivityCounters[i].reset(false);
6086 }
6087 }
6088
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006089 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006090 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006091 mNetworkByteActivityCounters[i].reset(false);
6092 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006093 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006094 mMobileRadioActiveTime.reset(false);
6095 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006096 }
6097
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006098 if (mWifiControllerActivity != null) {
6099 mWifiControllerActivity.reset(false);
6100 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006101
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006102 if (mBluetoothControllerActivity != null) {
6103 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006104 }
6105
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006106 if (mModemControllerActivity != null) {
6107 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006108 }
6109
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006110 mUserCpuTime.reset(false);
6111 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006112 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006113
6114 if (mCpuClusterSpeed != null) {
6115 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
6116 if (speeds != null) {
6117 for (LongSamplingCounter speed : speeds) {
6118 if (speed != null) {
6119 speed.reset(false);
6120 }
6121 }
6122 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006123 }
6124 }
6125
Adam Lesinski5f056f62016-07-14 16:56:08 -07006126 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6127 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6128
Dianne Hackbornd953c532014-08-16 18:17:38 -07006129 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6130 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6131 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006132 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006133 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006134 } else {
6135 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006136 }
6137 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006138 mWakelockStats.cleanup();
6139 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6140 for (int is=syncStats.size()-1; is>=0; is--) {
6141 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006142 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006143 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006144 timer.detach();
6145 } else {
6146 active = true;
6147 }
6148 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006149 mSyncStats.cleanup();
6150 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6151 for (int ij=jobStats.size()-1; ij>=0; ij--) {
6152 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006153 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006154 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006155 timer.detach();
6156 } else {
6157 active = true;
6158 }
6159 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006160 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006161 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6162 Sensor s = mSensorStats.valueAt(ise);
6163 if (s.reset()) {
6164 mSensorStats.removeAt(ise);
6165 } else {
6166 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006167 }
6168 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006169 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6170 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006171 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006172 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006173 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006174 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006175 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006176 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006177 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006178 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006179 } else {
6180 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006181 }
6182 }
6183 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006184 if (mPackageStats.size() > 0) {
6185 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6186 while (it.hasNext()) {
6187 Map.Entry<String, Pkg> pkgEntry = it.next();
6188 Pkg p = pkgEntry.getValue();
6189 p.detach();
6190 if (p.mServiceStats.size() > 0) {
6191 Iterator<Map.Entry<String, Pkg.Serv>> it2
6192 = p.mServiceStats.entrySet().iterator();
6193 while (it2.hasNext()) {
6194 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6195 servEntry.getValue().detach();
6196 }
6197 }
6198 }
6199 mPackageStats.clear();
6200 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006201
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006202 mLastStepUserTime = mLastStepSystemTime = 0;
6203 mCurStepUserTime = mCurStepSystemTime = 0;
6204
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006205 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006206 if (mWifiRunningTimer != null) {
6207 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006208 }
6209 if (mFullWifiLockTimer != null) {
6210 mFullWifiLockTimer.detach();
6211 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006212 if (mWifiScanTimer != null) {
6213 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006214 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006215 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6216 if (mWifiBatchedScanTimer[i] != null) {
6217 mWifiBatchedScanTimer[i].detach();
6218 }
6219 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006220 if (mWifiMulticastTimer != null) {
6221 mWifiMulticastTimer.detach();
6222 }
6223 if (mAudioTurnedOnTimer != null) {
6224 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006225 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006226 }
6227 if (mVideoTurnedOnTimer != null) {
6228 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006229 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006230 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006231 if (mFlashlightTurnedOnTimer != null) {
6232 mFlashlightTurnedOnTimer.detach();
6233 mFlashlightTurnedOnTimer = null;
6234 }
6235 if (mCameraTurnedOnTimer != null) {
6236 mCameraTurnedOnTimer.detach();
6237 mCameraTurnedOnTimer = null;
6238 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006239 if (mForegroundActivityTimer != null) {
6240 mForegroundActivityTimer.detach();
6241 mForegroundActivityTimer = null;
6242 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006243 if (mBluetoothScanTimer != null) {
6244 mBluetoothScanTimer.detach();
6245 mBluetoothScanTimer = null;
6246 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006247 if (mUserActivityCounters != null) {
6248 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6249 mUserActivityCounters[i].detach();
6250 }
6251 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006252 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006253 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006254 mNetworkByteActivityCounters[i].detach();
6255 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006256 }
6257 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006258
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006259 if (mWifiControllerActivity != null) {
6260 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006261 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006262
6263 if (mBluetoothControllerActivity != null) {
6264 mBluetoothControllerActivity.detach();
6265 }
6266
6267 if (mModemControllerActivity != null) {
6268 mModemControllerActivity.detach();
6269 }
6270
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006271 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006272
6273 mUserCpuTime.detach();
6274 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006275 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006276
6277 if (mCpuClusterSpeed != null) {
6278 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6279 if (cpuSpeeds != null) {
6280 for (LongSamplingCounter c : cpuSpeeds) {
6281 if (c != null) {
6282 c.detach();
6283 }
6284 }
6285 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006286 }
6287 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006288
6289 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6290 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006291 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006292
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006293 return !active;
6294 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006295
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006296 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006297 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6298 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006299 out.writeInt(NW);
6300 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006301 out.writeString(wakeStats.keyAt(iw));
6302 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006303 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 }
6305
Dianne Hackbornd953c532014-08-16 18:17:38 -07006306 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6307 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006308 out.writeInt(NS);
6309 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006310 out.writeString(syncStats.keyAt(is));
6311 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006312 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6313 }
6314
Dianne Hackbornd953c532014-08-16 18:17:38 -07006315 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6316 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006317 out.writeInt(NJ);
6318 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006319 out.writeString(jobStats.keyAt(ij));
6320 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006321 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6322 }
6323
Dianne Hackborn61659e52014-07-09 16:13:01 -07006324 int NSE = mSensorStats.size();
6325 out.writeInt(NSE);
6326 for (int ise=0; ise<NSE; ise++) {
6327 out.writeInt(mSensorStats.keyAt(ise));
6328 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006329 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 }
6331
Dianne Hackborn61659e52014-07-09 16:13:01 -07006332 int NP = mProcessStats.size();
6333 out.writeInt(NP);
6334 for (int ip=0; ip<NP; ip++) {
6335 out.writeString(mProcessStats.keyAt(ip));
6336 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 proc.writeToParcelLocked(out);
6338 }
6339
6340 out.writeInt(mPackageStats.size());
6341 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6342 out.writeString(pkgEntry.getKey());
6343 Uid.Pkg pkg = pkgEntry.getValue();
6344 pkg.writeToParcelLocked(out);
6345 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006346
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006347 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006348 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006349 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006350 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006351 out.writeInt(0);
6352 }
6353 if (mFullWifiLockTimer != null) {
6354 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006355 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006356 } else {
6357 out.writeInt(0);
6358 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006359 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006360 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006361 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006362 } else {
6363 out.writeInt(0);
6364 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006365 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6366 if (mWifiBatchedScanTimer[i] != null) {
6367 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006368 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006369 } else {
6370 out.writeInt(0);
6371 }
6372 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006373 if (mWifiMulticastTimer != null) {
6374 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006375 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006376 } else {
6377 out.writeInt(0);
6378 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006379
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006380 if (mAudioTurnedOnTimer != null) {
6381 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006382 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006383 } else {
6384 out.writeInt(0);
6385 }
6386 if (mVideoTurnedOnTimer != null) {
6387 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006388 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006389 } else {
6390 out.writeInt(0);
6391 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006392 if (mFlashlightTurnedOnTimer != null) {
6393 out.writeInt(1);
6394 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6395 } else {
6396 out.writeInt(0);
6397 }
6398 if (mCameraTurnedOnTimer != null) {
6399 out.writeInt(1);
6400 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6401 } else {
6402 out.writeInt(0);
6403 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006404 if (mForegroundActivityTimer != null) {
6405 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006406 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006407 } else {
6408 out.writeInt(0);
6409 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006410 if (mBluetoothScanTimer != null) {
6411 out.writeInt(1);
6412 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6413 } else {
6414 out.writeInt(0);
6415 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006416 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6417 if (mProcessStateTimer[i] != null) {
6418 out.writeInt(1);
6419 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6420 } else {
6421 out.writeInt(0);
6422 }
6423 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006424 if (mVibratorOnTimer != null) {
6425 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006426 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006427 } else {
6428 out.writeInt(0);
6429 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006430 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006431 out.writeInt(1);
6432 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6433 mUserActivityCounters[i].writeToParcel(out);
6434 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006435 } else {
6436 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006437 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006438 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006439 out.writeInt(1);
6440 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006441 mNetworkByteActivityCounters[i].writeToParcel(out);
6442 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006443 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006444 mMobileRadioActiveTime.writeToParcel(out);
6445 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006446 } else {
6447 out.writeInt(0);
6448 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006449
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006450 if (mWifiControllerActivity != null) {
6451 out.writeInt(1);
6452 mWifiControllerActivity.writeToParcel(out, 0);
6453 } else {
6454 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006455 }
6456
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006457 if (mBluetoothControllerActivity != null) {
6458 out.writeInt(1);
6459 mBluetoothControllerActivity.writeToParcel(out, 0);
6460 } else {
6461 out.writeInt(0);
6462 }
6463
6464 if (mModemControllerActivity != null) {
6465 out.writeInt(1);
6466 mModemControllerActivity.writeToParcel(out, 0);
6467 } else {
6468 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006469 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006470
6471 mUserCpuTime.writeToParcel(out);
6472 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006473 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006474
Adam Lesinski6832f392015-09-05 18:05:40 -07006475 if (mCpuClusterSpeed != null) {
6476 out.writeInt(1);
6477 out.writeInt(mCpuClusterSpeed.length);
6478 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6479 if (cpuSpeeds != null) {
6480 out.writeInt(1);
6481 out.writeInt(cpuSpeeds.length);
6482 for (LongSamplingCounter c : cpuSpeeds) {
6483 if (c != null) {
6484 out.writeInt(1);
6485 c.writeToParcel(out);
6486 } else {
6487 out.writeInt(0);
6488 }
6489 }
6490 } else {
6491 out.writeInt(0);
6492 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006493 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006494 } else {
6495 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006496 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006497
6498 if (mMobileRadioApWakeupCount != null) {
6499 out.writeInt(1);
6500 mMobileRadioApWakeupCount.writeToParcel(out);
6501 } else {
6502 out.writeInt(0);
6503 }
6504
6505 if (mWifiRadioApWakeupCount != null) {
6506 out.writeInt(1);
6507 mWifiRadioApWakeupCount.writeToParcel(out);
6508 } else {
6509 out.writeInt(0);
6510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 }
6512
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006513 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006514 int numWakelocks = in.readInt();
6515 mWakelockStats.clear();
6516 for (int j = 0; j < numWakelocks; j++) {
6517 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006518 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006519 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006520 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 }
6522
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006523 int numSyncs = in.readInt();
6524 mSyncStats.clear();
6525 for (int j = 0; j < numSyncs; j++) {
6526 String syncName = in.readString();
6527 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006528 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006529 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006530 }
6531 }
6532
6533 int numJobs = in.readInt();
6534 mJobStats.clear();
6535 for (int j = 0; j < numJobs; j++) {
6536 String jobName = in.readString();
6537 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006538 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
6539 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006540 }
6541 }
6542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 int numSensors = in.readInt();
6544 mSensorStats.clear();
6545 for (int k = 0; k < numSensors; k++) {
6546 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006547 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
6548 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 mSensorStats.put(sensorNumber, sensor);
6550 }
6551
6552 int numProcs = in.readInt();
6553 mProcessStats.clear();
6554 for (int k = 0; k < numProcs; k++) {
6555 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006556 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 proc.readFromParcelLocked(in);
6558 mProcessStats.put(processName, proc);
6559 }
6560
6561 int numPkgs = in.readInt();
6562 mPackageStats.clear();
6563 for (int l = 0; l < numPkgs; l++) {
6564 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006565 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 pkg.readFromParcelLocked(in);
6567 mPackageStats.put(packageName, pkg);
6568 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006569
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006570 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006571 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006572 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6573 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006574 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006575 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006576 }
6577 mFullWifiLockOut = false;
6578 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006579 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6580 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006581 } else {
6582 mFullWifiLockTimer = null;
6583 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006584 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006585 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006586 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6587 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006588 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006589 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006590 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006591 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6592 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6593 if (in.readInt() != 0) {
6594 makeWifiBatchedScanBin(i, in);
6595 } else {
6596 mWifiBatchedScanTimer[i] = null;
6597 }
6598 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006599 mWifiMulticastEnabled = false;
6600 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006601 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6602 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006603 } else {
6604 mWifiMulticastTimer = null;
6605 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006606 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006607 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6608 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006609 } else {
6610 mAudioTurnedOnTimer = null;
6611 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006612 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006613 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6614 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006615 } else {
6616 mVideoTurnedOnTimer = null;
6617 }
6618 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006619 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6620 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006621 } else {
6622 mFlashlightTurnedOnTimer = null;
6623 }
6624 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006625 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6626 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006627 } else {
6628 mCameraTurnedOnTimer = null;
6629 }
6630 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006631 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6632 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006633 } else {
6634 mForegroundActivityTimer = null;
6635 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006636 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006637 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6638 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006639 } else {
6640 mBluetoothScanTimer = null;
6641 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006642 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006643 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6644 if (in.readInt() != 0) {
6645 makeProcessState(i, in);
6646 } else {
6647 mProcessStateTimer[i] = null;
6648 }
6649 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006650 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006651 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6652 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006653 } else {
6654 mVibratorOnTimer = null;
6655 }
6656 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006657 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6658 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006659 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006660 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006661 } else {
6662 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006663 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006664 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006665 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6666 mNetworkPacketActivityCounters
6667 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006668 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006669 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006670 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006671 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006672 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006673 }
Joe Onoratoabded112016-02-08 16:49:39 -08006674 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6675 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006676 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006677 mNetworkByteActivityCounters = null;
6678 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006679 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006680
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006681 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006682 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006683 NUM_WIFI_TX_LEVELS, in);
6684 } else {
6685 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006686 }
6687
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006688 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006689 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006690 NUM_BT_TX_LEVELS, in);
6691 } else {
6692 mBluetoothControllerActivity = null;
6693 }
6694
6695 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006696 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006697 ModemActivityInfo.TX_POWER_LEVELS, in);
6698 } else {
6699 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006700 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006701
Joe Onoratoabded112016-02-08 16:49:39 -08006702 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6703 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6704 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006705
Adam Lesinski6832f392015-09-05 18:05:40 -07006706 if (in.readInt() != 0) {
6707 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006708 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006709 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006710 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006711
6712 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6713 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6714 if (in.readInt() != 0) {
6715 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006716 if (mBsi.mPowerProfile != null &&
6717 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006718 throw new ParcelFormatException("Incompatible number of cpu speeds");
6719 }
6720
6721 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6722 mCpuClusterSpeed[cluster] = cpuSpeeds;
6723 for (int speed = 0; speed < numSpeeds; speed++) {
6724 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006725 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006726 }
6727 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006728 } else {
6729 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006730 }
6731 }
6732 } else {
6733 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006734 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006735
6736 if (in.readInt() != 0) {
6737 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6738 } else {
6739 mMobileRadioApWakeupCount = null;
6740 }
6741
6742 if (in.readInt() != 0) {
6743 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6744 } else {
6745 mWifiRadioApWakeupCount = null;
6746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 }
6748
6749 /**
6750 * The statistics associated with a particular wake lock.
6751 */
Joe Onoratoabded112016-02-08 16:49:39 -08006752 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6753 /**
6754 * BatteryStatsImpl that we are associated with.
6755 */
6756 protected BatteryStatsImpl mBsi;
6757
6758 /**
6759 * BatteryStatsImpl that we are associated with.
6760 */
6761 protected Uid mUid;
6762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006763 /**
6764 * How long (in ms) this uid has been keeping the device partially awake.
6765 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07006766 DurationTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767
6768 /**
6769 * How long (in ms) this uid has been keeping the device fully awake.
6770 */
Evan Millarc64edde2009-04-18 12:26:32 -07006771 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772
6773 /**
6774 * How long (in ms) this uid has had a window keeping the device awake.
6775 */
Evan Millarc64edde2009-04-18 12:26:32 -07006776 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777
6778 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006779 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006780 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006781 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006782
Joe Onoratoabded112016-02-08 16:49:39 -08006783 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6784 mBsi = bsi;
6785 mUid = uid;
6786 }
6787
Adam Lesinski9425fe22015-06-19 12:02:13 -07006788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6790 * proper timer pool from the given BatteryStatsImpl object.
6791 *
6792 * @param in the Parcel to be read from.
6793 * return a new Timer, or null.
6794 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07006795 private StopwatchTimer readStopwatchTimerFromParcel(int type,
6796 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 if (in.readInt() == 0) {
6798 return null;
6799 }
6800
Joe Onoratoabded112016-02-08 16:49:39 -08006801 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 }
6803
Joe Onorato92fd23f2016-07-25 11:18:42 -07006804 /**
6805 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6806 * proper timer pool from the given BatteryStatsImpl object.
6807 *
6808 * @param in the Parcel to be read from.
6809 * return a new Timer, or null.
6810 */
6811 private DurationTimer readDurationTimerFromParcel(int type,
6812 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
6813 if (in.readInt() == 0) {
6814 return null;
6815 }
6816
6817 return new DurationTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
6818 }
6819
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006820 boolean reset() {
6821 boolean wlactive = false;
6822 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006823 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006824 }
6825 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006826 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006827 }
6828 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006829 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006830 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006831 if (mTimerDraw != null) {
6832 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006833 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006834 if (!wlactive) {
6835 if (mTimerFull != null) {
6836 mTimerFull.detach();
6837 mTimerFull = null;
6838 }
6839 if (mTimerPartial != null) {
6840 mTimerPartial.detach();
6841 mTimerPartial = null;
6842 }
6843 if (mTimerWindow != null) {
6844 mTimerWindow.detach();
6845 mTimerWindow = null;
6846 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006847 if (mTimerDraw != null) {
6848 mTimerDraw.detach();
6849 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006850 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006851 }
6852 return !wlactive;
6853 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006854
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006855 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07006856 mTimerPartial = readDurationTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006857 mBsi.mPartialTimers, screenOffTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07006858 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
6859 mBsi.mFullTimers, timeBase, in);
6860 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
6861 mBsi.mWindowTimers, timeBase, in);
6862 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
6863 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 }
6865
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006866 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6867 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6868 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6869 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006870 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 }
6872
6873 @Override
6874 public Timer getWakeTime(int type) {
6875 switch (type) {
6876 case WAKE_TYPE_FULL: return mTimerFull;
6877 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6878 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006879 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 default: throw new IllegalArgumentException("type = " + type);
6881 }
6882 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006883
6884 public StopwatchTimer getStopwatchTimer(int type) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006885 switch (type) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07006886 case WAKE_TYPE_PARTIAL: {
6887 DurationTimer t = mTimerPartial;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006888 if (t == null) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07006889 t = new DurationTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006890 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006891 mTimerPartial = t;
6892 }
6893 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006894 }
6895 case WAKE_TYPE_FULL: {
6896 StopwatchTimer t = mTimerFull;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006897 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006898 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6899 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006900 mTimerFull = t;
6901 }
6902 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006903 }
6904 case WAKE_TYPE_WINDOW: {
6905 StopwatchTimer t = mTimerWindow;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006906 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006907 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6908 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006909 mTimerWindow = t;
6910 }
6911 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006912 }
6913 case WAKE_TYPE_DRAW: {
6914 StopwatchTimer t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006915 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006916 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6917 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006918 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006919 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006920 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07006921 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006922 default:
6923 throw new IllegalArgumentException("type=" + type);
6924 }
6925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 }
6927
Joe Onoratoabded112016-02-08 16:49:39 -08006928 public static class Sensor extends BatteryStats.Uid.Sensor {
6929 /**
6930 * BatteryStatsImpl that we are associated with.
6931 */
6932 protected BatteryStatsImpl mBsi;
6933
6934 /**
6935 * BatteryStatsImpl that we are associated with.
6936 */
6937 protected Uid mUid;
6938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006940 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006941
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006942 Counter mBgCounter;
6943
Joe Onoratoabded112016-02-08 16:49:39 -08006944 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6945 mBsi = bsi;
6946 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 mHandle = handle;
6948 }
6949
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006950 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 if (in.readInt() == 0) {
6952 return null;
6953 }
6954
Joe Onoratoabded112016-02-08 16:49:39 -08006955 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006957 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006958 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 }
Joe Onoratoabded112016-02-08 16:49:39 -08006960 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 }
6962
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006963 private Counter readCounterFromParcel(TimeBase timeBase, Parcel in) {
6964 if (in.readInt() == 0) {
6965 return null;
6966 }
6967 return new Counter(timeBase, in);
6968 }
6969
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006970 boolean reset() {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006971 if (mBgCounter != null) {
Adam Lesinski5f212c82017-03-13 12:25:13 -07006972 mBgCounter.reset(true /*detachIfReset*/);
6973 // If we detach, we must null the mBgCounter reference so that it
6974 // can be recreated and attached.
6975 mBgCounter = null;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006976 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006977 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006978 mTimer = null;
6979 return true;
6980 }
6981 return false;
6982 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006983
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006984 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6985 mTimer = readTimerFromParcel(timeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006986 mBgCounter = readCounterFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 }
6988
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006989 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6990 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08006991 Counter.writeCounterToParcel(out, mBgCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 }
6993
6994 @Override
6995 public Timer getSensorTime() {
6996 return mTimer;
6997 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006998
6999 @Override
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007000 public Counter getSensorBgCount() {
7001 return mBgCounter;
7002 }
7003
7004 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 public int getHandle() {
7006 return mHandle;
7007 }
7008 }
7009
7010 /**
7011 * The statistics associated with a particular process.
7012 */
Joe Onoratoabded112016-02-08 16:49:39 -08007013 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7014 /**
7015 * BatteryStatsImpl that we are associated with.
7016 */
7017 protected BatteryStatsImpl mBsi;
7018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007020 * The name of this process.
7021 */
7022 final String mName;
7023
7024 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007025 * Remains true until removed from the stats.
7026 */
7027 boolean mActive = true;
7028
7029 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007030 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 */
7032 long mUserTime;
7033
7034 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007035 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036 */
7037 long mSystemTime;
7038
7039 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007040 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007041 */
7042 long mForegroundTime;
7043
7044 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007045 * Number of times the process has been started.
7046 */
7047 int mStarts;
7048
7049 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007050 * Number of times the process has crashed.
7051 */
7052 int mNumCrashes;
7053
7054 /**
7055 * Number of times the process has had an ANR.
7056 */
7057 int mNumAnrs;
7058
7059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 * The amount of user time loaded from a previous save.
7061 */
7062 long mLoadedUserTime;
7063
7064 /**
7065 * The amount of system time loaded from a previous save.
7066 */
7067 long mLoadedSystemTime;
7068
7069 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007070 * The amount of foreground time loaded from a previous save.
7071 */
7072 long mLoadedForegroundTime;
7073
7074 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007075 * The number of times the process has started from a previous save.
7076 */
7077 int mLoadedStarts;
7078
7079 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007080 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007082 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083
7084 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007085 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007087 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007088
7089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 * The amount of user time when last unplugged.
7091 */
7092 long mUnpluggedUserTime;
7093
7094 /**
7095 * The amount of system time when last unplugged.
7096 */
7097 long mUnpluggedSystemTime;
7098
7099 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007100 * The amount of foreground time since unplugged.
7101 */
7102 long mUnpluggedForegroundTime;
7103
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007104 /**
7105 * The number of times the process has started before unplugged.
7106 */
7107 int mUnpluggedStarts;
7108
Dianne Hackborn61659e52014-07-09 16:13:01 -07007109 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007110 * Number of times the process has crashed before unplugged.
7111 */
7112 int mUnpluggedNumCrashes;
7113
7114 /**
7115 * Number of times the process has had an ANR before unplugged.
7116 */
7117 int mUnpluggedNumAnrs;
7118
Dianne Hackborn287952c2010-09-22 22:34:31 -07007119 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007120
Joe Onoratoabded112016-02-08 16:49:39 -08007121 public Proc(BatteryStatsImpl bsi, String name) {
7122 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007123 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007124 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007126
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007127 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 mUnpluggedUserTime = mUserTime;
7129 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007130 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007131 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007132 mUnpluggedNumCrashes = mNumCrashes;
7133 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 }
7135
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007136 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007138
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007139 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007140 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007141 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007142 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007143
Dianne Hackborn287952c2010-09-22 22:34:31 -07007144 public int countExcessivePowers() {
7145 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007146 }
7147
Dianne Hackborn287952c2010-09-22 22:34:31 -07007148 public ExcessivePower getExcessivePower(int i) {
7149 if (mExcessivePower != null) {
7150 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007151 }
7152 return null;
7153 }
7154
7155 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007156 if (mExcessivePower == null) {
7157 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007158 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007159 ExcessivePower ew = new ExcessivePower();
7160 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007161 ew.overTime = overTime;
7162 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007163 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007164 }
7165
Dianne Hackborn287952c2010-09-22 22:34:31 -07007166 public void addExcessiveCpu(long overTime, long usedTime) {
7167 if (mExcessivePower == null) {
7168 mExcessivePower = new ArrayList<ExcessivePower>();
7169 }
7170 ExcessivePower ew = new ExcessivePower();
7171 ew.type = ExcessivePower.TYPE_CPU;
7172 ew.overTime = overTime;
7173 ew.usedTime = usedTime;
7174 mExcessivePower.add(ew);
7175 }
7176
7177 void writeExcessivePowerToParcelLocked(Parcel out) {
7178 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007179 out.writeInt(0);
7180 return;
7181 }
7182
Dianne Hackborn287952c2010-09-22 22:34:31 -07007183 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007184 out.writeInt(N);
7185 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007186 ExcessivePower ew = mExcessivePower.get(i);
7187 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007188 out.writeLong(ew.overTime);
7189 out.writeLong(ew.usedTime);
7190 }
7191 }
7192
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007193 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007194 final int N = in.readInt();
7195 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007196 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007197 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007198 }
7199
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007200 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007201 throw new ParcelFormatException(
7202 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007203 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007204
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007205 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007206 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007207 ExcessivePower ew = new ExcessivePower();
7208 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007209 ew.overTime = in.readLong();
7210 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007211 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007212 }
7213 }
7214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007215 void writeToParcelLocked(Parcel out) {
7216 out.writeLong(mUserTime);
7217 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007218 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007220 out.writeInt(mNumCrashes);
7221 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 out.writeLong(mLoadedUserTime);
7223 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007224 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007226 out.writeInt(mLoadedNumCrashes);
7227 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 out.writeLong(mUnpluggedUserTime);
7229 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007230 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007232 out.writeInt(mUnpluggedNumCrashes);
7233 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007234 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 }
7236
7237 void readFromParcelLocked(Parcel in) {
7238 mUserTime = in.readLong();
7239 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007240 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007242 mNumCrashes = in.readInt();
7243 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 mLoadedUserTime = in.readLong();
7245 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007246 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007248 mLoadedNumCrashes = in.readInt();
7249 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 mUnpluggedUserTime = in.readLong();
7251 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007252 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007254 mUnpluggedNumCrashes = in.readInt();
7255 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007256 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 }
7258
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007259 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 mUserTime += utime;
7261 mSystemTime += stime;
7262 }
7263
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007264 public void addForegroundTimeLocked(long ttime) {
7265 mForegroundTime += ttime;
7266 }
7267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007268 public void incStartsLocked() {
7269 mStarts++;
7270 }
7271
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007272 public void incNumCrashesLocked() {
7273 mNumCrashes++;
7274 }
7275
7276 public void incNumAnrsLocked() {
7277 mNumAnrs++;
7278 }
7279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007281 public boolean isActive() {
7282 return mActive;
7283 }
7284
7285 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007287 long val = mUserTime;
7288 if (which == STATS_CURRENT) {
7289 val -= mLoadedUserTime;
7290 } else if (which == STATS_SINCE_UNPLUGGED) {
7291 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 }
7293 return val;
7294 }
7295
7296 @Override
7297 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007298 long val = mSystemTime;
7299 if (which == STATS_CURRENT) {
7300 val -= mLoadedSystemTime;
7301 } else if (which == STATS_SINCE_UNPLUGGED) {
7302 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 }
7304 return val;
7305 }
7306
7307 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007308 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007309 long val = mForegroundTime;
7310 if (which == STATS_CURRENT) {
7311 val -= mLoadedForegroundTime;
7312 } else if (which == STATS_SINCE_UNPLUGGED) {
7313 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007314 }
7315 return val;
7316 }
7317
7318 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007320 int val = mStarts;
7321 if (which == STATS_CURRENT) {
7322 val -= mLoadedStarts;
7323 } else if (which == STATS_SINCE_UNPLUGGED) {
7324 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 }
7326 return val;
7327 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007328
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007329 @Override
7330 public int getNumCrashes(int which) {
7331 int val = mNumCrashes;
7332 if (which == STATS_CURRENT) {
7333 val -= mLoadedNumCrashes;
7334 } else if (which == STATS_SINCE_UNPLUGGED) {
7335 val -= mUnpluggedNumCrashes;
7336 }
7337 return val;
7338 }
7339
7340 @Override
7341 public int getNumAnrs(int which) {
7342 int val = mNumAnrs;
7343 if (which == STATS_CURRENT) {
7344 val -= mLoadedNumAnrs;
7345 } else if (which == STATS_SINCE_UNPLUGGED) {
7346 val -= mUnpluggedNumAnrs;
7347 }
7348 return val;
7349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 }
7351
7352 /**
7353 * The statistics associated with a particular package.
7354 */
Joe Onoratoabded112016-02-08 16:49:39 -08007355 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7356 /**
7357 * BatteryStatsImpl that we are associated with.
7358 */
7359 protected BatteryStatsImpl mBsi;
7360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007362 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007364 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365
7366 /**
7367 * The statics we have collected for this package's services.
7368 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007369 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370
Joe Onoratoabded112016-02-08 16:49:39 -08007371 public Pkg(BatteryStatsImpl bsi) {
7372 mBsi = bsi;
7373 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007375
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007376 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 }
7378
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007379 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007380 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007381
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007382 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007383 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007384 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007387 int numWA = in.readInt();
7388 mWakeupAlarms.clear();
7389 for (int i=0; i<numWA; i++) {
7390 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007391 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393
7394 int numServs = in.readInt();
7395 mServiceStats.clear();
7396 for (int m = 0; m < numServs; m++) {
7397 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007398 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 mServiceStats.put(serviceName, serv);
7400
7401 serv.readFromParcelLocked(in);
7402 }
7403 }
7404
7405 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007406 int numWA = mWakeupAlarms.size();
7407 out.writeInt(numWA);
7408 for (int i=0; i<numWA; i++) {
7409 out.writeString(mWakeupAlarms.keyAt(i));
7410 mWakeupAlarms.valueAt(i).writeToParcel(out);
7411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007413 final int NS = mServiceStats.size();
7414 out.writeInt(NS);
7415 for (int i=0; i<NS; i++) {
7416 out.writeString(mServiceStats.keyAt(i));
7417 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 serv.writeToParcelLocked(out);
7419 }
7420 }
7421
7422 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007423 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7424 return mWakeupAlarms;
7425 }
7426
7427 public void noteWakeupAlarmLocked(String tag) {
7428 Counter c = mWakeupAlarms.get(tag);
7429 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007430 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007431 mWakeupAlarms.put(tag, c);
7432 }
7433 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 }
7435
7436 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007437 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
7438 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 }
7440
7441 /**
7442 * The statistics associated with a particular service.
7443 */
Joe Onoratoabded112016-02-08 16:49:39 -08007444 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
7445 /**
7446 * BatteryStatsImpl that we are associated with.
7447 */
7448 protected BatteryStatsImpl mBsi;
7449
7450 /**
7451 * The android package in which this service resides.
7452 */
7453 protected Pkg mPkg;
7454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 /**
7456 * Total time (ms in battery uptime) the service has been left started.
7457 */
Joe Onoratoabded112016-02-08 16:49:39 -08007458 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007459
7460 /**
7461 * If service has been started and not yet stopped, this is
7462 * when it was started.
7463 */
Joe Onoratoabded112016-02-08 16:49:39 -08007464 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465
7466 /**
7467 * True if we are currently running.
7468 */
Joe Onoratoabded112016-02-08 16:49:39 -08007469 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007470
7471 /**
7472 * Total number of times startService() has been called.
7473 */
Joe Onoratoabded112016-02-08 16:49:39 -08007474 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475
7476 /**
7477 * Total time (ms in battery uptime) the service has been left launched.
7478 */
Joe Onoratoabded112016-02-08 16:49:39 -08007479 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480
7481 /**
7482 * If service has been launched and not yet exited, this is
7483 * when it was launched (ms in battery uptime).
7484 */
Joe Onoratoabded112016-02-08 16:49:39 -08007485 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486
7487 /**
7488 * True if we are currently launched.
7489 */
Joe Onoratoabded112016-02-08 16:49:39 -08007490 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007491
7492 /**
7493 * Total number times the service has been launched.
7494 */
Joe Onoratoabded112016-02-08 16:49:39 -08007495 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496
7497 /**
7498 * The amount of time spent started loaded from a previous save
7499 * (ms in battery uptime).
7500 */
Joe Onoratoabded112016-02-08 16:49:39 -08007501 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502
7503 /**
7504 * The number of starts loaded from a previous save.
7505 */
Joe Onoratoabded112016-02-08 16:49:39 -08007506 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507
7508 /**
7509 * The number of launches loaded from a previous save.
7510 */
Joe Onoratoabded112016-02-08 16:49:39 -08007511 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512
7513 /**
7514 * The amount of time spent started as of the last run (ms
7515 * in battery uptime).
7516 */
Joe Onoratoabded112016-02-08 16:49:39 -08007517 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518
7519 /**
7520 * The number of starts as of the last run.
7521 */
Joe Onoratoabded112016-02-08 16:49:39 -08007522 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523
7524 /**
7525 * The number of launches as of the last run.
7526 */
Joe Onoratoabded112016-02-08 16:49:39 -08007527 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007528
7529 /**
7530 * The amount of time spent started when last unplugged (ms
7531 * in battery uptime).
7532 */
Joe Onoratoabded112016-02-08 16:49:39 -08007533 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534
7535 /**
7536 * The number of starts when last unplugged.
7537 */
Joe Onoratoabded112016-02-08 16:49:39 -08007538 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539
7540 /**
7541 * The number of launches when last unplugged.
7542 */
Joe Onoratoabded112016-02-08 16:49:39 -08007543 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007544
Joe Onoratoabded112016-02-08 16:49:39 -08007545 /**
7546 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7547 */
7548 public Serv(BatteryStatsImpl bsi) {
7549 mBsi = bsi;
7550 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007551 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007552
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007553 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7554 long baseRealtime) {
7555 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 mUnpluggedStarts = mStarts;
7557 mUnpluggedLaunches = mLaunches;
7558 }
7559
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007560 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7561 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007563
Joe Onoratoabded112016-02-08 16:49:39 -08007564 /**
7565 * Remove this Serv as a listener from the time base.
7566 */
7567 public void detach() {
7568 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007569 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007570
Joe Onoratoabded112016-02-08 16:49:39 -08007571 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 mStartTime = in.readLong();
7573 mRunningSince = in.readLong();
7574 mRunning = in.readInt() != 0;
7575 mStarts = in.readInt();
7576 mLaunchedTime = in.readLong();
7577 mLaunchedSince = in.readLong();
7578 mLaunched = in.readInt() != 0;
7579 mLaunches = in.readInt();
7580 mLoadedStartTime = in.readLong();
7581 mLoadedStarts = in.readInt();
7582 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007583 mLastStartTime = 0;
7584 mLastStarts = 0;
7585 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586 mUnpluggedStartTime = in.readLong();
7587 mUnpluggedStarts = in.readInt();
7588 mUnpluggedLaunches = in.readInt();
7589 }
7590
Joe Onoratoabded112016-02-08 16:49:39 -08007591 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 out.writeLong(mStartTime);
7593 out.writeLong(mRunningSince);
7594 out.writeInt(mRunning ? 1 : 0);
7595 out.writeInt(mStarts);
7596 out.writeLong(mLaunchedTime);
7597 out.writeLong(mLaunchedSince);
7598 out.writeInt(mLaunched ? 1 : 0);
7599 out.writeInt(mLaunches);
7600 out.writeLong(mLoadedStartTime);
7601 out.writeInt(mLoadedStarts);
7602 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007603 out.writeLong(mUnpluggedStartTime);
7604 out.writeInt(mUnpluggedStarts);
7605 out.writeInt(mUnpluggedLaunches);
7606 }
7607
Joe Onoratoabded112016-02-08 16:49:39 -08007608 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 if (!mLaunched) return mLaunchedTime;
7610 return mLaunchedTime + batteryUptime - mLaunchedSince;
7611 }
7612
Joe Onoratoabded112016-02-08 16:49:39 -08007613 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 if (!mRunning) return mStartTime;
7615 return mStartTime + batteryUptime - mRunningSince;
7616 }
7617
7618 public void startLaunchedLocked() {
7619 if (!mLaunched) {
7620 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007621 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007622 mLaunched = true;
7623 }
7624 }
7625
7626 public void stopLaunchedLocked() {
7627 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007628 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 if (time > 0) {
7630 mLaunchedTime += time;
7631 } else {
7632 mLaunches--;
7633 }
7634 mLaunched = false;
7635 }
7636 }
7637
7638 public void startRunningLocked() {
7639 if (!mRunning) {
7640 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007641 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007642 mRunning = true;
7643 }
7644 }
7645
7646 public void stopRunningLocked() {
7647 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007648 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 if (time > 0) {
7650 mStartTime += time;
7651 } else {
7652 mStarts--;
7653 }
7654 mRunning = false;
7655 }
7656 }
7657
7658 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007659 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 }
7661
7662 @Override
7663 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007664 int val = mLaunches;
7665 if (which == STATS_CURRENT) {
7666 val -= mLoadedLaunches;
7667 } else if (which == STATS_SINCE_UNPLUGGED) {
7668 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 return val;
7671 }
7672
7673 @Override
7674 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007675 long val = getStartTimeToNowLocked(now);
7676 if (which == STATS_CURRENT) {
7677 val -= mLoadedStartTime;
7678 } else if (which == STATS_SINCE_UNPLUGGED) {
7679 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 return val;
7682 }
7683
7684 @Override
7685 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007686 int val = mStarts;
7687 if (which == STATS_CURRENT) {
7688 val -= mLoadedStarts;
7689 } else if (which == STATS_SINCE_UNPLUGGED) {
7690 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 }
7692
7693 return val;
7694 }
7695 }
7696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007698 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 }
7700 }
7701
7702 /**
7703 * Retrieve the statistics object for a particular process, creating
7704 * if needed.
7705 */
7706 public Proc getProcessStatsLocked(String name) {
7707 Proc ps = mProcessStats.get(name);
7708 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007709 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 mProcessStats.put(name, ps);
7711 }
7712
7713 return ps;
7714 }
7715
Dianne Hackborna8d10942015-11-19 17:55:19 -08007716 public void updateUidProcessStateLocked(int procState) {
7717 int uidRunningState;
7718 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7719 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7720 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7721 uidRunningState = PROCESS_STATE_TOP;
7722 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7723 // Persistent and other foreground states go here.
7724 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7725 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7726 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7727 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7728 // Persistent and other foreground states go here.
7729 uidRunningState = PROCESS_STATE_FOREGROUND;
7730 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7731 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007732 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007733 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007734 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007735
Dianne Hackborna8d10942015-11-19 17:55:19 -08007736 if (mProcessState == uidRunningState) return;
7737
Joe Onoratoabded112016-02-08 16:49:39 -08007738 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007739
7740 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7741 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7742 }
7743 mProcessState = uidRunningState;
7744 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7745 if (mProcessStateTimer[uidRunningState] == null) {
7746 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007747 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007748 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007749 }
7750 }
7751
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007752 public SparseArray<? extends Pid> getPidStats() {
7753 return mPids;
7754 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007755
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007756 public Pid getPidStatsLocked(int pid) {
7757 Pid p = mPids.get(pid);
7758 if (p == null) {
7759 p = new Pid();
7760 mPids.put(pid, p);
7761 }
7762 return p;
7763 }
7764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 /**
7766 * Retrieve the statistics object for a particular service, creating
7767 * if needed.
7768 */
7769 public Pkg getPackageStatsLocked(String name) {
7770 Pkg ps = mPackageStats.get(name);
7771 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007772 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 mPackageStats.put(name, ps);
7774 }
7775
7776 return ps;
7777 }
7778
7779 /**
7780 * Retrieve the statistics object for a particular service, creating
7781 * if needed.
7782 */
7783 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7784 Pkg ps = getPackageStatsLocked(pkg);
7785 Pkg.Serv ss = ps.mServiceStats.get(serv);
7786 if (ss == null) {
7787 ss = ps.newServiceStatsLocked();
7788 ps.mServiceStats.put(serv, ss);
7789 }
7790
7791 return ss;
7792 }
7793
Dianne Hackbornd953c532014-08-16 18:17:38 -07007794 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7795 StopwatchTimer timer = mSyncStats.instantiateObject();
7796 timer.readSummaryFromParcelLocked(in);
7797 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007798 }
7799
Dianne Hackbornd953c532014-08-16 18:17:38 -07007800 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7801 StopwatchTimer timer = mJobStats.instantiateObject();
7802 timer.readSummaryFromParcelLocked(in);
7803 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007804 }
7805
Dianne Hackbornd953c532014-08-16 18:17:38 -07007806 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007807 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007808 mWakelockStats.add(wlName, wl);
7809 if (in.readInt() != 0) {
7810 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007812 if (in.readInt() != 0) {
7813 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7814 }
7815 if (in.readInt() != 0) {
7816 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007818 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007819 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 }
7822
Evan Millarc64edde2009-04-18 12:26:32 -07007823 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 Sensor se = mSensorStats.get(sensor);
7825 if (se == null) {
7826 if (!create) {
7827 return null;
7828 }
Joe Onoratoabded112016-02-08 16:49:39 -08007829 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007830 mSensorStats.put(sensor, se);
7831 }
Evan Millarc64edde2009-04-18 12:26:32 -07007832 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 if (t != null) {
7834 return t;
7835 }
Joe Onoratoabded112016-02-08 16:49:39 -08007836 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007838 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007839 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 }
Joe Onoratoabded112016-02-08 16:49:39 -08007841 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7842 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 se.mTimer = t;
7844 return t;
7845 }
7846
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007847 public Counter getSensorBgCounterLocked(int sensor, boolean create) {
7848 Sensor se = mSensorStats.get(sensor);
7849 if (se == null) {
7850 if (!create) {
7851 return null;
7852 }
7853 se = new Sensor(mBsi, this, sensor);
7854 mSensorStats.put(sensor, se);
7855 }
7856 Counter c = se.mBgCounter;
7857 if (c != null) return c;
7858 c = new Counter(mBsi.mOnBatteryTimeBase);
7859 se.mBgCounter = c;
7860 return c;
7861 }
7862
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007863 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007864 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007865 if (t != null) {
7866 t.startRunningLocked(elapsedRealtimeMs);
7867 }
7868 }
7869
7870 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007871 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007872 if (t != null) {
7873 t.stopRunningLocked(elapsedRealtimeMs);
7874 }
7875 }
7876
7877 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007878 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007879 if (t != null) {
7880 t.startRunningLocked(elapsedRealtimeMs);
7881 }
7882 }
7883
7884 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007885 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007886 if (t != null) {
7887 t.stopRunningLocked(elapsedRealtimeMs);
7888 }
7889 }
7890
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007891 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007892 Wakelock wl = mWakelockStats.startObject(name);
7893 if (wl != null) {
7894 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007896 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007897 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007898 if (p.mWakeNesting++ == 0) {
7899 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007900 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 }
7903
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007904 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007905 Wakelock wl = mWakelockStats.stopObject(name);
7906 if (wl != null) {
7907 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007909 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007910 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007911 if (p != null && p.mWakeNesting > 0) {
7912 if (p.mWakeNesting-- == 1) {
7913 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7914 p.mWakeStartMs = 0;
7915 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007916 }
7917 }
7918 }
7919
7920 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7921 Proc p = getProcessStatsLocked(proc);
7922 if (p != null) {
7923 p.addExcessiveWake(overTime, usedTime);
7924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007926
Dianne Hackborn287952c2010-09-22 22:34:31 -07007927 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7928 Proc p = getProcessStatsLocked(proc);
7929 if (p != null) {
7930 p.addExcessiveCpu(overTime, usedTime);
7931 }
7932 }
7933
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007934 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Amith Yamasani154d1242017-02-16 10:01:48 -08007935 StopwatchTimer t = getSensorTimerLocked(sensor, /* create= */ true);
7936 t.startRunningLocked(elapsedRealtimeMs);
7937
7938 Counter c = getSensorBgCounterLocked(sensor, /* create= */ true);
7939 if (mProcessState >= PROCESS_STATE_BACKGROUND && t.mNesting == 1) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007940 c.stepAtomic();
7941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007942 }
7943
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007944 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007945 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007946 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007948 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007950 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007951
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007952 public void noteStartGps(long elapsedRealtimeMs) {
Amith Yamasani154d1242017-02-16 10:01:48 -08007953 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, /* create= */ true);
7954 t.startRunningLocked(elapsedRealtimeMs);
7955
7956 Counter c = getSensorBgCounterLocked(Sensor.GPS, /* create= */ true);
7957 if (mProcessState >= PROCESS_STATE_BACKGROUND && t.mNesting == 1) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007958 c.stepAtomic();
7959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007960 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007961
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007962 public void noteStopGps(long elapsedRealtimeMs) {
Amith Yamasani154d1242017-02-16 10:01:48 -08007963 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007964 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007966 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 }
7969
7970 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007971 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007972 }
7973 }
7974
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007975 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007976 this(new SystemClocks(), systemDir, handler, externalSync, null);
7977 }
7978
7979 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
7980 PlatformIdleStateCallback cb) {
7981 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08007982 }
7983
7984 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007985 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08007986 init(clocks);
7987
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007988 if (systemDir != null) {
7989 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7990 new File(systemDir, "batterystats.bin.tmp"));
7991 } else {
7992 mFile = null;
7993 }
7994 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007995 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007996 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007997 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007999 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008000 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008001 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008002 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008003 }
Joe Onoratoabded112016-02-08 16:49:39 -08008004 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8005 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8006 mOnBatteryTimeBase);
8007 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8008 mOnBatteryTimeBase);
8009 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8010 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8011 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8012 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8013 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8014 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8015 mOnBatteryTimeBase);
8016 }
8017 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8018 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008019 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008020 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008021 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008022 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008023 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008024 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8025 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008026 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008027 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8028 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8029 NUM_BT_TX_LEVELS);
8030 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8031 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008032 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8033 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8034 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008035 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008036 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8037 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008038 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8039 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008040 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008041 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008042 mOnBatteryTimeBase);
8043 }
Joe Onoratoabded112016-02-08 16:49:39 -08008044 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8045 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8046 mOnBatteryTimeBase);
8047 }
8048 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8049 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8050 mOnBatteryTimeBase);
8051 }
8052 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8053 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8054 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8055 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8056 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008057 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8058 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008060 long uptime = mClocks.uptimeMillis() * 1000;
8061 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008062 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008063 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008064 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008065 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008066 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008067 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008068 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008069 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008070 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008071 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008072 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
8074
8075 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008076 this(new SystemClocks(), p);
8077 }
8078
8079 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8080 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008081 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008082 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008083 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008084 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008085 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008086 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008088 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 }
8090
Adam Lesinskie08af192015-03-25 16:42:59 -07008091 public void setPowerProfile(PowerProfile profile) {
8092 synchronized (this) {
8093 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008094
8095 // We need to initialize the KernelCpuSpeedReaders to read from
8096 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8097 // information.
8098 final int numClusters = mPowerProfile.getNumCpuClusters();
8099 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8100 int firstCpuOfCluster = 0;
8101 for (int i = 0; i < numClusters; i++) {
8102 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8103 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8104 numSpeedSteps);
8105 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8106 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008107
8108 if (mEstimatedBatteryCapacity == -1) {
8109 // Initialize the estimated battery capacity to a known preset one.
8110 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8111 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008112 }
8113 }
8114
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008115 public void setCallback(BatteryCallback cb) {
8116 mCallback = cb;
8117 }
8118
Amith Yamasanif37447b2009-10-08 18:28:01 -07008119 public void setRadioScanningTimeout(long timeout) {
8120 if (mPhoneSignalScanningTimer != null) {
8121 mPhoneSignalScanningTimer.setTimeout(timeout);
8122 }
8123 }
8124
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008125 public void updateDailyDeadlineLocked() {
8126 // Get the current time.
8127 long currentTime = mDailyStartTime = System.currentTimeMillis();
8128 Calendar calDeadline = Calendar.getInstance();
8129 calDeadline.setTimeInMillis(currentTime);
8130
8131 // Move time up to the next day, ranging from 1am to 3pm.
8132 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8133 calDeadline.set(Calendar.MILLISECOND, 0);
8134 calDeadline.set(Calendar.SECOND, 0);
8135 calDeadline.set(Calendar.MINUTE, 0);
8136 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8137 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8138 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8139 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8140 }
8141
8142 public void recordDailyStatsIfNeededLocked(boolean settled) {
8143 long currentTime = System.currentTimeMillis();
8144 if (currentTime >= mNextMaxDailyDeadline) {
8145 recordDailyStatsLocked();
8146 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8147 recordDailyStatsLocked();
8148 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8149 recordDailyStatsLocked();
8150 }
8151 }
8152
8153 public void recordDailyStatsLocked() {
8154 DailyItem item = new DailyItem();
8155 item.mStartTime = mDailyStartTime;
8156 item.mEndTime = System.currentTimeMillis();
8157 boolean hasData = false;
8158 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8159 hasData = true;
8160 item.mDischargeSteps = new LevelStepTracker(
8161 mDailyDischargeStepTracker.mNumStepDurations,
8162 mDailyDischargeStepTracker.mStepDurations);
8163 }
8164 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8165 hasData = true;
8166 item.mChargeSteps = new LevelStepTracker(
8167 mDailyChargeStepTracker.mNumStepDurations,
8168 mDailyChargeStepTracker.mStepDurations);
8169 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008170 if (mDailyPackageChanges != null) {
8171 hasData = true;
8172 item.mPackageChanges = mDailyPackageChanges;
8173 mDailyPackageChanges = null;
8174 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008175 mDailyDischargeStepTracker.init();
8176 mDailyChargeStepTracker.init();
8177 updateDailyDeadlineLocked();
8178
8179 if (hasData) {
8180 mDailyItems.add(item);
8181 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8182 mDailyItems.remove(0);
8183 }
8184 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8185 try {
8186 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008187 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008188 writeDailyItemsLocked(out);
8189 BackgroundThread.getHandler().post(new Runnable() {
8190 @Override
8191 public void run() {
8192 synchronized (mCheckinFile) {
8193 FileOutputStream stream = null;
8194 try {
8195 stream = mDailyFile.startWrite();
8196 memStream.writeTo(stream);
8197 stream.flush();
8198 FileUtils.sync(stream);
8199 stream.close();
8200 mDailyFile.finishWrite(stream);
8201 } catch (IOException e) {
8202 Slog.w("BatteryStats",
8203 "Error writing battery daily items", e);
8204 mDailyFile.failWrite(stream);
8205 }
8206 }
8207 }
8208 });
8209 } catch (IOException e) {
8210 }
8211 }
8212 }
8213
8214 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8215 StringBuilder sb = new StringBuilder(64);
8216 out.startDocument(null, true);
8217 out.startTag(null, "daily-items");
8218 for (int i=0; i<mDailyItems.size(); i++) {
8219 final DailyItem dit = mDailyItems.get(i);
8220 out.startTag(null, "item");
8221 out.attribute(null, "start", Long.toString(dit.mStartTime));
8222 out.attribute(null, "end", Long.toString(dit.mEndTime));
8223 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8224 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008225 if (dit.mPackageChanges != null) {
8226 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8227 PackageChange pc = dit.mPackageChanges.get(j);
8228 if (pc.mUpdate) {
8229 out.startTag(null, "upd");
8230 out.attribute(null, "pkg", pc.mPackageName);
8231 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8232 out.endTag(null, "upd");
8233 } else {
8234 out.startTag(null, "rem");
8235 out.attribute(null, "pkg", pc.mPackageName);
8236 out.endTag(null, "rem");
8237 }
8238 }
8239 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008240 out.endTag(null, "item");
8241 }
8242 out.endTag(null, "daily-items");
8243 out.endDocument();
8244 }
8245
8246 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
8247 StringBuilder tmpBuilder) throws IOException {
8248 if (steps != null) {
8249 out.startTag(null, tag);
8250 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
8251 for (int i=0; i<steps.mNumStepDurations; i++) {
8252 out.startTag(null, "s");
8253 tmpBuilder.setLength(0);
8254 steps.encodeEntryAt(i, tmpBuilder);
8255 out.attribute(null, "v", tmpBuilder.toString());
8256 out.endTag(null, "s");
8257 }
8258 out.endTag(null, tag);
8259 }
8260 }
8261
8262 public void readDailyStatsLocked() {
8263 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
8264 mDailyItems.clear();
8265 FileInputStream stream;
8266 try {
8267 stream = mDailyFile.openRead();
8268 } catch (FileNotFoundException e) {
8269 return;
8270 }
8271 try {
8272 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008273 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008274 readDailyItemsLocked(parser);
8275 } catch (XmlPullParserException e) {
8276 } finally {
8277 try {
8278 stream.close();
8279 } catch (IOException e) {
8280 }
8281 }
8282 }
8283
8284 private void readDailyItemsLocked(XmlPullParser parser) {
8285 try {
8286 int type;
8287 while ((type = parser.next()) != XmlPullParser.START_TAG
8288 && type != XmlPullParser.END_DOCUMENT) {
8289 ;
8290 }
8291
8292 if (type != XmlPullParser.START_TAG) {
8293 throw new IllegalStateException("no start tag found");
8294 }
8295
8296 int outerDepth = parser.getDepth();
8297 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8298 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8299 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8300 continue;
8301 }
8302
8303 String tagName = parser.getName();
8304 if (tagName.equals("item")) {
8305 readDailyItemTagLocked(parser);
8306 } else {
8307 Slog.w(TAG, "Unknown element under <daily-items>: "
8308 + parser.getName());
8309 XmlUtils.skipCurrentTag(parser);
8310 }
8311 }
8312
8313 } catch (IllegalStateException e) {
8314 Slog.w(TAG, "Failed parsing daily " + e);
8315 } catch (NullPointerException e) {
8316 Slog.w(TAG, "Failed parsing daily " + e);
8317 } catch (NumberFormatException e) {
8318 Slog.w(TAG, "Failed parsing daily " + e);
8319 } catch (XmlPullParserException e) {
8320 Slog.w(TAG, "Failed parsing daily " + e);
8321 } catch (IOException e) {
8322 Slog.w(TAG, "Failed parsing daily " + e);
8323 } catch (IndexOutOfBoundsException e) {
8324 Slog.w(TAG, "Failed parsing daily " + e);
8325 }
8326 }
8327
8328 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8329 XmlPullParserException, IOException {
8330 DailyItem dit = new DailyItem();
8331 String attr = parser.getAttributeValue(null, "start");
8332 if (attr != null) {
8333 dit.mStartTime = Long.parseLong(attr);
8334 }
8335 attr = parser.getAttributeValue(null, "end");
8336 if (attr != null) {
8337 dit.mEndTime = Long.parseLong(attr);
8338 }
8339 int outerDepth = parser.getDepth();
8340 int type;
8341 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8342 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8343 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8344 continue;
8345 }
8346
8347 String tagName = parser.getName();
8348 if (tagName.equals("dis")) {
8349 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8350 } else if (tagName.equals("chg")) {
8351 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008352 } else if (tagName.equals("upd")) {
8353 if (dit.mPackageChanges == null) {
8354 dit.mPackageChanges = new ArrayList<>();
8355 }
8356 PackageChange pc = new PackageChange();
8357 pc.mUpdate = true;
8358 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8359 String verStr = parser.getAttributeValue(null, "ver");
8360 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8361 dit.mPackageChanges.add(pc);
8362 XmlUtils.skipCurrentTag(parser);
8363 } else if (tagName.equals("rem")) {
8364 if (dit.mPackageChanges == null) {
8365 dit.mPackageChanges = new ArrayList<>();
8366 }
8367 PackageChange pc = new PackageChange();
8368 pc.mUpdate = false;
8369 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8370 dit.mPackageChanges.add(pc);
8371 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008372 } else {
8373 Slog.w(TAG, "Unknown element under <item>: "
8374 + parser.getName());
8375 XmlUtils.skipCurrentTag(parser);
8376 }
8377 }
8378 mDailyItems.add(dit);
8379 }
8380
8381 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8382 String tag)
8383 throws NumberFormatException, XmlPullParserException, IOException {
8384 final String numAttr = parser.getAttributeValue(null, "n");
8385 if (numAttr == null) {
8386 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
8387 XmlUtils.skipCurrentTag(parser);
8388 return;
8389 }
8390 final int num = Integer.parseInt(numAttr);
8391 LevelStepTracker steps = new LevelStepTracker(num);
8392 if (isCharge) {
8393 dit.mChargeSteps = steps;
8394 } else {
8395 dit.mDischargeSteps = steps;
8396 }
8397 int i = 0;
8398 int outerDepth = parser.getDepth();
8399 int type;
8400 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8401 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8402 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8403 continue;
8404 }
8405
8406 String tagName = parser.getName();
8407 if ("s".equals(tagName)) {
8408 if (i < num) {
8409 String valueAttr = parser.getAttributeValue(null, "v");
8410 if (valueAttr != null) {
8411 steps.decodeEntryAt(i, valueAttr);
8412 i++;
8413 }
8414 }
8415 } else {
8416 Slog.w(TAG, "Unknown element under <" + tag + ">: "
8417 + parser.getName());
8418 XmlUtils.skipCurrentTag(parser);
8419 }
8420 }
8421 steps.mNumStepDurations = i;
8422 }
8423
8424 @Override
8425 public DailyItem getDailyItemLocked(int daysAgo) {
8426 int index = mDailyItems.size()-1-daysAgo;
8427 return index >= 0 ? mDailyItems.get(index) : null;
8428 }
8429
8430 @Override
8431 public long getCurrentDailyStartTime() {
8432 return mDailyStartTime;
8433 }
8434
8435 @Override
8436 public long getNextMinDailyDeadline() {
8437 return mNextMinDailyDeadline;
8438 }
8439
8440 @Override
8441 public long getNextMaxDailyDeadline() {
8442 return mNextMaxDailyDeadline;
8443 }
8444
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008445 @Override
8446 public boolean startIteratingOldHistoryLocked() {
8447 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8448 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008449 if ((mHistoryIterator = mHistory) == null) {
8450 return false;
8451 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008452 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008453 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008454 mReadOverflow = false;
8455 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008456 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008457 }
8458
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008459 @Override
8460 public boolean getNextOldHistoryLocked(HistoryItem out) {
8461 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
8462 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008463 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008464 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008465 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008466 HistoryItem cur = mHistoryIterator;
8467 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008468 if (!mReadOverflow && !end) {
8469 Slog.w(TAG, "Old history ends before new history!");
8470 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008471 return false;
8472 }
8473 out.setTo(cur);
8474 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008475 if (!mReadOverflow) {
8476 if (end) {
8477 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008478 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008479 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008480 pw.println("Histories differ!");
8481 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008482 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008483 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008484 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8485 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008486 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008487 }
8488 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008489 return true;
8490 }
8491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008493 public void finishIteratingOldHistoryLocked() {
8494 mIteratingHistory = false;
8495 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008496 mHistoryIterator = null;
8497 }
8498
8499 public int getHistoryTotalSize() {
8500 return MAX_HISTORY_BUFFER;
8501 }
8502
8503 public int getHistoryUsedSize() {
8504 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008505 }
8506
8507 @Override
8508 public boolean startIteratingHistoryLocked() {
8509 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8510 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008511 if (mHistoryBuffer.dataSize() <= 0) {
8512 return false;
8513 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008514 mHistoryBuffer.setDataPosition(0);
8515 mReadOverflow = false;
8516 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008517 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8518 mReadHistoryUids = new int[mHistoryTagPool.size()];
8519 mReadHistoryChars = 0;
8520 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8521 final HistoryTag tag = ent.getKey();
8522 final int idx = ent.getValue();
8523 mReadHistoryStrings[idx] = tag.string;
8524 mReadHistoryUids[idx] = tag.uid;
8525 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008526 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008527 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008528 }
8529
8530 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008531 public int getHistoryStringPoolSize() {
8532 return mReadHistoryStrings.length;
8533 }
8534
8535 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008536 public int getHistoryStringPoolBytes() {
8537 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8538 // Each string character is 2 bytes.
8539 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8540 }
8541
8542 @Override
8543 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008544 return mReadHistoryStrings[index];
8545 }
8546
8547 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008548 public int getHistoryTagPoolUid(int index) {
8549 return mReadHistoryUids[index];
8550 }
8551
8552 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008553 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008554 final int pos = mHistoryBuffer.dataPosition();
8555 if (pos == 0) {
8556 out.clear();
8557 }
8558 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008559 if (end) {
8560 return false;
8561 }
8562
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008563 final long lastRealtime = out.time;
8564 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008565 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008566 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8567 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008568 out.currentTime = lastWalltime + (out.time - lastRealtime);
8569 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008570 return true;
8571 }
8572
8573 @Override
8574 public void finishIteratingHistoryLocked() {
8575 mIteratingHistory = false;
8576 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008577 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008578 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008579
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008580 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008581 public long getHistoryBaseTime() {
8582 return mHistoryBaseTime;
8583 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008584
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008585 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 public int getStartCount() {
8587 return mStartCount;
8588 }
8589
8590 public boolean isOnBattery() {
8591 return mOnBattery;
8592 }
8593
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008594 public boolean isCharging() {
8595 return mCharging;
8596 }
8597
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008598 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008599 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008600 }
8601
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008602 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008603 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008604 mOnBatteryTimeBase.init(uptime, realtime);
8605 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008606 mRealtime = 0;
8607 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008608 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008609 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008610 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008611
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008612 void initDischarge() {
8613 mLowDischargeAmountSinceCharge = 0;
8614 mHighDischargeAmountSinceCharge = 0;
8615 mDischargeAmountScreenOn = 0;
8616 mDischargeAmountScreenOnSinceCharge = 0;
8617 mDischargeAmountScreenOff = 0;
8618 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008619 mDischargeStepTracker.init();
8620 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008621 mDischargeScreenOffCounter.reset(false);
8622 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008623 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008624
8625 public void resetAllStatsCmdLocked() {
8626 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008627 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008628 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008629 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008630 long realtime = mSecRealtime * 1000;
8631 mDischargeStartLevel = mHistoryCur.batteryLevel;
8632 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008633 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008634 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8635 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008636 mOnBatteryTimeBase.reset(uptime, realtime);
8637 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8638 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008639 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008640 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8641 mDischargeScreenOffUnplugLevel = 0;
8642 } else {
8643 mDischargeScreenOnUnplugLevel = 0;
8644 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8645 }
8646 mDischargeAmountScreenOn = 0;
8647 mDischargeAmountScreenOff = 0;
8648 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008649 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008650 }
8651
8652 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008653 final long uptimeMillis = mClocks.uptimeMillis();
8654 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008655 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008656 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008657 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008658 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008659 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008660 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008661
8662 if (mPowerProfile != null) {
8663 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8664 } else {
8665 mEstimatedBatteryCapacity = -1;
8666 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008667 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008668 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008669 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008670 mLongestLightIdleTime = 0;
8671 mLongestFullIdleTime = 0;
8672 mDeviceIdleModeLightTimer.reset(false);
8673 mDeviceIdleModeFullTimer.reset(false);
8674 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008675 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008676 mPhoneOnTimer.reset(false);
8677 mAudioOnTimer.reset(false);
8678 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008679 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008680 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008681 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008682 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008683 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008684 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008685 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008686 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008687 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008688 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008689 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008690 mNetworkByteActivityCounters[i].reset(false);
8691 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008692 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008693 mMobileRadioActiveTimer.reset(false);
8694 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008695 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008696 mMobileRadioActiveUnknownTime.reset(false);
8697 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008698 mWifiOnTimer.reset(false);
8699 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008700 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008701 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008702 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008703 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8704 mWifiSupplStateTimer[i].reset(false);
8705 }
8706 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8707 mWifiSignalStrengthsTimer[i].reset(false);
8708 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008709 mWifiActivity.reset(false);
8710 mBluetoothActivity.reset(false);
8711 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008712 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008713
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008714 for (int i=0; i<mUidStats.size(); i++) {
8715 if (mUidStats.valueAt(i).reset()) {
8716 mUidStats.remove(mUidStats.keyAt(i));
8717 i--;
8718 }
8719 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008720
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008721 if (mKernelWakelockStats.size() > 0) {
8722 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008723 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008724 }
8725 mKernelWakelockStats.clear();
8726 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008727
James Carr3a226052016-07-01 14:49:52 -07008728 if (mKernelMemoryStats.size() > 0) {
8729 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
8730 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
8731 }
8732 mKernelMemoryStats.clear();
8733 }
8734
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008735 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008736 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8737 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008738 }
8739 mWakeupReasonStats.clear();
8740 }
8741
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008742 mLastHistoryStepDetails = null;
8743 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8744 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8745 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8746 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8747 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8748 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8749 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8750 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8751 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8752 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8753
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008754 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008755
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008756 clearHistoryLocked();
8757 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008758
Dianne Hackborn40c87252014-03-19 16:55:40 -07008759 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008760 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008761 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8762 // Not recording process starts/stops.
8763 continue;
8764 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008765 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008766 if (active == null) {
8767 continue;
8768 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008769 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8770 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008771 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008772 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8773 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008774 }
8775 }
8776 }
8777 }
8778
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008779 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008780 if (oldScreenOn) {
8781 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8782 if (diff > 0) {
8783 mDischargeAmountScreenOn += diff;
8784 mDischargeAmountScreenOnSinceCharge += diff;
8785 }
8786 } else {
8787 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8788 if (diff > 0) {
8789 mDischargeAmountScreenOff += diff;
8790 mDischargeAmountScreenOffSinceCharge += diff;
8791 }
8792 }
8793 if (newScreenOn) {
8794 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8795 mDischargeScreenOffUnplugLevel = 0;
8796 } else {
8797 mDischargeScreenOnUnplugLevel = 0;
8798 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8799 }
8800 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008801
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008802 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008803 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008804 final boolean screenOn = mScreenState == Display.STATE_ON;
8805 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008806 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008807 }
8808
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008809 private String[] mMobileIfaces = EmptyArray.STRING;
8810 private String[] mWifiIfaces = EmptyArray.STRING;
8811
8812 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8813
8814 private static final int NETWORK_STATS_LAST = 0;
8815 private static final int NETWORK_STATS_NEXT = 1;
8816 private static final int NETWORK_STATS_DELTA = 2;
8817
Joe Onoratoabded112016-02-08 16:49:39 -08008818 private NetworkStats[] mMobileNetworkStats;
8819 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008820
8821 /**
8822 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8823 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8824 */
8825 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8826 NetworkStats[] networkStatsBuffer)
8827 throws IOException {
8828 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8829 false)) {
8830 return null;
8831 }
8832
8833 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8834 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8835 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8836 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8837 networkStatsBuffer[NETWORK_STATS_DELTA]);
8838 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8839 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8840 return networkStatsBuffer[NETWORK_STATS_DELTA];
8841 }
8842
8843 /**
8844 * Distribute WiFi energy info and network traffic to apps.
8845 * @param info The energy information from the WiFi controller.
8846 */
8847 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008848 if (DEBUG_ENERGY) {
8849 Slog.d(TAG, "Updating wifi stats");
8850 }
8851
Joe Onoratoabded112016-02-08 16:49:39 -08008852 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008853 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008854 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008855 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8856 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8857 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008858 } catch (IOException e) {
8859 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8860 return;
8861 }
8862
8863 if (!mOnBatteryInternal) {
8864 return;
8865 }
8866
Adam Lesinskie08af192015-03-25 16:42:59 -07008867 SparseLongArray rxPackets = new SparseLongArray();
8868 SparseLongArray txPackets = new SparseLongArray();
8869 long totalTxPackets = 0;
8870 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008871 if (delta != null) {
8872 final int size = delta.size();
8873 for (int i = 0; i < size; i++) {
8874 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8875
Adam Lesinskie08af192015-03-25 16:42:59 -07008876 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008877 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008878 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8879 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008880 }
8881
Adam Lesinski6cca4142016-02-25 18:23:02 -08008882 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8883 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008884 continue;
8885 }
8886
8887 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008888 if (entry.rxBytes != 0) {
8889 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8890 entry.rxPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08008891 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
8892 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
8893 entry.rxPackets);
8894 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08008895 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8896 entry.rxBytes);
8897 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8898 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008899
Adam Lesinskiba88e682015-12-08 12:06:55 -08008900 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008901
Adam Lesinskiba88e682015-12-08 12:06:55 -08008902 // Sum the total number of packets so that the Rx Power can
8903 // be evenly distributed amongst the apps.
8904 totalRxPackets += entry.rxPackets;
8905 }
8906
8907 if (entry.txBytes != 0) {
8908 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8909 entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08008910 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
8911 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
8912 entry.txPackets);
8913 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08008914 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8915 entry.txBytes);
8916 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8917 entry.txPackets);
8918
8919 txPackets.put(u.getUid(), entry.txPackets);
8920
8921 // Sum the total number of packets so that the Tx Power can
8922 // be evenly distributed amongst the apps.
8923 totalTxPackets += entry.txPackets;
8924 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008925 }
8926 }
8927
8928 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008929 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008930
Adam Lesinskie08af192015-03-25 16:42:59 -07008931 // Measured in mAms
8932 final long txTimeMs = info.getControllerTxTimeMillis();
8933 final long rxTimeMs = info.getControllerRxTimeMillis();
8934 final long idleTimeMs = info.getControllerIdleTimeMillis();
8935 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8936
8937 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008938 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008939
8940 if (DEBUG_ENERGY) {
8941 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8942 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8943 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8944 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8945 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8946 }
8947
8948 long totalWifiLockTimeMs = 0;
8949 long totalScanTimeMs = 0;
8950
8951 // On the first pass, collect some totals so that we can normalize power
8952 // calculations if we need to.
8953 final int uidStatsSize = mUidStats.size();
8954 for (int i = 0; i < uidStatsSize; i++) {
8955 final Uid uid = mUidStats.valueAt(i);
8956
8957 // Sum the total scan power for all apps.
8958 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8959 elapsedRealtimeMs * 1000) / 1000;
8960
8961 // Sum the total time holding wifi lock for all apps.
8962 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8963 elapsedRealtimeMs * 1000) / 1000;
8964 }
8965
8966 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8967 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8968 + rxTimeMs + " ms). Normalizing scan time.");
8969 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008970 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8971 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8972 + txTimeMs + " ms). Normalizing scan time.");
8973 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008974
8975 // Actually assign and distribute power usage to apps.
8976 for (int i = 0; i < uidStatsSize; i++) {
8977 final Uid uid = mUidStats.valueAt(i);
8978
8979 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8980 elapsedRealtimeMs * 1000) / 1000;
8981 if (scanTimeSinceMarkMs > 0) {
8982 // Set the new mark so that next time we get new data since this point.
8983 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8984
Mitchell Willsf9016492015-07-29 17:47:44 -07008985 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8986 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8987
8988 // Our total scan time is more than the reported Tx/Rx time.
8989 // This is possible because the cost of a scan is approximate.
8990 // Let's normalize the result so that we evenly blame each app
8991 // scanning.
8992 //
8993 // This means that we may have apps that transmitted/received packets not be
8994 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008995 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008996 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8997 totalScanTimeMs;
8998 }
8999 if (totalScanTimeMs > txTimeMs) {
9000 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9001 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009002 }
9003
9004 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07009005 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9006 + scanRxTimeSinceMarkMs + " ms Tx:"
9007 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07009008 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009009
9010 ControllerActivityCounterImpl activityCounter =
9011 uid.getOrCreateWifiControllerActivityLocked();
9012 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9013 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07009014 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9015 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009016 }
9017
9018 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9019 // lock.
9020 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9021 elapsedRealtimeMs * 1000) / 1000;
9022 if (wifiLockTimeSinceMarkMs > 0) {
9023 // Set the new mark so that next time we get new data since this point.
9024 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9025
9026 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9027 / totalWifiLockTimeMs;
9028 if (DEBUG_ENERGY) {
9029 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9030 + myIdleTimeMs + " ms");
9031 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009032 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9033 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009034 }
9035 }
9036
9037 if (DEBUG_ENERGY) {
9038 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07009039 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009040 }
9041
Mitchell Willsf9016492015-07-29 17:47:44 -07009042 // Distribute the remaining Tx power appropriately between all apps that transmitted
9043 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07009044 for (int i = 0; i < txPackets.size(); i++) {
9045 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07009046 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07009047 if (DEBUG_ENERGY) {
9048 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9049 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009050 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9051 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009052 }
9053
9054 // Distribute the remaining Rx power appropriately between all apps that received
9055 // packets.
9056 for (int i = 0; i < rxPackets.size(); i++) {
9057 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9058 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
9059 if (DEBUG_ENERGY) {
9060 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9061 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009062 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9063 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009064 }
9065
9066 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9067
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009068 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009069 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9070 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
9071 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07009072
Adam Lesinski8576cf92015-06-09 12:48:25 -07009073 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9074 final double opVolt = mPowerProfile.getAveragePower(
9075 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9076 if (opVolt != 0) {
9077 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009078 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07009079 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07009080 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009081 }
9082 }
9083
9084 /**
9085 * Distribute Cell radio energy info and network traffic to apps.
9086 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009087 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
9088 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009089 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009090 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009091 }
9092
Adam Lesinskie08af192015-03-25 16:42:59 -07009093 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009094 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009095 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
9096 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
9097 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009098 } catch (IOException e) {
9099 Slog.wtf(TAG, "Failed to get mobile network stats", e);
9100 return;
9101 }
9102
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009103 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009104 return;
9105 }
9106
Adam Lesinskie08af192015-03-25 16:42:59 -07009107 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9108 elapsedRealtimeMs * 1000);
9109 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009110
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009111 long totalRxPackets = 0;
9112 long totalTxPackets = 0;
9113 if (delta != null) {
9114 final int size = delta.size();
9115 for (int i = 0; i < size; i++) {
9116 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08009117 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009118 continue;
9119 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009120
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009121 if (DEBUG_ENERGY) {
9122 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9123 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9124 + " txPackets=" + entry.txPackets);
9125 }
9126
9127 totalRxPackets += entry.rxPackets;
9128 totalTxPackets += entry.txPackets;
9129
9130 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9131 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
9132 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009133 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9134 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
9135 entry.rxBytes, entry.rxPackets);
9136 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
9137 entry.txBytes, entry.txPackets);
9138 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009139
9140 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9141 entry.rxBytes);
9142 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9143 entry.txBytes);
9144 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9145 entry.rxPackets);
9146 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9147 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009148 }
9149
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009150 // Now distribute proportional blame to the apps that did networking.
9151 long totalPackets = totalRxPackets + totalTxPackets;
9152 if (totalPackets > 0) {
9153 for (int i = 0; i < size; i++) {
9154 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9155 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9156 continue;
9157 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009158
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009159 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9160
9161 // Distribute total radio active time in to this app.
9162 final long appPackets = entry.rxPackets + entry.txPackets;
9163 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9164 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9165
9166 // Remove this app from the totals, so that we don't lose any time
9167 // due to rounding.
9168 radioTime -= appRadioTime;
9169 totalPackets -= appPackets;
9170
9171 if (activityInfo != null) {
9172 ControllerActivityCounterImpl activityCounter =
9173 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08009174 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009175 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9176 / totalRxPackets;
9177 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9178 }
9179
Adam Lesinskia4268172016-01-29 12:13:54 -08009180 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009181 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9182 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9183 txMs /= totalTxPackets;
9184 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9185 }
9186 }
9187 }
9188 }
9189 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009190
9191 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009192 // Whoops, there is some radio time we can't blame on an app!
9193 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9194 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009195 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009196 }
9197
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009198 if (activityInfo != null) {
9199 mHasModemReporting = true;
9200 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
9201 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9202 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9203 mModemActivity.getTxTimeCounters()[lvl]
9204 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9205 }
9206
9207 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9208 final double opVolt = mPowerProfile.getAveragePower(
9209 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9210 if (opVolt != 0) {
9211 // We store the power drain as mAms.
9212 mModemActivity.getPowerCounter().addCountLocked(
9213 (long) (activityInfo.getEnergyUsed() / opVolt));
9214 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009215 }
9216 }
9217
9218 /**
9219 * Distribute Bluetooth energy info and network traffic to apps.
9220 * @param info The energy information from the bluetooth controller.
9221 */
9222 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009223 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009224 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009225 }
9226
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009227 if (info == null || !mOnBatteryInternal) {
9228 return;
9229 }
Adam Lesinskie283d332015-04-16 12:29:25 -07009230
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009231 mHasBluetoothReporting = true;
9232
9233 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
9234 final long rxTimeMs = info.getControllerRxTimeMillis();
9235 final long txTimeMs = info.getControllerTxTimeMillis();
9236
9237 if (DEBUG_ENERGY) {
9238 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
9239 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9240 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9241 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
9242 }
9243
9244 long totalScanTimeMs = 0;
9245
9246 final int uidCount = mUidStats.size();
9247 for (int i = 0; i < uidCount; i++) {
9248 final Uid u = mUidStats.valueAt(i);
9249 if (u.mBluetoothScanTimer == null) {
9250 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07009251 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009252
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009253 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
9254 elapsedRealtimeMs * 1000) / 1000;
9255 }
9256
9257 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
9258 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
9259
9260 if (DEBUG_ENERGY) {
9261 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
9262 + " TX=" + normalizeScanTxTime);
9263 }
9264
9265 long leftOverRxTimeMs = rxTimeMs;
9266 long leftOverTxTimeMs = txTimeMs;
9267
9268 for (int i = 0; i < uidCount; i++) {
9269 final Uid u = mUidStats.valueAt(i);
9270 if (u.mBluetoothScanTimer == null) {
9271 continue;
9272 }
9273
9274 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
9275 elapsedRealtimeMs * 1000) / 1000;
9276 if (scanTimeSinceMarkMs > 0) {
9277 // Set the new mark so that next time we get new data since this point.
9278 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
9279
9280 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
9281 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
9282
9283 if (normalizeScanRxTime) {
9284 // Scan time is longer than the total rx time in the controller,
9285 // so distribute the scan time proportionately. This means regular traffic
9286 // will not blamed, but scans are more expensive anyways.
9287 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
9288 }
9289
9290 if (normalizeScanTxTime) {
9291 // Scan time is longer than the total tx time in the controller,
9292 // so distribute the scan time proportionately. This means regular traffic
9293 // will not blamed, but scans are more expensive anyways.
9294 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
9295 }
9296
9297 final ControllerActivityCounterImpl counter =
9298 u.getOrCreateBluetoothControllerActivityLocked();
9299 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
9300 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
9301
9302 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
9303 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
9304 }
9305 }
9306
9307 if (DEBUG_ENERGY) {
9308 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
9309 + " TX=" + leftOverTxTimeMs);
9310 }
9311
9312 //
9313 // Now distribute blame to apps that did bluetooth traffic.
9314 //
9315
9316 long totalTxBytes = 0;
9317 long totalRxBytes = 0;
9318
9319 final UidTraffic[] uidTraffic = info.getUidTraffic();
9320 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9321 for (int i = 0; i < numUids; i++) {
9322 final UidTraffic traffic = uidTraffic[i];
9323
9324 // Add to the global counters.
9325 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9326 traffic.getRxBytes());
9327 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9328 traffic.getTxBytes());
9329
9330 // Add to the UID counters.
9331 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9332 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9333 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9334
9335 // Calculate the total traffic.
9336 totalTxBytes += traffic.getTxBytes();
9337 totalRxBytes += traffic.getRxBytes();
9338 }
9339
9340 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9341 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009342 for (int i = 0; i < numUids; i++) {
9343 final UidTraffic traffic = uidTraffic[i];
9344
Adam Lesinski50e47602015-12-04 17:04:54 -08009345 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009346 final ControllerActivityCounterImpl counter =
9347 u.getOrCreateBluetoothControllerActivityLocked();
9348
9349 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
9350 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9351
9352 if (DEBUG_ENERGY) {
9353 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
9354 + " rx_time=" + timeRxMs);
9355 }
9356 counter.getRxTimeCounter().addCountLocked(timeRxMs);
9357 leftOverRxTimeMs -= timeRxMs;
9358 }
9359
9360 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
9361 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
9362
9363 if (DEBUG_ENERGY) {
9364 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
9365 + " tx_time=" + timeTxMs);
9366 }
9367
9368 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
9369 leftOverTxTimeMs -= timeTxMs;
9370 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009371 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009372 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009373
9374 mBluetoothActivity.getRxTimeCounter().addCountLocked(
9375 info.getControllerRxTimeMillis());
9376 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
9377 info.getControllerTxTimeMillis());
9378 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
9379 info.getControllerIdleTimeMillis());
9380
9381 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9382 final double opVolt = mPowerProfile.getAveragePower(
9383 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9384 if (opVolt != 0) {
9385 // We store the power drain as mAms.
9386 mBluetoothActivity.getPowerCounter().addCountLocked(
9387 (long) (info.getControllerEnergyUsed() / opVolt));
9388 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009389 }
9390
9391 /**
9392 * Read and distribute kernel wake lock use across apps.
9393 */
9394 public void updateKernelWakelocksLocked() {
9395 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
9396 mTmpWakelockStats);
9397 if (wakelockStats == null) {
9398 // Not crashing might make board bringup easier.
9399 Slog.w(TAG, "Couldn't get kernel wake lock stats");
9400 return;
9401 }
9402
9403 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
9404 String name = ent.getKey();
9405 KernelWakelockStats.Entry kws = ent.getValue();
9406
9407 SamplingTimer kwlt = mKernelWakelockStats.get(name);
9408 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009409 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009410 mKernelWakelockStats.put(name, kwlt);
9411 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07009412
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009413 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009414 kwlt.setUpdateVersion(kws.mVersion);
9415 }
9416
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009417 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07009418 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
9419 // this time.
9420 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9421 SamplingTimer st = ent.getValue();
9422 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
9423 st.endSample();
9424 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009425 }
9426 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009427
Adam Lesinskid84ad302016-05-17 18:31:02 -07009428 // Record whether we've seen a non-zero time (for debugging b/22716723).
9429 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009430 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
9431 }
9432
9433 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
9434 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
9435 wakelockStats.kernelWakelockVersion);
9436 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009437 }
9438
Adam Lesinski72478f02015-06-17 15:39:43 -07009439 // We use an anonymous class to access these variables,
9440 // so they can't live on the stack or they'd have to be
9441 // final MutableLong objects (more allocations).
9442 // Used in updateCpuTimeLocked().
9443 long mTempTotalCpuUserTimeUs;
9444 long mTempTotalCpuSystemTimeUs;
9445
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009446 /**
James Carr3a226052016-07-01 14:49:52 -07009447 * Reads the newest memory stats from the kernel.
9448 */
9449 public void updateKernelMemoryBandwidthLocked() {
9450 mKernelMemoryBandwidthStats.updateStats();
9451 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
9452 final int bandwidthEntryCount = bandwidthEntries.size();
9453 int index;
9454 for (int i = 0; i < bandwidthEntryCount; i++) {
9455 SamplingTimer timer;
9456 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
9457 timer = mKernelMemoryStats.valueAt(index);
9458 } else {
9459 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
9460 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
9461 }
9462 timer.update(bandwidthEntries.valueAt(i), 1);
9463 if (DEBUG_MEMORY) {
9464 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
9465 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
9466 mKernelMemoryStats.get(
9467 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
9468 mKernelMemoryStats.size()));
9469 }
9470 }
9471 }
9472
9473 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07009474 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
9475 * and we are on battery with screen off, we give more of the cpu time to those apps holding
9476 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009477 */
Adam Lesinski72478f02015-06-17 15:39:43 -07009478 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009479 if (mPowerProfile == null) {
9480 return;
9481 }
9482
Adam Lesinski72478f02015-06-17 15:39:43 -07009483 if (DEBUG_ENERGY_CPU) {
9484 Slog.d(TAG, "!Cpu updating!");
9485 }
9486
9487 // Holding a wakelock costs more than just using the cpu.
9488 // Currently, we assign only half the cpu time to an app that is running but
9489 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
9490 // If no app is holding a wakelock, then the distribution is normal.
9491 final int wakelockWeight = 50;
9492
Adam Lesinski6832f392015-09-05 18:05:40 -07009493 // Read the time spent for each cluster at various cpu frequencies.
9494 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
9495 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
9496 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
9497 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009498
9499 int numWakelocks = 0;
9500
9501 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
9502 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
9503 final int numPartialTimers = mPartialTimers.size();
9504 if (mOnBatteryScreenOffTimeBase.isRunning()) {
9505 for (int i = 0; i < numPartialTimers; i++) {
9506 final StopwatchTimer timer = mPartialTimers.get(i);
9507 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9508 // Since the collection and blaming of wakelocks can be scheduled to run after
9509 // some delay, the mPartialTimers list may have new entries. We can't blame
9510 // the newly added timer for past cpu time, so we only consider timers that
9511 // were present for one round of collection. Once a timer has gone through
9512 // a round of collection, its mInList field is set to true.
9513 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009514 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009515 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009516 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009517
9518 final int numWakelocksF = numWakelocks;
9519 mTempTotalCpuUserTimeUs = 0;
9520 mTempTotalCpuSystemTimeUs = 0;
9521
9522 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9523 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9524 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009525 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009526 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9527 new KernelUidCpuTimeReader.Callback() {
9528 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009529 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
9530 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009531 final Uid u = getUidStatsLocked(mapUid(uid));
9532
9533 // Accumulate the total system and user time.
9534 mTempTotalCpuUserTimeUs += userTimeUs;
9535 mTempTotalCpuSystemTimeUs += systemTimeUs;
9536
9537 StringBuilder sb = null;
9538 if (DEBUG_ENERGY_CPU) {
9539 sb = new StringBuilder();
9540 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9541 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9542 sb.append(" s=");
9543 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009544 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009545 }
9546
9547 if (numWakelocksF > 0) {
9548 // We have wakelocks being held, so only give a portion of the
9549 // time to the process. The rest will be distributed among wakelock
9550 // holders.
9551 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9552 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9553 }
9554
9555 if (sb != null) {
9556 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9557 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9558 sb.append(" s=");
9559 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009560 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009561 Slog.d(TAG, sb.toString());
9562 }
9563
9564 u.mUserCpuTime.addCountLocked(userTimeUs);
9565 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009566 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009567
9568 // Add the cpu speeds to this UID. These are used as a ratio
9569 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009570 final int numClusters = mPowerProfile.getNumCpuClusters();
9571 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9572 numClusters) {
9573 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009574 }
9575
9576 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009577 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9578 cluster);
9579 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9580 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009581 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009582 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009583 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009584
9585 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9586 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9587 if (cpuSpeeds[speed] == null) {
9588 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9589 }
9590 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9591 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009592 }
9593 }
9594 });
9595
Amith Yamasanid2450862017-02-07 15:58:24 -08009596 // TODO: STOPSHIP, remove the "true" below after b/34961340 is fixed
9597 if (DEBUG_ENERGY_CPU || true) {
Joe Onoratoabded112016-02-08 16:49:39 -08009598 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009599 " ms");
9600 }
9601
9602 if (mOnBatteryInternal && numWakelocks > 0) {
9603 // Distribute a portion of the total cpu time to wakelock holders.
9604 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9605 mTempTotalCpuSystemTimeUs =
9606 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9607
9608 for (int i = 0; i < numPartialTimers; i++) {
9609 final StopwatchTimer timer = mPartialTimers.get(i);
9610
9611 // The system does not share any blame, as it is usually holding the wakelock
9612 // on behalf of an app.
9613 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9614 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9615 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9616
9617 if (DEBUG_ENERGY_CPU) {
9618 StringBuilder sb = new StringBuilder();
9619 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9620 .append(": u=");
9621 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9622 sb.append(" s=");
9623 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9624 Slog.d(TAG, sb.toString());
9625 }
9626
9627 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9628 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9629
9630 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009631 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009632
9633 mTempTotalCpuUserTimeUs -= userTimeUs;
9634 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9635 numWakelocks--;
9636 }
9637 }
9638
9639 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9640 // Anything left over is given to the system.
9641 if (DEBUG_ENERGY_CPU) {
9642 StringBuilder sb = new StringBuilder();
9643 sb.append(" Distributing lost time to system: u=");
9644 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9645 sb.append(" s=");
9646 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9647 Slog.d(TAG, sb.toString());
9648 }
9649
9650 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9651 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9652 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9653
9654 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009655 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9656 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009657 }
9658 }
9659
9660 // See if there is a difference in wakelocks between this collection and the last
9661 // collection.
9662 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9663 // No difference, so each timer is now considered for the next collection.
9664 for (int i = 0; i < numPartialTimers; i++) {
9665 mPartialTimers.get(i).mInList = true;
9666 }
9667 } else {
9668 // The lists are different, meaning we added (or removed a timer) since the last
9669 // collection.
9670 final int numLastPartialTimers = mLastPartialTimers.size();
9671 for (int i = 0; i < numLastPartialTimers; i++) {
9672 mLastPartialTimers.get(i).mInList = false;
9673 }
9674 mLastPartialTimers.clear();
9675
9676 // Mark the current timers as gone through a collection.
9677 for (int i = 0; i < numPartialTimers; i++) {
9678 final StopwatchTimer timer = mPartialTimers.get(i);
9679 timer.mInList = true;
9680 mLastPartialTimers.add(timer);
9681 }
9682 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009683 }
9684
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009685 boolean setChargingLocked(boolean charging) {
9686 if (mCharging != charging) {
9687 mCharging = charging;
9688 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009689 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009690 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009691 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009692 }
9693 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9694 return true;
9695 }
9696 return false;
9697 }
9698
Dianne Hackborn40c87252014-03-19 16:55:40 -07009699 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009700 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009701 boolean doWrite = false;
9702 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9703 m.arg1 = onBattery ? 1 : 0;
9704 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009705
Dianne Hackborn40c87252014-03-19 16:55:40 -07009706 final long uptime = mSecUptime * 1000;
9707 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009708 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009709 if (onBattery) {
9710 // We will reset our status if we are unplugging after the
9711 // battery was last full, or the level is at 100, or
9712 // we have gone through a significant charge (from a very low
9713 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009714 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009715 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009716 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009717 || (mDischargeCurrentLevel < 20 && level >= 80)
9718 || (getHighDischargeAmountSinceCharge() >= 200
9719 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009720 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009721 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009722 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9723 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009724 // Before we write, collect a snapshot of the final aggregated
9725 // stats to be reported in the next checkin. Only do this if we have
9726 // a sufficient amount of data to make it interesting.
9727 if (getLowDischargeAmountSinceCharge() >= 20) {
9728 final Parcel parcel = Parcel.obtain();
9729 writeSummaryToParcel(parcel, true);
9730 BackgroundThread.getHandler().post(new Runnable() {
9731 @Override public void run() {
9732 synchronized (mCheckinFile) {
9733 FileOutputStream stream = null;
9734 try {
9735 stream = mCheckinFile.startWrite();
9736 stream.write(parcel.marshall());
9737 stream.flush();
9738 FileUtils.sync(stream);
9739 stream.close();
9740 mCheckinFile.finishWrite(stream);
9741 } catch (IOException e) {
9742 Slog.w("BatteryStats",
9743 "Error writing checkin battery statistics", e);
9744 mCheckinFile.failWrite(stream);
9745 } finally {
9746 parcel.recycle();
9747 }
9748 }
9749 }
9750 });
9751 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009752 doWrite = true;
9753 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -08009754 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009755 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -08009756 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009757 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009758 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009759 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009760 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009761 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009762 if (mCharging) {
9763 setChargingLocked(false);
9764 }
9765 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009766 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009767 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009768 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009769 mDischargeStepTracker.clearTime();
9770 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009771 mInitStepMode = mCurStepMode;
9772 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009773 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009774 mHistoryCur.batteryLevel = (byte)level;
9775 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9776 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9777 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009778 if (reset) {
9779 mRecordingHistory = true;
9780 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9781 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009782 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009783 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009784 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009785 mDischargeScreenOnUnplugLevel = level;
9786 mDischargeScreenOffUnplugLevel = 0;
9787 } else {
9788 mDischargeScreenOnUnplugLevel = 0;
9789 mDischargeScreenOffUnplugLevel = level;
9790 }
9791 mDischargeAmountScreenOn = 0;
9792 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009793 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009794 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009795 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009796 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009797 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009798 mHistoryCur.batteryLevel = (byte)level;
9799 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9800 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9801 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009802 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009803 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009804 if (level < mDischargeUnplugLevel) {
9805 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9806 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9807 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009808 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9809 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009810 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009811 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009812 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009813 mInitStepMode = mCurStepMode;
9814 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009815 }
9816 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9817 if (mFile != null) {
9818 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 }
9820 }
9821 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009822
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009823 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9824 boolean reset) {
9825 mRecordingHistory = true;
9826 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009827 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9828 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009829 mHistoryCur);
9830 mHistoryCur.currentTime = 0;
9831 if (reset) {
9832 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9833 }
9834 }
9835
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009836 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9837 final long uptimeMs) {
9838 if (mRecordingHistory) {
9839 mHistoryCur.currentTime = currentTime;
9840 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9841 mHistoryCur);
9842 mHistoryCur.currentTime = 0;
9843 }
9844 }
9845
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009846 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9847 if (mRecordingHistory) {
9848 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009849 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9850 mHistoryCur);
9851 mHistoryCur.currentTime = 0;
9852 }
9853 }
9854
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009855 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009856 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009857 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009858 }
9859 }
9860
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009861 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009862 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009863
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009864 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -08009865 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009866 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009867 final long uptime = mClocks.uptimeMillis();
9868 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009869 if (!mHaveBatteryLevel) {
9870 mHaveBatteryLevel = true;
9871 // We start out assuming that the device is plugged in (not
9872 // on battery). If our first report is now that we are indeed
9873 // plugged in, then twiddle our state to correctly reflect that
9874 // since we won't be going through the full setOnBattery().
9875 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009876 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009877 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009878 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009879 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009880 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009881 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009882 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009883 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009884 mHistoryCur.batteryStatus = (byte)status;
9885 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009886 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009887 mMaxChargeStepLevel = mMinDischargeStepLevel =
9888 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009889 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009890 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9891 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9892 }
9893 int oldStatus = mHistoryCur.batteryStatus;
9894 if (onBattery) {
9895 mDischargeCurrentLevel = level;
9896 if (!mRecordingHistory) {
9897 mRecordingHistory = true;
9898 startRecordingHistory(elapsedRealtime, uptime, true);
9899 }
9900 } else if (level < 96) {
9901 if (!mRecordingHistory) {
9902 mRecordingHistory = true;
9903 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009904 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009905 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009906 mCurrentBatteryLevel = level;
9907 if (mDischargePlugLevel < 0) {
9908 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009909 }
Adam Lesinski926969b2016-04-28 17:31:12 -07009910
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009911 if (onBattery != mOnBattery) {
9912 mHistoryCur.batteryLevel = (byte)level;
9913 mHistoryCur.batteryStatus = (byte)status;
9914 mHistoryCur.batteryHealth = (byte)health;
9915 mHistoryCur.batteryPlugType = (byte)plugType;
9916 mHistoryCur.batteryTemperature = (short)temp;
9917 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009918 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9919 // Only record discharges
9920 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9921 mDischargeCounter.addCountLocked(chargeDiff);
9922 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9923 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009924 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009925 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009926 } else {
9927 boolean changed = false;
9928 if (mHistoryCur.batteryLevel != level) {
9929 mHistoryCur.batteryLevel = (byte)level;
9930 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009931
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009932 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9933 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009934 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009935 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009936 if (mHistoryCur.batteryStatus != status) {
9937 mHistoryCur.batteryStatus = (byte)status;
9938 changed = true;
9939 }
9940 if (mHistoryCur.batteryHealth != health) {
9941 mHistoryCur.batteryHealth = (byte)health;
9942 changed = true;
9943 }
9944 if (mHistoryCur.batteryPlugType != plugType) {
9945 mHistoryCur.batteryPlugType = (byte)plugType;
9946 changed = true;
9947 }
9948 if (temp >= (mHistoryCur.batteryTemperature+10)
9949 || temp <= (mHistoryCur.batteryTemperature-10)) {
9950 mHistoryCur.batteryTemperature = (short)temp;
9951 changed = true;
9952 }
9953 if (volt > (mHistoryCur.batteryVoltage+20)
9954 || volt < (mHistoryCur.batteryVoltage-20)) {
9955 mHistoryCur.batteryVoltage = (char)volt;
9956 changed = true;
9957 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009958 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
9959 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009960 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9961 // Only record discharges
9962 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9963 mDischargeCounter.addCountLocked(chargeDiff);
9964 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9965 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009966 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07009967 changed = true;
9968 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009969 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9970 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9971 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9972 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009973 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009974 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9975 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9976 modeBits, elapsedRealtime);
9977 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9978 modeBits, elapsedRealtime);
9979 mLastDischargeStepLevel = level;
9980 mMinDischargeStepLevel = level;
9981 mInitStepMode = mCurStepMode;
9982 mModStepMode = 0;
9983 }
9984 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009985 if (level >= 90) {
9986 // If the battery level is at least 90%, always consider the device to be
9987 // charging even if it happens to go down a level.
9988 changed |= setChargingLocked(true);
9989 mLastChargeStepLevel = level;
9990 } if (!mCharging) {
9991 if (mLastChargeStepLevel < level) {
9992 // We have not reporting that we are charging, but the level has now
9993 // gone up, so consider the state to be charging.
9994 changed |= setChargingLocked(true);
9995 mLastChargeStepLevel = level;
9996 }
9997 } else {
9998 if (mLastChargeStepLevel > level) {
9999 // We had reported that the device was charging, but here we are with
10000 // power connected and the level going down. Looks like the current
10001 // power supplied isn't enough, so consider the device to now be
10002 // discharging.
10003 changed |= setChargingLocked(false);
10004 mLastChargeStepLevel = level;
10005 }
10006 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010007 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
10008 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10009 modeBits, elapsedRealtime);
10010 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10011 modeBits, elapsedRealtime);
10012 mLastChargeStepLevel = level;
10013 mMaxChargeStepLevel = level;
10014 mInitStepMode = mCurStepMode;
10015 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070010016 }
10017 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010018 if (changed) {
10019 addHistoryRecordLocked(elapsedRealtime, uptime);
10020 }
Evan Millarc64edde2009-04-18 12:26:32 -070010021 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010022 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10023 // We don't record history while we are plugged in and fully charged.
10024 // The next time we are unplugged, history will be cleared.
10025 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010026 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010027
10028 if (differsByMoreThan(chargeFullUAh, mLastChargeFullUAh, 100)) {
10029 mLastChargeFullUAh = chargeFullUAh;
10030 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ESTIMATED_BATTERY_CAP,
10031 "", chargeFullUAh / 1000);
10032 }
10033 }
10034
10035 private static boolean differsByMoreThan(int left, int right, int diff) {
10036 return Math.abs(left - right) > diff;
Adam Lesinski33dac552015-03-09 15:24:48 -070010037 }
10038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 public long getAwakeTimeBattery() {
10040 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
10041 }
10042
10043 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080010044 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 }
10046
10047 @Override
10048 public long computeUptime(long curTime, int which) {
10049 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010050 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010052 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 }
10054 return 0;
10055 }
10056
10057 @Override
10058 public long computeRealtime(long curTime, int which) {
10059 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010060 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010061 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010062 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 }
10064 return 0;
10065 }
10066
10067 @Override
10068 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010069 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010070 }
10071
10072 @Override
10073 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010074 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010075 }
10076
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010077 @Override
10078 public long computeBatteryScreenOffUptime(long curTime, int which) {
10079 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
10080 }
10081
10082 @Override
10083 public long computeBatteryScreenOffRealtime(long curTime, int which) {
10084 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 }
10086
Dianne Hackborn260c5022014-04-29 11:23:16 -070010087 private long computeTimePerLevel(long[] steps, int numSteps) {
10088 // For now we'll do a simple average across all steps.
10089 if (numSteps <= 0) {
10090 return -1;
10091 }
10092 long total = 0;
10093 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010094 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010095 }
10096 return total / numSteps;
10097 /*
10098 long[] buckets = new long[numSteps];
10099 int numBuckets = 0;
10100 int numToAverage = 4;
10101 int i = 0;
10102 while (i < numSteps) {
10103 long totalTime = 0;
10104 int num = 0;
10105 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010106 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010107 num++;
10108 }
10109 buckets[numBuckets] = totalTime / num;
10110 numBuckets++;
10111 numToAverage *= 2;
10112 i += num;
10113 }
10114 if (numBuckets < 1) {
10115 return -1;
10116 }
10117 long averageTime = buckets[numBuckets-1];
10118 for (i=numBuckets-2; i>=0; i--) {
10119 averageTime = (averageTime + buckets[i]) / 2;
10120 }
10121 return averageTime;
10122 */
10123 }
10124
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010125 @Override
10126 public long computeBatteryTimeRemaining(long curTime) {
10127 if (!mOnBattery) {
10128 return -1;
10129 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010130 /* Simple implementation just looks at the average discharge per level across the
10131 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010132 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
10133 if (discharge < 2) {
10134 return -1;
10135 }
10136 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
10137 if (duration < 1000*1000) {
10138 return -1;
10139 }
10140 long usPerLevel = duration/discharge;
10141 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010142 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010143 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010144 return -1;
10145 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010146 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010147 if (msPerLevel <= 0) {
10148 return -1;
10149 }
10150 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010151 }
10152
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010153 @Override
10154 public LevelStepTracker getDischargeLevelStepTracker() {
10155 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010156 }
10157
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010158 @Override
10159 public LevelStepTracker getDailyDischargeLevelStepTracker() {
10160 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010161 }
10162
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010163 @Override
10164 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010165 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010166 // Not yet working.
10167 return -1;
10168 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010169 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010170 int curLevel = mCurrentBatteryLevel;
10171 int plugLevel = mDischargePlugLevel;
10172 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
10173 return -1;
10174 }
10175 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
10176 if (duration < 1000*1000) {
10177 return -1;
10178 }
10179 long usPerLevel = duration/(curLevel-plugLevel);
10180 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070010181 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010182 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010183 return -1;
10184 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010185 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010186 if (msPerLevel <= 0) {
10187 return -1;
10188 }
10189 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010190 }
10191
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010192 @Override
10193 public LevelStepTracker getChargeLevelStepTracker() {
10194 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010195 }
10196
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010197 @Override
10198 public LevelStepTracker getDailyChargeLevelStepTracker() {
10199 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010200 }
10201
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010202 @Override
10203 public ArrayList<PackageChange> getDailyPackageChanges() {
10204 return mDailyPackageChanges;
10205 }
10206
Joe Onoratoe1acd632016-02-23 13:25:10 -080010207 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010208 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010209 }
10210
10211 @Override
10212 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010213 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 }
10215
10216 @Override
10217 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010218 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070010220
The Android Open Source Project10592532009-03-18 17:39:46 -070010221 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010222 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010223 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010224 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010225 }
10226 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010227
Evan Millar633a1742009-04-02 16:36:33 -070010228 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010229 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010230 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010231
The Android Open Source Project10592532009-03-18 17:39:46 -070010232 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010233 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010234 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010235 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010236 }
10237 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010238
Evan Millar633a1742009-04-02 16:36:33 -070010239 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010240 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010242
Amith Yamasanie43530a2009-08-21 13:11:37 -070010243 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010244 public int getLowDischargeAmountSinceCharge() {
10245 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010246 int val = mLowDischargeAmountSinceCharge;
10247 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10248 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
10249 }
10250 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010251 }
10252 }
10253
10254 @Override
10255 public int getHighDischargeAmountSinceCharge() {
10256 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010257 int val = mHighDischargeAmountSinceCharge;
10258 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10259 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
10260 }
10261 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010262 }
10263 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010264
10265 @Override
10266 public int getDischargeAmount(int which) {
10267 int dischargeAmount = which == STATS_SINCE_CHARGED
10268 ? getHighDischargeAmountSinceCharge()
10269 : (getDischargeStartLevel() - getDischargeCurrentLevel());
10270 if (dischargeAmount < 0) {
10271 dischargeAmount = 0;
10272 }
10273 return dischargeAmount;
10274 }
10275
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010276 public int getDischargeAmountScreenOn() {
10277 synchronized(this) {
10278 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010279 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010280 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10281 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10282 }
10283 return val;
10284 }
10285 }
10286
10287 public int getDischargeAmountScreenOnSinceCharge() {
10288 synchronized(this) {
10289 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010290 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010291 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10292 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10293 }
10294 return val;
10295 }
10296 }
10297
10298 public int getDischargeAmountScreenOff() {
10299 synchronized(this) {
10300 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010301 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010302 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10303 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10304 }
10305 return val;
10306 }
10307 }
10308
10309 public int getDischargeAmountScreenOffSinceCharge() {
10310 synchronized(this) {
10311 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010312 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010313 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10314 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10315 }
10316 return val;
10317 }
10318 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 /**
10321 * Retrieve the statistics object for a particular uid, creating if needed.
10322 */
10323 public Uid getUidStatsLocked(int uid) {
10324 Uid u = mUidStats.get(uid);
10325 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080010326 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 mUidStats.put(uid, u);
10328 }
10329 return u;
10330 }
10331
10332 /**
10333 * Remove the statistics object for a particular uid.
10334 */
10335 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070010336 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 mUidStats.remove(uid);
10338 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070010339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 /**
10341 * Retrieve the statistics object for a particular process, creating
10342 * if needed.
10343 */
10344 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010345 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 Uid u = getUidStatsLocked(uid);
10347 return u.getProcessStatsLocked(name);
10348 }
10349
10350 /**
10351 * Retrieve the statistics object for a particular process, creating
10352 * if needed.
10353 */
10354 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010355 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 Uid u = getUidStatsLocked(uid);
10357 return u.getPackageStatsLocked(pkg);
10358 }
10359
10360 /**
10361 * Retrieve the statistics object for a particular service, creating
10362 * if needed.
10363 */
10364 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010365 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 Uid u = getUidStatsLocked(uid);
10367 return u.getServiceStatsLocked(pkg, name);
10368 }
10369
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010370 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010371 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010372 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010373 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010374 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010375
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010376 Parcel mPendingWrite = null;
10377 final ReentrantLock mWriteLock = new ReentrantLock();
10378
10379 public void writeAsyncLocked() {
10380 writeLocked(false);
10381 }
10382
10383 public void writeSyncLocked() {
10384 writeLocked(true);
10385 }
10386
10387 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010388 if (mFile == null) {
10389 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 return;
10391 }
10392
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010393 if (mShuttingDown) {
10394 return;
10395 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010396
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010397 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010398 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080010399 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010400
10401 if (mPendingWrite != null) {
10402 mPendingWrite.recycle();
10403 }
10404 mPendingWrite = out;
10405
10406 if (sync) {
10407 commitPendingDataToDisk();
10408 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010409 BackgroundThread.getHandler().post(new Runnable() {
10410 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010411 commitPendingDataToDisk();
10412 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010413 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010414 }
10415 }
10416
10417 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010418 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010419 synchronized (this) {
10420 next = mPendingWrite;
10421 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010422 if (next == null) {
10423 return;
10424 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010425 }
10426
Amith Yamasanid2450862017-02-07 15:58:24 -080010427 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010429 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010430 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070010432 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010434 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010436 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010437 mFile.rollback();
10438 } finally {
10439 next.recycle();
10440 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070010441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 }
10443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010445 if (mDailyFile != null) {
10446 readDailyStatsLocked();
10447 }
10448
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010449 if (mFile == null) {
10450 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 return;
10452 }
10453
10454 mUidStats.clear();
10455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010457 File file = mFile.chooseForRead();
10458 if (!file.exists()) {
10459 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010460 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010461 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010463 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010464 Parcel in = Parcel.obtain();
10465 in.unmarshall(raw, 0, raw.length);
10466 in.setDataPosition(0);
10467 stream.close();
10468
10469 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080010470 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010471 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010472 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010474
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010475 mEndPlatformVersion = Build.ID;
10476
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010477 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010478 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080010479 final long elapsedRealtime = mClocks.elapsedRealtime();
10480 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010481 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010482 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010483 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010484 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
10485 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010486 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010487
10488 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 }
10490
10491 public int describeContents() {
10492 return 0;
10493 }
10494
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010495 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070010496 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010497
10498 mHistoryBuffer.setDataSize(0);
10499 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010500 mHistoryTagPool.clear();
10501 mNextHistoryTagIdx = 0;
10502 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010503
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010504 int numTags = in.readInt();
10505 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080010506 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010507 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010508 if (str == null) {
10509 throw new ParcelFormatException("null history tag string");
10510 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010511 int uid = in.readInt();
10512 HistoryTag tag = new HistoryTag();
10513 tag.string = str;
10514 tag.uid = uid;
10515 tag.poolIdx = idx;
10516 mHistoryTagPool.put(tag, idx);
10517 if (idx >= mNextHistoryTagIdx) {
10518 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010519 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010520 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010521 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010522
10523 int bufSize = in.readInt();
10524 int curPos = in.dataPosition();
10525 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010526 throw new ParcelFormatException("File corrupt: history data buffer too large " +
10527 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010528 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010529 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
10530 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010531 } else {
10532 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
10533 + " bytes at " + curPos);
10534 mHistoryBuffer.appendFrom(in, curPos, bufSize);
10535 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010536 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010537
Dianne Hackbornae384452011-06-28 12:33:48 -070010538 if (andOldHistory) {
10539 readOldHistory(in);
10540 }
10541
10542 if (DEBUG_HISTORY) {
10543 StringBuilder sb = new StringBuilder(128);
10544 sb.append("****************** OLD mHistoryBaseTime: ");
10545 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10546 Slog.i(TAG, sb.toString());
10547 }
10548 mHistoryBaseTime = historyBaseTime;
10549 if (DEBUG_HISTORY) {
10550 StringBuilder sb = new StringBuilder(128);
10551 sb.append("****************** NEW mHistoryBaseTime: ");
10552 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10553 Slog.i(TAG, sb.toString());
10554 }
10555
10556 // We are just arbitrarily going to insert 1 minute from the sample of
10557 // the last run until samples in this run.
10558 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080010559 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010560 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070010561 if (DEBUG_HISTORY) {
10562 StringBuilder sb = new StringBuilder(128);
10563 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
10564 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10565 Slog.i(TAG, sb.toString());
10566 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070010567 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010568 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010569
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010570 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010571 if (!USE_OLD_HISTORY) {
10572 return;
10573 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010574 mHistory = mHistoryEnd = mHistoryCache = null;
10575 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010576 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010577 HistoryItem rec = new HistoryItem(time, in);
10578 addHistoryRecordLocked(rec);
10579 }
10580 }
10581
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010582 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010583 if (DEBUG_HISTORY) {
10584 StringBuilder sb = new StringBuilder(128);
10585 sb.append("****************** WRITING mHistoryBaseTime: ");
10586 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010587 sb.append(" mLastHistoryElapsedRealtime: ");
10588 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010589 Slog.i(TAG, sb.toString());
10590 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010591 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010592 if (!inclData) {
10593 out.writeInt(0);
10594 out.writeInt(0);
10595 return;
10596 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010597 out.writeInt(mHistoryTagPool.size());
10598 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10599 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010600 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010601 out.writeString(tag.string);
10602 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010603 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010604 out.writeInt(mHistoryBuffer.dataSize());
10605 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10606 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10607 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010608
10609 if (andOldHistory) {
10610 writeOldHistory(out);
10611 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010612 }
10613
10614 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010615 if (!USE_OLD_HISTORY) {
10616 return;
10617 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010618 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010619 while (rec != null) {
10620 if (rec.time >= 0) rec.writeToParcel(out, 0);
10621 rec = rec.next;
10622 }
10623 out.writeLong(-1);
10624 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010625
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010626 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 final int version = in.readInt();
10628 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010629 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 + ", expected " + VERSION + "; erasing old stats");
10631 return;
10632 }
10633
Dianne Hackbornae384452011-06-28 12:33:48 -070010634 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010639 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010640 mStartPlatformVersion = in.readString();
10641 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010642 mOnBatteryTimeBase.readSummaryFromParcel(in);
10643 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010644 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010645 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010646 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010647 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010648 mEstimatedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010649 mLowDischargeAmountSinceCharge = in.readInt();
10650 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010651 mDischargeAmountScreenOnSinceCharge = in.readInt();
10652 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010653 mDischargeStepTracker.readFromParcel(in);
10654 mChargeStepTracker.readFromParcel(in);
10655 mDailyDischargeStepTracker.readFromParcel(in);
10656 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010657 mDischargeCounter.readSummaryFromParcelLocked(in);
10658 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010659 int NPKG = in.readInt();
10660 if (NPKG > 0) {
10661 mDailyPackageChanges = new ArrayList<>(NPKG);
10662 while (NPKG > 0) {
10663 NPKG--;
10664 PackageChange pc = new PackageChange();
10665 pc.mPackageName = in.readString();
10666 pc.mUpdate = in.readInt() != 0;
10667 pc.mVersionCode = in.readInt();
10668 mDailyPackageChanges.add(pc);
10669 }
10670 } else {
10671 mDailyPackageChanges = null;
10672 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010673 mDailyStartTime = in.readLong();
10674 mNextMinDailyDeadline = in.readLong();
10675 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010678
Jeff Browne95c3cd2014-05-02 16:59:26 -070010679 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010680 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010681 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10682 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10683 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010684 mInteractive = false;
10685 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010687 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010688 mLongestLightIdleTime = in.readLong();
10689 mLongestFullIdleTime = in.readLong();
10690 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10691 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10692 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010693 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010694 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010695 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010696 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10697 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010698 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010699 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10700 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10701 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010702 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010703 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10704 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010705 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010706 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010707 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010708 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010709 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010710 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10711 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010712 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010713 mWifiOn = false;
10714 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010715 mGlobalWifiRunning = false;
10716 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010717 for (int i=0; i<NUM_WIFI_STATES; i++) {
10718 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10719 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010720 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10721 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10722 }
10723 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10724 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10725 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010726 mWifiActivity.readSummaryFromParcel(in);
10727 mBluetoothActivity.readSummaryFromParcel(in);
10728 mModemActivity.readSummaryFromParcel(in);
10729 mHasWifiReporting = in.readInt() != 0;
10730 mHasBluetoothReporting = in.readInt() != 0;
10731 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010732
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010733 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010734 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010735 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010736 mCameraOnNesting = 0;
10737 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010738 mBluetoothScanNesting = 0;
10739 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740
Evan Millarc64edde2009-04-18 12:26:32 -070010741 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010742 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010743 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010744 }
Evan Millarc64edde2009-04-18 12:26:32 -070010745 for (int ikw = 0; ikw < NKW; ikw++) {
10746 if (in.readInt() != 0) {
10747 String kwltName = in.readString();
10748 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10749 }
10750 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010751
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010752 int NWR = in.readInt();
10753 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010754 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010755 }
10756 for (int iwr = 0; iwr < NWR; iwr++) {
10757 if (in.readInt() != 0) {
10758 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010759 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010760 }
10761 }
10762
James Carr3a226052016-07-01 14:49:52 -070010763 int NMS = in.readInt();
10764 for (int ims = 0; ims < NMS; ims++) {
10765 if (in.readInt() != 0) {
10766 long kmstName = in.readLong();
10767 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
10768 }
10769 }
10770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010772 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010773 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 for (int iu = 0; iu < NU; iu++) {
10776 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010777 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 mUidStats.put(uid, u);
10779
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010780 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010781 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010782 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010783 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010784 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010785 if (in.readInt() != 0) {
10786 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10787 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010788 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010789 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010790 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010791 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010792 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10793 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10794 if (in.readInt() != 0) {
10795 u.makeWifiBatchedScanBin(i, null);
10796 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10797 }
10798 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010799 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010800 if (in.readInt() != 0) {
10801 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10802 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010803 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010804 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010805 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010806 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010807 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10808 }
10809 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010810 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10811 }
10812 if (in.readInt() != 0) {
10813 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10814 }
10815 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010816 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10817 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010818 if (in.readInt() != 0) {
10819 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10820 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010821 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010822 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10823 if (in.readInt() != 0) {
10824 u.makeProcessState(i, null);
10825 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10826 }
10827 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010828 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010829 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010830 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010831
Dianne Hackborn617f8772009-03-31 15:04:46 -070010832 if (in.readInt() != 0) {
10833 if (u.mUserActivityCounters == null) {
10834 u.initUserActivityLocked();
10835 }
10836 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10837 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10838 }
10839 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010840
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010841 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010842 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010843 u.initNetworkActivityLocked();
10844 }
10845 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010846 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10847 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010848 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010849 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10850 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010851 }
10852
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010853 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10854 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010855 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010856
Adam Lesinski6832f392015-09-05 18:05:40 -070010857 if (in.readInt() != 0) {
10858 final int numClusters = in.readInt();
10859 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10860 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010861 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010862
10863 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10864 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010865 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010866 final int NSB = in.readInt();
10867 if (mPowerProfile != null &&
10868 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10869 throw new ParcelFormatException("File corrupt: too many speed bins " +
10870 NSB);
10871 }
10872
Adam Lesinski6832f392015-09-05 18:05:40 -070010873 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10874 for (int speed = 0; speed < NSB; speed++) {
10875 if (in.readInt() != 0) {
10876 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10877 mOnBatteryTimeBase);
10878 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10879 }
10880 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010881 } else {
10882 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010883 }
10884 }
10885 } else {
10886 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010887 }
10888
Adam Lesinski5f056f62016-07-14 16:56:08 -070010889 if (in.readInt() != 0) {
10890 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10891 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
10892 } else {
10893 u.mMobileRadioApWakeupCount = null;
10894 }
10895
10896 if (in.readInt() != 0) {
10897 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10898 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
10899 } else {
10900 u.mWifiRadioApWakeupCount = null;
10901 }
10902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010903 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070010904 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010905 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 for (int iw = 0; iw < NW; iw++) {
10908 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010909 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010910 }
10911
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010912 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070010913 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010914 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010915 }
10916 for (int is = 0; is < NS; is++) {
10917 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010918 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010919 }
10920
10921 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070010922 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010923 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010924 }
10925 for (int ij = 0; ij < NJ; ij++) {
10926 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010927 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010928 }
10929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010930 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010931 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010932 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934 for (int is = 0; is < NP; is++) {
10935 int seNumber = in.readInt();
10936 if (in.readInt() != 0) {
10937 u.getSensorTimerLocked(seNumber, true)
10938 .readSummaryFromParcelLocked(in);
10939 }
10940 }
10941
10942 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010943 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010944 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010946 for (int ip = 0; ip < NP; ip++) {
10947 String procName = in.readString();
10948 Uid.Proc p = u.getProcessStatsLocked(procName);
10949 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010950 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010951 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010953 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10954 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010955 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956 }
10957
10958 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010959 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010960 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 for (int ip = 0; ip < NP; ip++) {
10963 String pkgName = in.readString();
10964 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010965 final int NWA = in.readInt();
10966 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010967 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010968 }
10969 p.mWakeupAlarms.clear();
10970 for (int iwa=0; iwa<NWA; iwa++) {
10971 String tag = in.readString();
10972 Counter c = new Counter(mOnBatteryTimeBase);
10973 c.readSummaryFromParcelLocked(in);
10974 p.mWakeupAlarms.put(tag, c);
10975 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010976 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010977 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010978 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010980 for (int is = 0; is < NS; is++) {
10981 String servName = in.readString();
10982 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10983 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010984 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010986 }
10987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010988 }
10989 }
10990
10991 /**
10992 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10993 * disk. This format does not allow a lossless round-trip.
10994 *
10995 * @param out the Parcel to be written to.
10996 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010997 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010998 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010999
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011000 // Pull the clock time. This may update the time and make a new history entry
11001 // if we had originally pulled a time before the RTC was set.
11002 long startClockTime = getStartClockTime();
11003
Joe Onoratoabded112016-02-08 16:49:39 -080011004 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
11005 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006
11007 out.writeInt(VERSION);
11008
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011009 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011012 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011013 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011014 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011015 out.writeString(mStartPlatformVersion);
11016 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011017 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11018 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011019 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011020 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011021 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011022 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011023 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011024 out.writeInt(getLowDischargeAmountSinceCharge());
11025 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011026 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11027 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011028 mDischargeStepTracker.writeToParcel(out);
11029 mChargeStepTracker.writeToParcel(out);
11030 mDailyDischargeStepTracker.writeToParcel(out);
11031 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011032 mDischargeCounter.writeSummaryFromParcelLocked(out);
11033 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011034 if (mDailyPackageChanges != null) {
11035 final int NPKG = mDailyPackageChanges.size();
11036 out.writeInt(NPKG);
11037 for (int i=0; i<NPKG; i++) {
11038 PackageChange pc = mDailyPackageChanges.get(i);
11039 out.writeString(pc.mPackageName);
11040 out.writeInt(pc.mUpdate ? 1 : 0);
11041 out.writeInt(pc.mVersionCode);
11042 }
11043 } else {
11044 out.writeInt(0);
11045 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011046 out.writeLong(mDailyStartTime);
11047 out.writeLong(mNextMinDailyDeadline);
11048 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011049
11050 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011051 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011052 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011053 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011054 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011055 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011056 out.writeLong(mLongestLightIdleTime);
11057 out.writeLong(mLongestFullIdleTime);
11058 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11059 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11060 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011061 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011062 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011063 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011064 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011065 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011066 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011067 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011068 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011069 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011070 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011071 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11072 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011073 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011074 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11075 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011076 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011077 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11078 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011079 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11080 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011081 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011082 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011083 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011084 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11085 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11086 }
11087 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11088 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11089 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011090 mWifiActivity.writeSummaryToParcel(out);
11091 mBluetoothActivity.writeSummaryToParcel(out);
11092 mModemActivity.writeSummaryToParcel(out);
11093 out.writeInt(mHasWifiReporting ? 1 : 0);
11094 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11095 out.writeInt(mHasModemReporting ? 1 : 0);
11096
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011097 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011098 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011099 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011100 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011101
Evan Millarc64edde2009-04-18 12:26:32 -070011102 out.writeInt(mKernelWakelockStats.size());
11103 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11104 Timer kwlt = ent.getValue();
11105 if (kwlt != null) {
11106 out.writeInt(1);
11107 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011108 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11109 } else {
11110 out.writeInt(0);
11111 }
11112 }
11113
11114 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011115 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11116 SamplingTimer timer = ent.getValue();
11117 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011118 out.writeInt(1);
11119 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011120 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070011121 } else {
11122 out.writeInt(0);
11123 }
11124 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011125
James Carr3a226052016-07-01 14:49:52 -070011126 out.writeInt(mKernelMemoryStats.size());
11127 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11128 Timer kmt = mKernelMemoryStats.valueAt(i);
11129 if (kmt != null) {
11130 out.writeInt(1);
11131 out.writeLong(mKernelMemoryStats.keyAt(i));
11132 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11133 } else {
11134 out.writeInt(0);
11135 }
11136 }
11137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 final int NU = mUidStats.size();
11139 out.writeInt(NU);
11140 for (int iu = 0; iu < NU; iu++) {
11141 out.writeInt(mUidStats.keyAt(iu));
11142 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011143
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011144 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011145 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011146 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011147 } else {
11148 out.writeInt(0);
11149 }
11150 if (u.mFullWifiLockTimer != null) {
11151 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011152 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011153 } else {
11154 out.writeInt(0);
11155 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011156 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011157 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011158 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011159 } else {
11160 out.writeInt(0);
11161 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011162 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11163 if (u.mWifiBatchedScanTimer[i] != null) {
11164 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011165 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070011166 } else {
11167 out.writeInt(0);
11168 }
11169 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011170 if (u.mWifiMulticastTimer != null) {
11171 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011172 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011173 } else {
11174 out.writeInt(0);
11175 }
11176 if (u.mAudioTurnedOnTimer != null) {
11177 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011178 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011179 } else {
11180 out.writeInt(0);
11181 }
11182 if (u.mVideoTurnedOnTimer != null) {
11183 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011184 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011185 } else {
11186 out.writeInt(0);
11187 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011188 if (u.mFlashlightTurnedOnTimer != null) {
11189 out.writeInt(1);
11190 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11191 } else {
11192 out.writeInt(0);
11193 }
11194 if (u.mCameraTurnedOnTimer != null) {
11195 out.writeInt(1);
11196 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11197 } else {
11198 out.writeInt(0);
11199 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011200 if (u.mForegroundActivityTimer != null) {
11201 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011202 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011203 } else {
11204 out.writeInt(0);
11205 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011206 if (u.mBluetoothScanTimer != null) {
11207 out.writeInt(1);
11208 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11209 } else {
11210 out.writeInt(0);
11211 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070011212 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11213 if (u.mProcessStateTimer[i] != null) {
11214 out.writeInt(1);
11215 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11216 } else {
11217 out.writeInt(0);
11218 }
11219 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011220 if (u.mVibratorOnTimer != null) {
11221 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011222 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011223 } else {
11224 out.writeInt(0);
11225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011226
Dianne Hackborn617f8772009-03-31 15:04:46 -070011227 if (u.mUserActivityCounters == null) {
11228 out.writeInt(0);
11229 } else {
11230 out.writeInt(1);
11231 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11232 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
11233 }
11234 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011235
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011236 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011237 out.writeInt(0);
11238 } else {
11239 out.writeInt(1);
11240 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011241 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11242 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011243 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011244 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
11245 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011246 }
11247
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011248 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
11249 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070011250 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011251
Adam Lesinski6832f392015-09-05 18:05:40 -070011252 if (u.mCpuClusterSpeed != null) {
11253 out.writeInt(1);
11254 out.writeInt(u.mCpuClusterSpeed.length);
11255 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
11256 if (cpuSpeeds != null) {
11257 out.writeInt(1);
11258 out.writeInt(cpuSpeeds.length);
11259 for (LongSamplingCounter c : cpuSpeeds) {
11260 if (c != null) {
11261 out.writeInt(1);
11262 c.writeSummaryFromParcelLocked(out);
11263 } else {
11264 out.writeInt(0);
11265 }
11266 }
11267 } else {
11268 out.writeInt(0);
11269 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011270 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011271 } else {
11272 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011273 }
11274
Adam Lesinski5f056f62016-07-14 16:56:08 -070011275 if (u.mMobileRadioApWakeupCount != null) {
11276 out.writeInt(1);
11277 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11278 } else {
11279 out.writeInt(0);
11280 }
11281
11282 if (u.mWifiRadioApWakeupCount != null) {
11283 out.writeInt(1);
11284 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11285 } else {
11286 out.writeInt(0);
11287 }
11288
Dianne Hackbornd953c532014-08-16 18:17:38 -070011289 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
11290 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011291 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011292 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011293 out.writeString(wakeStats.keyAt(iw));
11294 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011295 if (wl.mTimerFull != null) {
11296 out.writeInt(1);
11297 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11298 } else {
11299 out.writeInt(0);
11300 }
11301 if (wl.mTimerPartial != null) {
11302 out.writeInt(1);
11303 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11304 } else {
11305 out.writeInt(0);
11306 }
11307 if (wl.mTimerWindow != null) {
11308 out.writeInt(1);
11309 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11310 } else {
11311 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011312 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011313 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070011314 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011315 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070011316 } else {
11317 out.writeInt(0);
11318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011319 }
11320
Dianne Hackbornd953c532014-08-16 18:17:38 -070011321 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
11322 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011323 out.writeInt(NS);
11324 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011325 out.writeString(syncStats.keyAt(is));
11326 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011327 }
11328
Dianne Hackbornd953c532014-08-16 18:17:38 -070011329 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
11330 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011331 out.writeInt(NJ);
11332 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011333 out.writeString(jobStats.keyAt(ij));
11334 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011335 }
11336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 int NSE = u.mSensorStats.size();
11338 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011339 for (int ise=0; ise<NSE; ise++) {
11340 out.writeInt(u.mSensorStats.keyAt(ise));
11341 Uid.Sensor se = u.mSensorStats.valueAt(ise);
11342 if (se.mTimer != null) {
11343 out.writeInt(1);
11344 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11345 } else {
11346 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011347 }
11348 }
11349
11350 int NP = u.mProcessStats.size();
11351 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011352 for (int ip=0; ip<NP; ip++) {
11353 out.writeString(u.mProcessStats.keyAt(ip));
11354 Uid.Proc ps = u.mProcessStats.valueAt(ip);
11355 out.writeLong(ps.mUserTime);
11356 out.writeLong(ps.mSystemTime);
11357 out.writeLong(ps.mForegroundTime);
11358 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011359 out.writeInt(ps.mNumCrashes);
11360 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011361 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011362 }
11363
11364 NP = u.mPackageStats.size();
11365 out.writeInt(NP);
11366 if (NP > 0) {
11367 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
11368 : u.mPackageStats.entrySet()) {
11369 out.writeString(ent.getKey());
11370 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011371 final int NWA = ps.mWakeupAlarms.size();
11372 out.writeInt(NWA);
11373 for (int iwa=0; iwa<NWA; iwa++) {
11374 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
11375 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
11376 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011377 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011378 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011379 for (int is=0; is<NS; is++) {
11380 out.writeString(ps.mServiceStats.keyAt(is));
11381 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
11382 long time = ss.getStartTimeToNowLocked(
11383 mOnBatteryTimeBase.getUptime(NOW_SYS));
11384 out.writeLong(time);
11385 out.writeInt(ss.mStarts);
11386 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 }
11388 }
11389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011390 }
11391 }
11392
11393 public void readFromParcel(Parcel in) {
11394 readFromParcelLocked(in);
11395 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397 void readFromParcelLocked(Parcel in) {
11398 int magic = in.readInt();
11399 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011400 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011401 }
11402
Dianne Hackbornae384452011-06-28 12:33:48 -070011403 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011406 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011407 mStartPlatformVersion = in.readString();
11408 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011409 mUptime = in.readLong();
11410 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011411 mRealtime = in.readLong();
11412 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011413 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011414 mEstimatedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011415 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011416 mOnBatteryTimeBase.readFromParcel(in);
11417 mOnBatteryScreenOffTimeBase.readFromParcel(in);
11418
Jeff Browne95c3cd2014-05-02 16:59:26 -070011419 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080011420 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011421 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011422 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
11423 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011424 }
Dianne Hackborn29325132014-05-21 15:01:03 -070011425 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011426 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011427 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011428 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
11429 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011430 mLongestLightIdleTime = in.readLong();
11431 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080011432 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
11433 mOnBatteryTimeBase, in);
11434 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
11435 mOnBatteryTimeBase, in);
11436 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
11437 mOnBatteryTimeBase, in);
11438 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
11439 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011440 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011441 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011442 null, mOnBatteryTimeBase, in);
11443 }
Joe Onoratoabded112016-02-08 16:49:39 -080011444 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
11445 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011446 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011447 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011448 null, mOnBatteryTimeBase, in);
11449 }
11450 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11451 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11452 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11453 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011454 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080011455 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
11456 mOnBatteryTimeBase, in);
11457 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
11458 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011459 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011460 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
11461 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011462 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011463 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011464 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011465 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011466 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
11467 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011468 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011469 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011470 null, mOnBatteryTimeBase, in);
11471 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011472 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011473 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011474 null, mOnBatteryTimeBase, in);
11475 }
11476 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011477 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011478 null, mOnBatteryTimeBase, in);
11479 }
Adam Lesinski33dac552015-03-09 15:24:48 -070011480
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011481 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11482 NUM_WIFI_TX_LEVELS, in);
11483 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11484 NUM_BT_TX_LEVELS, in);
11485 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11486 ModemActivityInfo.TX_POWER_LEVELS, in);
11487 mHasWifiReporting = in.readInt() != 0;
11488 mHasBluetoothReporting = in.readInt() != 0;
11489 mHasModemReporting = in.readInt() != 0;
11490
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011491 mNumConnectivityChange = in.readInt();
11492 mLoadedNumConnectivityChange = in.readInt();
11493 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011494 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011495 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011496 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011497 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011498 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011499 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011500 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011501 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011502 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011503 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011504 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011505 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011506 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011507 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011508 mLowDischargeAmountSinceCharge = in.readInt();
11509 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011510 mDischargeAmountScreenOn = in.readInt();
11511 mDischargeAmountScreenOnSinceCharge = in.readInt();
11512 mDischargeAmountScreenOff = in.readInt();
11513 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011514 mDischargeStepTracker.readFromParcel(in);
11515 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011516 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
11517 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011518 mLastWriteTime = in.readLong();
11519
Evan Millarc64edde2009-04-18 12:26:32 -070011520 mKernelWakelockStats.clear();
11521 int NKW = in.readInt();
11522 for (int ikw = 0; ikw < NKW; ikw++) {
11523 if (in.readInt() != 0) {
11524 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011525 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070011526 mKernelWakelockStats.put(wakelockName, kwlt);
11527 }
11528 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011529
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011530 mWakeupReasonStats.clear();
11531 int NWR = in.readInt();
11532 for (int iwr = 0; iwr < NWR; iwr++) {
11533 if (in.readInt() != 0) {
11534 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011535 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011536 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011537 }
11538 }
11539
James Carr3a226052016-07-01 14:49:52 -070011540 mKernelMemoryStats.clear();
11541 int nmt = in.readInt();
11542 for (int imt = 0; imt < nmt; imt++) {
11543 if (in.readInt() != 0) {
11544 Long bucket = in.readLong();
11545 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
11546 mKernelMemoryStats.put(bucket, kmt);
11547 }
11548 }
11549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011550 mPartialTimers.clear();
11551 mFullTimers.clear();
11552 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011553 mWifiRunningTimers.clear();
11554 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070011555 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070011556 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011557 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011558 mAudioTurnedOnTimers.clear();
11559 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011560 mFlashlightTurnedOnTimers.clear();
11561 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011562
11563 int numUids = in.readInt();
11564 mUidStats.clear();
11565 for (int i = 0; i < numUids; i++) {
11566 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011567 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011568 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011569 mUidStats.append(uid, u);
11570 }
11571 }
11572
11573 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011574 writeToParcelLocked(out, true, flags);
11575 }
11576
11577 public void writeToParcelWithoutUids(Parcel out, int flags) {
11578 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011579 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011580
11581 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011582 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011583 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011584 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011585
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011586 // Pull the clock time. This may update the time and make a new history entry
11587 // if we had originally pulled a time before the RTC was set.
11588 long startClockTime = getStartClockTime();
11589
Joe Onoratoabded112016-02-08 16:49:39 -080011590 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11591 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011592 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11593 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011595 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011596
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011597 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011599 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011600 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011601 out.writeString(mStartPlatformVersion);
11602 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011603 out.writeLong(mUptime);
11604 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011605 out.writeLong(mRealtime);
11606 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011607 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011608 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011609 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11610 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11611
11612 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11613 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11614 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11615 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011616 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011617 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011618 out.writeLong(mLongestLightIdleTime);
11619 out.writeLong(mLongestFullIdleTime);
11620 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11621 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11622 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011623 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011624 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11625 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11626 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11627 }
11628 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11629 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11630 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11631 }
11632 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11633 mNetworkByteActivityCounters[i].writeToParcel(out);
11634 mNetworkPacketActivityCounters[i].writeToParcel(out);
11635 }
11636 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11637 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011638 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011639 mMobileRadioActiveUnknownTime.writeToParcel(out);
11640 mMobileRadioActiveUnknownCount.writeToParcel(out);
11641 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11642 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11643 for (int i=0; i<NUM_WIFI_STATES; i++) {
11644 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11645 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011646 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11647 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11648 }
11649 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11650 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11651 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011652 mWifiActivity.writeToParcel(out, 0);
11653 mBluetoothActivity.writeToParcel(out, 0);
11654 mModemActivity.writeToParcel(out, 0);
11655 out.writeInt(mHasWifiReporting ? 1 : 0);
11656 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11657 out.writeInt(mHasModemReporting ? 1 : 0);
11658
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011659 out.writeInt(mNumConnectivityChange);
11660 out.writeInt(mLoadedNumConnectivityChange);
11661 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011662 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011663 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011664 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011665 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011666 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011667 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011668 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011669 out.writeInt(mLowDischargeAmountSinceCharge);
11670 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011671 out.writeInt(mDischargeAmountScreenOn);
11672 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11673 out.writeInt(mDischargeAmountScreenOff);
11674 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011675 mDischargeStepTracker.writeToParcel(out);
11676 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011677 mDischargeCounter.writeToParcel(out);
11678 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011679 out.writeLong(mLastWriteTime);
11680
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011681 if (inclUids) {
11682 out.writeInt(mKernelWakelockStats.size());
11683 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11684 SamplingTimer kwlt = ent.getValue();
11685 if (kwlt != null) {
11686 out.writeInt(1);
11687 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011688 kwlt.writeToParcel(out, uSecRealtime);
11689 } else {
11690 out.writeInt(0);
11691 }
11692 }
11693 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011694 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11695 SamplingTimer timer = ent.getValue();
11696 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011697 out.writeInt(1);
11698 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011699 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011700 } else {
11701 out.writeInt(0);
11702 }
Evan Millarc64edde2009-04-18 12:26:32 -070011703 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011704 } else {
11705 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011706 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011707
James Carr3a226052016-07-01 14:49:52 -070011708 out.writeInt(mKernelMemoryStats.size());
11709 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11710 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
11711 if (kmt != null) {
11712 out.writeInt(1);
11713 out.writeLong(mKernelMemoryStats.keyAt(i));
11714 kmt.writeToParcel(out, uSecRealtime);
11715 } else {
11716 out.writeInt(0);
11717 }
11718 }
11719
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011720 if (inclUids) {
11721 int size = mUidStats.size();
11722 out.writeInt(size);
11723 for (int i = 0; i < size; i++) {
11724 out.writeInt(mUidStats.keyAt(i));
11725 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011726
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011727 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011728 }
11729 } else {
11730 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011731 }
11732 }
11733
11734 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11735 new Parcelable.Creator<BatteryStatsImpl>() {
11736 public BatteryStatsImpl createFromParcel(Parcel in) {
11737 return new BatteryStatsImpl(in);
11738 }
11739
11740 public BatteryStatsImpl[] newArray(int size) {
11741 return new BatteryStatsImpl[size];
11742 }
11743 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011744
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011745 public void prepareForDumpLocked() {
11746 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011747 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011748
11749 // Pull the clock time. This may update the time and make a new history entry
11750 // if we had originally pulled a time before the RTC was set.
11751 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011752 }
11753
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011754 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011756 pw.println("mOnBatteryTimeBase:");
11757 mOnBatteryTimeBase.dump(pw, " ");
11758 pw.println("mOnBatteryScreenOffTimeBase:");
11759 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011760 Printer pr = new PrintWriterPrinter(pw);
11761 pr.println("*** Screen timer:");
11762 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011763 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011764 pr.println("*** Screen brightness #" + i + ":");
11765 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011766 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011767 pr.println("*** Interactive timer:");
11768 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011769 pr.println("*** Power save mode timer:");
11770 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011771 pr.println("*** Device idle mode light timer:");
11772 mDeviceIdleModeLightTimer.logState(pr, " ");
11773 pr.println("*** Device idle mode full timer:");
11774 mDeviceIdleModeFullTimer.logState(pr, " ");
11775 pr.println("*** Device light idling timer:");
11776 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011777 pr.println("*** Device idling timer:");
11778 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011779 pr.println("*** Phone timer:");
11780 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011781 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011782 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011783 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011784 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011785 pr.println("*** Signal scanning :");
11786 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011787 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011788 pr.println("*** Data connection type #" + i + ":");
11789 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011790 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011791 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011792 pr.println("*** Mobile network active timer:");
11793 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011794 pr.println("*** Mobile network active adjusted timer:");
11795 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011796 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011797 pr.println("*** Wifi timer:");
11798 mWifiOnTimer.logState(pr, " ");
11799 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011800 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011801 for (int i=0; i<NUM_WIFI_STATES; i++) {
11802 pr.println("*** Wifi state #" + i + ":");
11803 mWifiStateTimer[i].logState(pr, " ");
11804 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011805 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11806 pr.println("*** Wifi suppl state #" + i + ":");
11807 mWifiSupplStateTimer[i].logState(pr, " ");
11808 }
11809 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11810 pr.println("*** Wifi signal strength #" + i + ":");
11811 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11812 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011813 pr.println("*** Flashlight timer:");
11814 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011815 pr.println("*** Camera timer:");
11816 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011817 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011818 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011820}