blob: a682f956cc0a458437eb86eed7c9adacf39a129c [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;
Bookatz867c0d72017-03-07 18:23:42 -0800482 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
483 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800484
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700485 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700486 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800487 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800488 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700489 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800490 LongSamplingCounter mMobileRadioActiveUnknownTime;
491 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800492
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700493 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 /**
496 * These provide time bases that discount the time the device is plugged
497 * in to power.
498 */
499 boolean mOnBattery;
500 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700501
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700502 /**
503 * External reporting of whether the device is actually charging.
504 */
505 boolean mCharging = true;
506 int mLastChargingStateLevel;
507
The Android Open Source Project10592532009-03-18 17:39:46 -0700508 /*
509 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
510 */
Evan Millar633a1742009-04-02 16:36:33 -0700511 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700512 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700513 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700514 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700515 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700516 int mLowDischargeAmountSinceCharge;
517 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800518 int mDischargeScreenOnUnplugLevel;
519 int mDischargeScreenOffUnplugLevel;
520 int mDischargeAmountScreenOn;
521 int mDischargeAmountScreenOnSinceCharge;
522 int mDischargeAmountScreenOff;
523 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700524
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700525 private LongSamplingCounter mDischargeScreenOffCounter;
526 private LongSamplingCounter mDischargeCounter;
527
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700528 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700529
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700530 int mInitStepMode = 0;
531 int mCurStepMode = 0;
532 int mModStepMode = 0;
533
Dianne Hackborn260c5022014-04-29 11:23:16 -0700534 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700535 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800536 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
537 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700538 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700539
540 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700541 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800542 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
543 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
544
545 static final int MAX_DAILY_ITEMS = 10;
546
547 long mDailyStartTime = 0;
548 long mNextMinDailyDeadline = 0;
549 long mNextMaxDailyDeadline = 0;
550
551 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700554
Amith Yamasanif37447b2009-10-08 18:28:01 -0700555 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800556 private int mPhoneServiceStateRaw = -1;
557 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700558
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800559 private int mNumConnectivityChange;
560 private int mLoadedNumConnectivityChange;
561 private int mUnpluggedNumConnectivityChange;
562
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700563 private int mEstimatedBatteryCapacity = -1;
564
Adam Lesinski041d9172016-12-12 12:03:56 -0800565 // Last learned capacity reported by BatteryService in
566 // setBatteryState().
567 private int mLastChargeFullUAh = 0;
568
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700569 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
570
Adam Lesinskie08af192015-03-25 16:42:59 -0700571 private PowerProfile mPowerProfile;
572
Evan Millarc64edde2009-04-18 12:26:32 -0700573 /*
574 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
575 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700576 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700577
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700578 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700579 return mKernelWakelockStats;
580 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700581
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700582 String mLastWakeupReason = null;
583 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700584 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700585
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700586 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700587 return mWakeupReasonStats;
588 }
589
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700590 @Override
591 public LongCounter getDischargeScreenOffCoulombCounter() {
592 return mDischargeScreenOffCounter;
593 }
594
595 @Override
596 public LongCounter getDischargeCoulombCounter() {
597 return mDischargeCounter;
598 }
599
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700600 @Override
601 public int getEstimatedBatteryCapacity() {
602 return mEstimatedBatteryCapacity;
603 }
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800606 this(new SystemClocks());
607 }
608
609 public BatteryStatsImpl(Clocks clocks) {
610 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700611 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700612 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800613 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700614 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700615 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700616 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700617 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
619
Joe Onoratoabded112016-02-08 16:49:39 -0800620 private void init(Clocks clocks) {
621 mClocks = clocks;
622 mMobileNetworkStats = new NetworkStats[] {
623 new NetworkStats(mClocks.elapsedRealtime(), 50),
624 new NetworkStats(mClocks.elapsedRealtime(), 50),
625 new NetworkStats(mClocks.elapsedRealtime(), 50)
626 };
627 mWifiNetworkStats = new NetworkStats[] {
628 new NetworkStats(mClocks.elapsedRealtime(), 50),
629 new NetworkStats(mClocks.elapsedRealtime(), 50),
630 new NetworkStats(mClocks.elapsedRealtime(), 50)
631 };
632 }
633
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800634 public static interface TimeBaseObs {
635 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
636 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
637 }
638
Joe Onoratoabded112016-02-08 16:49:39 -0800639 // methods are protected not private to be VisibleForTesting
640 public static class TimeBase {
641 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800642
Joe Onoratoabded112016-02-08 16:49:39 -0800643 protected long mUptime;
644 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800645
Joe Onoratoabded112016-02-08 16:49:39 -0800646 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800647
Joe Onoratoabded112016-02-08 16:49:39 -0800648 protected long mPastUptime;
649 protected long mUptimeStart;
650 protected long mPastRealtime;
651 protected long mRealtimeStart;
652 protected long mUnpluggedUptime;
653 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800654
655 public void dump(PrintWriter pw, String prefix) {
656 StringBuilder sb = new StringBuilder(128);
657 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
658 sb.setLength(0);
659 sb.append(prefix);
660 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700661 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800662 pw.println(sb.toString());
663 sb.setLength(0);
664 sb.append(prefix);
665 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700666 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800667 pw.println(sb.toString());
668 sb.setLength(0);
669 sb.append(prefix);
670 sb.append("mPastUptime=");
671 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
672 formatTimeMs(sb, mUptimeStart / 1000);
673 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
674 pw.println(sb.toString());
675 sb.setLength(0);
676 sb.append(prefix);
677 sb.append("mPastRealtime=");
678 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
679 formatTimeMs(sb, mRealtimeStart / 1000);
680 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
681 pw.println(sb.toString());
682 }
683
684 public void add(TimeBaseObs observer) {
685 mObservers.add(observer);
686 }
687
688 public void remove(TimeBaseObs observer) {
689 if (!mObservers.remove(observer)) {
690 Slog.wtf(TAG, "Removed unknown observer: " + observer);
691 }
692 }
693
Joe Onoratoabded112016-02-08 16:49:39 -0800694 public boolean hasObserver(TimeBaseObs observer) {
695 return mObservers.contains(observer);
696 }
697
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800698 public void init(long uptime, long realtime) {
699 mRealtime = 0;
700 mUptime = 0;
701 mPastUptime = 0;
702 mPastRealtime = 0;
703 mUptimeStart = uptime;
704 mRealtimeStart = realtime;
705 mUnpluggedUptime = getUptime(mUptimeStart);
706 mUnpluggedRealtime = getRealtime(mRealtimeStart);
707 }
708
709 public void reset(long uptime, long realtime) {
710 if (!mRunning) {
711 mPastUptime = 0;
712 mPastRealtime = 0;
713 } else {
714 mUptimeStart = uptime;
715 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800716 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
717 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800718 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800719 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800720 mUnpluggedRealtime = getRealtime(realtime);
721 }
722 }
723
724 public long computeUptime(long curTime, int which) {
725 switch (which) {
726 case STATS_SINCE_CHARGED:
727 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800728 case STATS_CURRENT:
729 return getUptime(curTime);
730 case STATS_SINCE_UNPLUGGED:
731 return getUptime(curTime) - mUnpluggedUptime;
732 }
733 return 0;
734 }
735
736 public long computeRealtime(long curTime, int which) {
737 switch (which) {
738 case STATS_SINCE_CHARGED:
739 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800740 case STATS_CURRENT:
741 return getRealtime(curTime);
742 case STATS_SINCE_UNPLUGGED:
743 return getRealtime(curTime) - mUnpluggedRealtime;
744 }
745 return 0;
746 }
747
748 public long getUptime(long curTime) {
749 long time = mPastUptime;
750 if (mRunning) {
751 time += curTime - mUptimeStart;
752 }
753 return time;
754 }
755
756 public long getRealtime(long curTime) {
757 long time = mPastRealtime;
758 if (mRunning) {
759 time += curTime - mRealtimeStart;
760 }
761 return time;
762 }
763
764 public long getUptimeStart() {
765 return mUptimeStart;
766 }
767
768 public long getRealtimeStart() {
769 return mRealtimeStart;
770 }
771
772 public boolean isRunning() {
773 return mRunning;
774 }
775
776 public boolean setRunning(boolean running, long uptime, long realtime) {
777 if (mRunning != running) {
778 mRunning = running;
779 if (running) {
780 mUptimeStart = uptime;
781 mRealtimeStart = realtime;
782 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
783 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
784
785 for (int i = mObservers.size() - 1; i >= 0; i--) {
786 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
787 }
788 } else {
789 mPastUptime += uptime - mUptimeStart;
790 mPastRealtime += realtime - mRealtimeStart;
791
792 long batteryUptime = getUptime(uptime);
793 long batteryRealtime = getRealtime(realtime);
794
795 for (int i = mObservers.size() - 1; i >= 0; i--) {
796 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
797 }
798 }
799 return true;
800 }
801 return false;
802 }
803
804 public void readSummaryFromParcel(Parcel in) {
805 mUptime = in.readLong();
806 mRealtime = in.readLong();
807 }
808
809 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
810 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
811 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
812 }
813
814 public void readFromParcel(Parcel in) {
815 mRunning = false;
816 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800817 mPastUptime = in.readLong();
818 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700819 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800820 mPastRealtime = in.readLong();
821 mRealtimeStart = in.readLong();
822 mUnpluggedUptime = in.readLong();
823 mUnpluggedRealtime = in.readLong();
824 }
825
826 public void writeToParcel(Parcel out, long uptime, long realtime) {
827 final long runningUptime = getUptime(uptime);
828 final long runningRealtime = getRealtime(realtime);
829 out.writeLong(mUptime);
830 out.writeLong(runningUptime);
831 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700832 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800833 out.writeLong(runningRealtime);
834 out.writeLong(mRealtimeStart);
835 out.writeLong(mUnpluggedUptime);
836 out.writeLong(mUnpluggedRealtime);
837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700841 * State for keeping track of counting information.
842 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800843 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700844 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800845 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700846 int mLoadedCount;
847 int mLastCount;
848 int mUnpluggedCount;
849 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700850
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800851 Counter(TimeBase timeBase, Parcel in) {
852 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700853 mPluggedCount = in.readInt();
854 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700855 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700856 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700857 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800858 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700859 }
860
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800861 Counter(TimeBase timeBase) {
862 mTimeBase = timeBase;
863 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700864 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700865
Dianne Hackborn617f8772009-03-31 15:04:46 -0700866 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700867 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700868 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700869 out.writeInt(mUnpluggedCount);
870 }
871
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800872 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700873 mUnpluggedCount = mPluggedCount;
874 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700875 }
876
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800877 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700878 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700879 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700880
Dianne Hackborn617f8772009-03-31 15:04:46 -0700881 /**
882 * Writes a possibly null Counter to a Parcel.
883 *
884 * @param out the Parcel to be written to.
885 * @param counter a Counter, or null.
886 */
887 public static void writeCounterToParcel(Parcel out, Counter counter) {
888 if (counter == null) {
889 out.writeInt(0); // indicates null
890 return;
891 }
892 out.writeInt(1); // indicates non-null
893
894 counter.writeToParcel(out);
895 }
896
897 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700898 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700899 int val = mCount.get();
900 if (which == STATS_SINCE_UNPLUGGED) {
901 val -= mUnpluggedCount;
902 } else if (which != STATS_SINCE_CHARGED) {
903 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700904 }
905
906 return val;
907 }
908
909 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700910 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700911 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
912 + " mUnpluggedCount=" + mUnpluggedCount
913 + " mPluggedCount=" + mPluggedCount);
914 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700915
Christopher Tate4cee7252010-03-19 14:50:40 -0700916 void stepAtomic() {
917 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700918 }
919
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700920 /**
921 * Clear state of this counter.
922 */
923 void reset(boolean detachIfReset) {
924 mCount.set(0);
925 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
926 if (detachIfReset) {
927 detach();
928 }
929 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700930
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700931 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800932 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700933 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700934
Dianne Hackborn617f8772009-03-31 15:04:46 -0700935 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700936 int count = mCount.get();
937 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700938 }
939
940 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700941 mLoadedCount = in.readInt();
942 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700943 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700944 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700945 }
946 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700947
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700948 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800949 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700950 long mCount;
951 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700952 long mUnpluggedCount;
953 long mPluggedCount;
954
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800955 LongSamplingCounter(TimeBase timeBase, Parcel in) {
956 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700957 mPluggedCount = in.readLong();
958 mCount = mPluggedCount;
959 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700960 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800961 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700962 }
963
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800964 LongSamplingCounter(TimeBase timeBase) {
965 mTimeBase = timeBase;
966 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700967 }
968
969 public void writeToParcel(Parcel out) {
970 out.writeLong(mCount);
971 out.writeLong(mLoadedCount);
972 out.writeLong(mUnpluggedCount);
973 }
974
975 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800976 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700977 mUnpluggedCount = mPluggedCount;
978 mCount = mPluggedCount;
979 }
980
981 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800982 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700983 mPluggedCount = mCount;
984 }
985
986 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700987 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -0700988 if (which == STATS_SINCE_UNPLUGGED) {
989 val -= mUnpluggedCount;
990 } else if (which != STATS_SINCE_CHARGED) {
991 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700992 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700993 return val;
994 }
995
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700996 @Override
997 public void logState(Printer pw, String prefix) {
998 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700999 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001000 + " mUnpluggedCount=" + mUnpluggedCount
1001 + " mPluggedCount=" + mPluggedCount);
1002 }
1003
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001004 void addCountLocked(long count) {
1005 mCount += count;
1006 }
1007
1008 /**
1009 * Clear state of this counter.
1010 */
1011 void reset(boolean detachIfReset) {
1012 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001013 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001014 if (detachIfReset) {
1015 detach();
1016 }
1017 }
1018
1019 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001020 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001021 }
1022
1023 void writeSummaryFromParcelLocked(Parcel out) {
1024 out.writeLong(mCount);
1025 }
1026
1027 void readSummaryFromParcelLocked(Parcel in) {
1028 mLoadedCount = in.readLong();
1029 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001030 mUnpluggedCount = mPluggedCount = mLoadedCount;
1031 }
1032 }
1033
Dianne Hackborn617f8772009-03-31 15:04:46 -07001034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 * State for keeping track of timing information.
1036 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001037 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001038 protected final Clocks mClocks;
1039 protected final int mType;
1040 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001041
Joe Onoratoabded112016-02-08 16:49:39 -08001042 protected int mCount;
1043 protected int mLoadedCount;
1044 protected int mLastCount;
1045 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 // Times are in microseconds for better accuracy when dividing by the
1048 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 /**
1051 * The total time we have accumulated since the start of the original
1052 * boot, to the last time something interesting happened in the
1053 * current run.
1054 */
Joe Onoratoabded112016-02-08 16:49:39 -08001055 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 /**
1058 * The total time we loaded for the previous runs. Subtract this from
1059 * mTotalTime to find the time for the current run of the system.
1060 */
Joe Onoratoabded112016-02-08 16:49:39 -08001061 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 /**
1064 * The run time of the last run of the system, as loaded from the
1065 * saved data.
1066 */
Joe Onoratoabded112016-02-08 16:49:39 -08001067 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 /**
1070 * The value of mTotalTime when unplug() was last called. Subtract
1071 * this from mTotalTime to find the time since the last unplug from
1072 * power.
1073 */
Joe Onoratoabded112016-02-08 16:49:39 -08001074 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001075
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001076 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001077 * The total time this timer has been running until the latest mark has been set.
1078 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1079 */
Joe Onoratoabded112016-02-08 16:49:39 -08001080 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001081
1082 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001083 * Constructs from a parcel.
1084 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001085 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001086 * @param in
1087 */
Joe Onoratoabded112016-02-08 16:49:39 -08001088 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1089 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001091 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 mCount = in.readInt();
1094 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001095 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 mUnpluggedCount = in.readInt();
1097 mTotalTime = in.readLong();
1098 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001099 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001101 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001102 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001103 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105
Joe Onoratoabded112016-02-08 16:49:39 -08001106 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1107 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001109 mTimeBase = timeBase;
1110 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
Evan Millarc64edde2009-04-18 12:26:32 -07001112
1113 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001114
Evan Millarc64edde2009-04-18 12:26:32 -07001115 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001116
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001117 /**
1118 * Clear state of this timer. Returns true if the timer is inactive
1119 * so can be completely dropped.
1120 */
Joe Onoratoabded112016-02-08 16:49:39 -08001121 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001122 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001123 mCount = mLoadedCount = mLastCount = 0;
1124 if (detachIfReset) {
1125 detach();
1126 }
1127 return true;
1128 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001129
Joe Onoratoabded112016-02-08 16:49:39 -08001130 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001131 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001132 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001133
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001134 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001135 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1136 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001137 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001140 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001143 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145
Adam Lesinskie08af192015-03-25 16:42:59 -07001146 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001147 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001149 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 + " old mUnpluggedTime=" + mUnpluggedTime
1151 + " old mUnpluggedCount=" + mUnpluggedCount);
1152 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001153 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001154 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (DEBUG && mType < 0) {
1156 Log.v(TAG, "unplug #" + mType
1157 + ": new mUnpluggedTime=" + mUnpluggedTime
1158 + " new mUnpluggedCount=" + mUnpluggedCount);
1159 }
1160 }
1161
Adam Lesinskie08af192015-03-25 16:42:59 -07001162 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001163 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001164 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001165 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001166 + " old mTotalTime=" + mTotalTime);
1167 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001168 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001169 mCount = computeCurrentCountLocked();
1170 if (DEBUG && mType < 0) {
1171 Log.v(TAG, "plug #" + mType
1172 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Writes a possibly null Timer to a Parcel.
1178 *
1179 * @param out the Parcel to be written to.
1180 * @param timer a Timer, or null.
1181 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001182 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 if (timer == null) {
1184 out.writeInt(0); // indicates null
1185 return;
1186 }
1187 out.writeInt(1); // indicates non-null
1188
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001189 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191
1192 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001193 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001194 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1195 if (which == STATS_SINCE_UNPLUGGED) {
1196 val -= mUnpluggedTime;
1197 } else if (which != STATS_SINCE_CHARGED) {
1198 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200
1201 return val;
1202 }
1203
1204 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001205 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001206 int val = computeCurrentCountLocked();
1207 if (which == STATS_SINCE_UNPLUGGED) {
1208 val -= mUnpluggedCount;
1209 } else if (which != STATS_SINCE_CHARGED) {
1210 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212
1213 return val;
1214 }
1215
Adam Lesinskie08af192015-03-25 16:42:59 -07001216 @Override
1217 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1218 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1219 return val - mTimeBeforeMark;
1220 }
1221
1222 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001223 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001224 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1226 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001227 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001229 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001231 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001232
1233
Joe Onoratoabded112016-02-08 16:49:39 -08001234 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001235 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1236 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001237 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001238 }
1239
Joe Onoratoabded112016-02-08 16:49:39 -08001240 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001241 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001242 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001243 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001244 mUnpluggedTime = mTotalTime;
1245 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001246 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001247 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001248
1249 // When reading the summary, we set the mark to be the latest information.
1250 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001251 }
1252 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001253
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001254 /**
1255 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1256 * method. The state of the timer according to its {@link TimeBase} will determine how much
1257 * of the value is recorded.
1258 *
1259 * If the value being recorded resets, {@link #endSample()} can be called in order to
1260 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1261 * between calls, the {@link #endSample()} is automatically called and the new value is
1262 * expected to increase monotonically from that point on.
1263 */
Joe Onoratoabded112016-02-08 16:49:39 -08001264 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001265
Evan Millarc64edde2009-04-18 12:26:32 -07001266 /**
1267 * The most recent reported count from /proc/wakelocks.
1268 */
1269 int mCurrentReportedCount;
1270
1271 /**
1272 * The reported count from /proc/wakelocks when unplug() was last
1273 * called.
1274 */
1275 int mUnpluggedReportedCount;
1276
1277 /**
1278 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001279 */
Evan Millarc64edde2009-04-18 12:26:32 -07001280 long mCurrentReportedTotalTime;
1281
1282
1283 /**
1284 * The reported total_time from /proc/wakelocks when unplug() was last
1285 * called.
1286 */
1287 long mUnpluggedReportedTotalTime;
1288
1289 /**
1290 * Whether we are currently in a discharge cycle.
1291 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001292 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001293
1294 /**
1295 * Whether we are currently recording reported values.
1296 */
1297 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001298
Evan Millarc64edde2009-04-18 12:26:32 -07001299 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001300 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001301 */
1302 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001303
Adam Lesinski98f0d462016-04-19 16:46:20 -07001304 @VisibleForTesting
1305 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001306 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001307 mCurrentReportedCount = in.readInt();
1308 mUnpluggedReportedCount = in.readInt();
1309 mCurrentReportedTotalTime = in.readLong();
1310 mUnpluggedReportedTotalTime = in.readLong();
1311 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001312 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001313 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001314
Adam Lesinski98f0d462016-04-19 16:46:20 -07001315 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001316 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001317 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001318 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001319 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001320 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001321
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001322 /**
1323 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1324 * be less than the values used for a previous invocation.
1325 */
1326 public void endSample() {
1327 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1328 mCount = computeCurrentCountLocked();
1329 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1330 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001331 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001332
Evan Millarc64edde2009-04-18 12:26:32 -07001333 public void setUpdateVersion(int version) {
1334 mUpdateVersion = version;
1335 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001336
Evan Millarc64edde2009-04-18 12:26:32 -07001337 public int getUpdateVersion() {
1338 return mUpdateVersion;
1339 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001340
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001341 /**
1342 * Updates the current recorded values. These are meant to be monotonically increasing
1343 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1344 *
1345 * If the values being recorded have been reset, the monotonically increasing requirement
1346 * will be broken. In this case, {@link #endSample()} is automatically called and
1347 * the total value of totalTime and count are recorded, starting a new monotonically
1348 * increasing sample.
1349 *
1350 * @param totalTime total time of sample in microseconds.
1351 * @param count total number of times the event being sampled occurred.
1352 */
1353 public void update(long totalTime, int count) {
1354 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001355 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001356 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001357 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001358 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001359
1360 mTrackingReportedValues = true;
1361
1362 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1363 endSample();
1364 }
1365
1366 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001367 mCurrentReportedCount = count;
1368 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001369
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001370 /**
1371 * Adds deltaTime and deltaCount to the current sample.
1372 *
1373 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1374 * @param deltaCount additional number of times the event being sampled occurred.
1375 */
1376 public void add(long deltaTime, int deltaCount) {
1377 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001378 }
1379
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001380 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001381 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1382 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001383 if (mTrackingReportedValues) {
1384 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1385 mUnpluggedReportedCount = mCurrentReportedCount;
1386 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001387 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001388 }
1389
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001390 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001391 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1392 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1393 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001394 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001395
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001396 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001397 public void logState(Printer pw, String prefix) {
1398 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001399 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001400 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1401 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1402 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1403 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001404
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001405 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001406 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001407 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001408 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1409 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001410
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001411 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001412 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001413 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001414 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1415 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001416
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001417 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001418 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1419 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001420 out.writeInt(mCurrentReportedCount);
1421 out.writeInt(mUnpluggedReportedCount);
1422 out.writeLong(mCurrentReportedTotalTime);
1423 out.writeLong(mUnpluggedReportedTotalTime);
1424 out.writeInt(mTrackingReportedValues ? 1 : 0);
1425 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001426
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001427 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001428 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001429 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001430 mTrackingReportedValues = false;
1431 mUnpluggedReportedTotalTime = 0;
1432 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001433 return true;
1434 }
Evan Millarc64edde2009-04-18 12:26:32 -07001435 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001436
Evan Millarc64edde2009-04-18 12:26:32 -07001437 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001438 * A timer that increments in batches. It does not run for durations, but just jumps
1439 * for a pre-determined amount.
1440 */
Joe Onoratoabded112016-02-08 16:49:39 -08001441 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001442 final Uid mUid;
1443
1444 /**
1445 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1446 */
1447 long mLastAddedTime;
1448
1449 /**
1450 * The last duration that we added to the timer. This is in microseconds.
1451 */
1452 long mLastAddedDuration;
1453
1454 /**
1455 * Whether we are currently in a discharge cycle.
1456 */
1457 boolean mInDischarge;
1458
Joe Onoratoabded112016-02-08 16:49:39 -08001459 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1460 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001461 mUid = uid;
1462 mLastAddedTime = in.readLong();
1463 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001464 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001465 }
1466
Joe Onoratoabded112016-02-08 16:49:39 -08001467 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1468 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001469 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001470 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001471 }
1472
1473 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001474 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1475 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001476 out.writeLong(mLastAddedTime);
1477 out.writeLong(mLastAddedDuration);
1478 }
1479
1480 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001481 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001482 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001483 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001484 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001485 }
1486
1487 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001488 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001489 recomputeLastDuration(elapsedRealtime, false);
1490 mInDischarge = true;
1491 // If we are still within the last added duration, then re-added whatever remains.
1492 if (mLastAddedTime == elapsedRealtime) {
1493 mTotalTime += mLastAddedDuration;
1494 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001495 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001496 }
1497
1498 @Override
1499 public void logState(Printer pw, String prefix) {
1500 super.logState(pw, prefix);
1501 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1502 + " mLastAddedDuration=" + mLastAddedDuration);
1503 }
1504
1505 private long computeOverage(long curTime) {
1506 if (mLastAddedTime > 0) {
1507 return mLastTime + mLastAddedDuration - curTime;
1508 }
1509 return 0;
1510 }
1511
1512 private void recomputeLastDuration(long curTime, boolean abort) {
1513 final long overage = computeOverage(curTime);
1514 if (overage > 0) {
1515 // Aborting before the duration ran out -- roll back the remaining
1516 // duration. Only do this if currently discharging; otherwise we didn't
1517 // actually add the time.
1518 if (mInDischarge) {
1519 mTotalTime -= overage;
1520 }
1521 if (abort) {
1522 mLastAddedTime = 0;
1523 } else {
1524 mLastAddedTime = curTime;
1525 mLastAddedDuration -= overage;
1526 }
1527 }
1528 }
1529
1530 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001531 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001532 recomputeLastDuration(now, true);
1533 mLastAddedTime = now;
1534 mLastAddedDuration = durationMillis * 1000;
1535 if (mInDischarge) {
1536 mTotalTime += mLastAddedDuration;
1537 mCount++;
1538 }
1539 }
1540
1541 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001542 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001543 recomputeLastDuration(now, true);
1544 }
1545
1546 @Override
1547 protected int computeCurrentCountLocked() {
1548 return mCount;
1549 }
1550
1551 @Override
1552 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001553 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001554 if (overage > 0) {
1555 return mTotalTime = overage;
1556 }
1557 return mTotalTime;
1558 }
1559
1560 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001561 public boolean reset(boolean detachIfReset) {
1562 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001563 recomputeLastDuration(now, true);
1564 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001565 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001566 return !stillActive;
1567 }
1568 }
1569
Joe Onorato92fd23f2016-07-25 11:18:42 -07001570
1571 /**
1572 * A StopwatchTimer that also tracks the total and max individual
1573 * time spent active according to the given timebase. Whereas
1574 * StopwatchTimer apportions the time amongst all in the pool,
1575 * the total and max durations are not apportioned.
1576 */
1577 public static class DurationTimer extends StopwatchTimer {
1578 /**
1579 * The time (in ms) that the timer was last acquired or the time base
1580 * last (re-)started. Increasing the nesting depth does not reset this time.
1581 *
1582 * -1 if the timer is currently not running or the time base is not running.
1583 *
1584 * If written to a parcel, the start time is reset, as is mNesting in the base class
1585 * StopwatchTimer.
1586 */
1587 long mStartTimeMs = -1;
1588
1589 /**
Bookatz867c0d72017-03-07 18:23:42 -08001590 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001591 */
1592 long mMaxDurationMs;
1593
1594 /**
Bookatz867c0d72017-03-07 18:23:42 -08001595 * The time (in ms) that that the timer has been active since most recent
1596 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001597 */
1598 long mCurrentDurationMs;
1599
Bookatz867c0d72017-03-07 18:23:42 -08001600 /**
1601 * The total time (in ms) that that the timer has been active since most recent reset()
1602 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1603 * (but not including the present currentDuration) since reset. Not pooled.
1604 */
1605 long mTotalDurationMs;
1606
Joe Onorato92fd23f2016-07-25 11:18:42 -07001607 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1608 TimeBase timeBase, Parcel in) {
1609 super(clocks, uid, type, timerPool, timeBase, in);
1610 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001611 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001612 }
1613
1614 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1615 TimeBase timeBase) {
1616 super(clocks, uid, type, timerPool, timeBase);
1617 }
1618
1619 @Override
1620 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1621 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001622 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001623 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001624 }
1625
1626 /**
1627 * Write the summary to the parcel.
1628 *
1629 * Since the time base is probably meaningless after we come back, reading
1630 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001631 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001632 */
1633 @Override
1634 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1635 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001636 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001637 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001638 }
1639
1640 /**
1641 * Read the summary parcel.
1642 *
1643 * Has the side effect of stopping the timer.
1644 */
1645 @Override
1646 public void readSummaryFromParcelLocked(Parcel in) {
1647 super.readSummaryFromParcelLocked(in);
1648 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001649 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001650 mStartTimeMs = -1;
1651 mCurrentDurationMs = 0;
1652 }
1653
1654 /**
1655 * The TimeBase time started (again).
1656 *
1657 * If the timer is also running, store the start time.
1658 */
1659 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1660 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1661 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001662 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001663 }
1664 }
1665
1666 /**
1667 * The TimeBase stopped running.
1668 *
1669 * If the timer is running, add the duration into mCurrentDurationMs.
1670 */
1671 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001672 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1673 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001674 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001675 // baseRealtimeUs has already been converted to the timebase's realtime.
1676 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001677 }
1678 mStartTimeMs = -1;
1679 }
1680
1681 @Override
1682 public void logState(Printer pw, String prefix) {
1683 super.logState(pw, prefix);
1684 }
1685
1686 @Override
1687 public void startRunningLocked(long elapsedRealtimeMs) {
1688 super.startRunningLocked(elapsedRealtimeMs);
1689 if (mNesting == 1 && mTimeBase.isRunning()) {
1690 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001691 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001692 }
1693 }
1694
1695 /**
1696 * Decrements the mNesting ref-count on this timer.
1697 *
1698 * If it actually stopped (mNesting went to 0), then possibly update
1699 * mMaxDuration if the current duration was the longest ever.
1700 */
1701 @Override
1702 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001703 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001704 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001705 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001706 if (durationMs > mMaxDurationMs) {
1707 mMaxDurationMs = durationMs;
1708 }
1709 mStartTimeMs = -1;
1710 mCurrentDurationMs = 0;
1711 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001712 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1713 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1714 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001715 }
1716
1717 @Override
1718 public boolean reset(boolean detachIfReset) {
1719 boolean result = super.reset(detachIfReset);
1720 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08001721 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001722 mCurrentDurationMs = 0;
1723 if (mNesting > 0) {
1724 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
1725 } else {
1726 mStartTimeMs = -1;
1727 }
1728 return result;
1729 }
1730
1731 /**
1732 * Returns the max duration that this timer has ever seen.
1733 *
1734 * Note that this time is NOT split between the timers in the timer group that
1735 * this timer is attached to. It is the TOTAL time.
1736 */
1737 @Override
1738 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
1739 if (mNesting > 0) {
1740 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1741 if (durationMs > mMaxDurationMs) {
1742 return durationMs;
1743 }
1744 }
1745 return mMaxDurationMs;
1746 }
1747
1748 /**
1749 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08001750 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001751 *
1752 * Note that this time is NOT split between the timers in the timer group that
1753 * this timer is attached to. It is the TOTAL time.
1754 */
1755 @Override
1756 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
1757 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08001758 if (mNesting > 0 && mTimeBase.isRunning()) {
1759 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
1760 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001761 }
1762 return durationMs;
1763 }
Bookatz867c0d72017-03-07 18:23:42 -08001764
1765 /**
1766 * Returns the total cumulative duration that this timer has been on since reset().
1767 * If mTimerPool == null, this should be the same
1768 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
1769 *
1770 * Note that this time is NOT split between the timers in the timer group that
1771 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
1772 * the result will not be equivalent to getTotalTimeLocked.
1773 */
1774 @Override
1775 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
1776 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
1777 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07001778 }
1779
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001780 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001781 * State for keeping track of timing information.
1782 */
Joe Onoratoabded112016-02-08 16:49:39 -08001783 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001784 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001785 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001786
Evan Millarc64edde2009-04-18 12:26:32 -07001787 int mNesting;
1788
Evan Millarc64edde2009-04-18 12:26:32 -07001789 /**
1790 * The last time at which we updated the timer. If mNesting is > 0,
1791 * subtract this from the current battery time to find the amount of
1792 * time we have been running since we last computed an update.
1793 */
1794 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001795
Evan Millarc64edde2009-04-18 12:26:32 -07001796 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001797 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001798 * was actually held for an interesting duration.
1799 */
1800 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001801
Amith Yamasanif37447b2009-10-08 18:28:01 -07001802 long mTimeout;
1803
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001804 /**
1805 * For partial wake locks, keep track of whether we are in the list
1806 * to consume CPU cycles.
1807 */
1808 boolean mInList;
1809
Joe Onoratoabded112016-02-08 16:49:39 -08001810 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001811 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001812 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001813 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001814 mTimerPool = timerPool;
1815 mUpdateTime = in.readLong();
1816 }
1817
Joe Onoratoabded112016-02-08 16:49:39 -08001818 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001819 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001820 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001821 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001822 mTimerPool = timerPool;
1823 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001824
Joe Onoratoabded112016-02-08 16:49:39 -08001825 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001826 mTimeout = timeout;
1827 }
1828
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001829 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1830 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001831 out.writeLong(mUpdateTime);
1832 }
1833
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001834 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001835 if (mNesting > 0) {
1836 if (DEBUG && mType < 0) {
1837 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1838 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001839 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1840 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001841 if (DEBUG && mType < 0) {
1842 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1843 }
1844 }
1845 }
1846
1847 public void logState(Printer pw, String prefix) {
1848 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001849 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 + " mAcquireTime=" + mAcquireTime);
1851 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001852
Joe Onoratoabded112016-02-08 16:49:39 -08001853 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001855 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001856 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 if (mTimerPool != null) {
1858 // Accumulate time to all currently active timers before adding
1859 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001860 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 // Add this timer to the active pool
1862 mTimerPool.add(this);
1863 }
1864 // Increment the count
1865 mCount++;
1866 mAcquireTime = mTotalTime;
1867 if (DEBUG && mType < 0) {
1868 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1869 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1870 + " mAcquireTime=" + mAcquireTime);
1871 }
1872 }
1873 }
1874
Joe Onoratoabded112016-02-08 16:49:39 -08001875 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001876 return mNesting > 0;
1877 }
1878
Joe Onoratoabded112016-02-08 16:49:39 -08001879 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 // Ignore attempt to stop a timer that isn't running
1881 if (mNesting == 0) {
1882 return;
1883 }
1884 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001885 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 if (mTimerPool != null) {
1887 // Accumulate time to all active counters, scaled by the total
1888 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001889 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 // Remove this timer from the active pool
1891 mTimerPool.remove(this);
1892 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 mNesting = 1;
1894 mTotalTime = computeRunTimeLocked(batteryRealtime);
1895 mNesting = 0;
1896 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 if (DEBUG && mType < 0) {
1899 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1900 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1901 + " mAcquireTime=" + mAcquireTime);
1902 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 if (mTotalTime == mAcquireTime) {
1905 // If there was no change in the time, then discard this
1906 // count. A somewhat cheezy strategy, but hey.
1907 mCount--;
1908 }
1909 }
1910 }
1911
Joe Onoratoabded112016-02-08 16:49:39 -08001912 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001913 if (mNesting > 0) {
1914 mNesting = 1;
1915 stopRunningLocked(elapsedRealtimeMs);
1916 }
1917 }
1918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 // Update the total time for all other running Timers with the same type as this Timer
1920 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001921 private static long refreshTimersLocked(long batteryRealtime,
1922 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001923 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 final int N = pool.size();
1925 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001926 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 long heldTime = batteryRealtime - t.mUpdateTime;
1928 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001929 final long myTime = heldTime / N;
1930 if (t == self) {
1931 selfTime = myTime;
1932 }
1933 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
1935 t.mUpdateTime = batteryRealtime;
1936 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001937 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939
Evan Millarc64edde2009-04-18 12:26:32 -07001940 @Override
1941 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001942 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1943 curBatteryRealtime = mUpdateTime + mTimeout;
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 return mTotalTime + (mNesting > 0
1946 ? (curBatteryRealtime - mUpdateTime)
1947 / (mTimerPool != null ? mTimerPool.size() : 1)
1948 : 0);
1949 }
1950
Evan Millarc64edde2009-04-18 12:26:32 -07001951 @Override
1952 protected int computeCurrentCountLocked() {
1953 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
1955
Adam Lesinskie08af192015-03-25 16:42:59 -07001956 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001957 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001958 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001959 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001960 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001961 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001962 }
1963 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001964 return canDetach;
1965 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001966
Adam Lesinskie08af192015-03-25 16:42:59 -07001967 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001968 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001969 super.detach();
1970 if (mTimerPool != null) {
1971 mTimerPool.remove(this);
1972 }
1973 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001974
Adam Lesinskie08af192015-03-25 16:42:59 -07001975 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001976 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001977 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 mNesting = 0;
1979 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001980
1981 /**
1982 * Set the mark so that we can query later for the total time the timer has
1983 * accumulated since this point. The timer can be running or not.
1984 *
1985 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1986 */
1987 public void setMark(long elapsedRealtimeMs) {
1988 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1989 if (mNesting > 0) {
1990 // We are running.
1991 if (mTimerPool != null) {
1992 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1993 } else {
1994 mTotalTime += batteryRealtime - mUpdateTime;
1995 mUpdateTime = batteryRealtime;
1996 }
1997 }
1998 mTimeBeforeMark = mTotalTime;
1999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002001
Bookatz867c0d72017-03-07 18:23:42 -08002002 /**
2003 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2004 * TimeBase is effectively a subset of the other.
2005 */
2006 public static class DualTimer {
2007 // mMainTimer typically tracks the total time. May be pooled (but since it's a durationTimer,
2008 // it also has the unpooled getTotalDurationMsLocked() for STATS_SINCE_CHARGED).
2009 private final DurationTimer mMainTimer;
2010 // mSubTimer typically tracks only part of the total time, such as background time, as
2011 // determined by a subTimeBase. It is NOT pooled.
2012 private final DurationTimer mSubTimer;
2013
2014 /**
2015 * Creates a DualTimer to hold a mMainTimer and a mSubTimer.
2016 * The mMainTimer is based on the given timeBase and timerPool.
2017 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
2018 * the mMainTimer is.
2019 */
2020 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2021 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
2022 mMainTimer = new DurationTimer(clocks, uid, type, timerPool, timeBase, in);
2023 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2024 }
2025
2026 /**
2027 * Creates a DualTimer to hold a mMainTimer and a mSubTimer.
2028 * The mMainTimer is based on the given timeBase and timerPool.
2029 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
2030 * the mMainTimer is.
2031 */
2032 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2033 TimeBase timeBase, TimeBase subTimeBase) {
2034 mMainTimer = new DurationTimer(clocks, uid, type, timerPool, timeBase);
2035 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2036 }
2037
2038 /** Get the main timer. */
2039 public DurationTimer getMainTimer() {
2040 return mMainTimer;
2041 }
2042
2043 /** Get the secondary timer. */
2044 public DurationTimer getSubTimer() {
2045 return mSubTimer;
2046 }
2047
2048 public void startRunningLocked(long elapsedRealtimeMs) {
2049 mMainTimer.startRunningLocked(elapsedRealtimeMs);
2050 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2051 }
2052
2053 public void stopRunningLocked(long elapsedRealtimeMs) {
2054 mMainTimer.stopRunningLocked(elapsedRealtimeMs);
2055 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2056 }
2057
2058 public void stopAllRunningLocked(long elapsedRealtimeMs) {
2059 mMainTimer.stopAllRunningLocked(elapsedRealtimeMs);
2060 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2061 }
2062
2063 public void setMark(long elapsedRealtimeMs) {
2064 mMainTimer.setMark(elapsedRealtimeMs);
2065 mSubTimer.setMark(elapsedRealtimeMs);
2066 }
2067
2068 public boolean reset(boolean detachIfReset) {
2069 boolean active = false;
2070 active |= !mMainTimer.reset(detachIfReset);
2071 active |= !mSubTimer.reset(detachIfReset);
2072 return !active;
2073 }
2074
2075 public void detach() {
2076 mMainTimer.detach();
2077 mSubTimer.detach();
2078 }
2079
2080 /**
2081 * Writes a possibly null DualTimer to a Parcel.
2082 *
2083 * @param out the Parcel to which to write.
2084 * @param t a DualTimer, or null.
2085 */
2086 public static void writeDualTimerToParcel(Parcel out, DualTimer t, long elapsedRealtimeUs) {
2087 if (t != null) {
2088 out.writeInt(1);
2089 t.writeToParcel(out, elapsedRealtimeUs);
2090 } else {
2091 out.writeInt(0);
2092 }
2093 }
2094
2095 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2096 mMainTimer.writeToParcel(out, elapsedRealtimeUs);
2097 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2098 }
2099
2100 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
2101 mMainTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2102 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2103 }
2104
2105 public void readSummaryFromParcelLocked(Parcel in) {
2106 mMainTimer.readSummaryFromParcelLocked(in);
2107 mSubTimer.readSummaryFromParcelLocked(in);
2108 }
2109 }
2110
2111
Dianne Hackbornd953c532014-08-16 18:17:38 -07002112 public abstract class OverflowArrayMap<T> {
2113 private static final String OVERFLOW_NAME = "*overflow*";
2114
Dianne Hackborn657153b2016-07-29 14:54:14 -07002115 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002116 final ArrayMap<String, T> mMap = new ArrayMap<>();
2117 T mCurOverflow;
2118 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002119 long mLastOverflowTime;
2120 long mLastOverflowFinishTime;
2121 long mLastClearTime;
2122 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002123
Dianne Hackborn657153b2016-07-29 14:54:14 -07002124 public OverflowArrayMap(int uid) {
2125 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002126 }
2127
2128 public ArrayMap<String, T> getMap() {
2129 return mMap;
2130 }
2131
2132 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002133 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002134 mMap.clear();
2135 mCurOverflow = null;
2136 mActiveOverflow = null;
2137 }
2138
2139 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002140 if (name == null) {
2141 name = "";
2142 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002143 mMap.put(name, obj);
2144 if (OVERFLOW_NAME.equals(name)) {
2145 mCurOverflow = obj;
2146 }
2147 }
2148
2149 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002150 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002151 if (mActiveOverflow != null) {
2152 if (mActiveOverflow.size() == 0) {
2153 mActiveOverflow = null;
2154 }
2155 }
2156 if (mActiveOverflow == null) {
2157 // There is no currently active overflow, so we should no longer have
2158 // an overflow entry.
2159 if (mMap.containsKey(OVERFLOW_NAME)) {
2160 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2161 + mMap.get(OVERFLOW_NAME));
2162 mMap.remove(OVERFLOW_NAME);
2163 }
2164 mCurOverflow = null;
2165 } else {
2166 // There is currently active overflow, so we should still have an overflow entry.
2167 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2168 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2169 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2170 }
2171 }
2172 }
2173
2174 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002175 if (name == null) {
2176 name = "";
2177 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002178 T obj = mMap.get(name);
2179 if (obj != null) {
2180 return obj;
2181 }
2182
2183 // No object exists for the given name, but do we currently have it
2184 // running as part of the overflow?
2185 if (mActiveOverflow != null) {
2186 MutableInt over = mActiveOverflow.get(name);
2187 if (over != null) {
2188 // We are already actively counting this name in the overflow object.
2189 obj = mCurOverflow;
2190 if (obj == null) {
2191 // Shouldn't be here, but we'll try to recover.
2192 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2193 obj = mCurOverflow = instantiateObject();
2194 mMap.put(OVERFLOW_NAME, obj);
2195 }
2196 over.value++;
2197 return obj;
2198 }
2199 }
2200
2201 // No object exists for given name nor in the overflow; we need to make
2202 // a new one.
2203 final int N = mMap.size();
2204 if (N >= MAX_WAKELOCKS_PER_UID) {
2205 // Went over the limit on number of objects to track; this one goes
2206 // in to the overflow.
2207 obj = mCurOverflow;
2208 if (obj == null) {
2209 // Need to start overflow now...
2210 obj = mCurOverflow = instantiateObject();
2211 mMap.put(OVERFLOW_NAME, obj);
2212 }
2213 if (mActiveOverflow == null) {
2214 mActiveOverflow = new ArrayMap<>();
2215 }
2216 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002217 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002218 return obj;
2219 }
2220
2221 // Normal case where we just need to make a new object.
2222 obj = instantiateObject();
2223 mMap.put(name, obj);
2224 return obj;
2225 }
2226
2227 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002228 if (name == null) {
2229 name = "";
2230 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002231 T obj = mMap.get(name);
2232 if (obj != null) {
2233 return obj;
2234 }
2235
2236 // No object exists for the given name, but do we currently have it
2237 // running as part of the overflow?
2238 if (mActiveOverflow != null) {
2239 MutableInt over = mActiveOverflow.get(name);
2240 if (over != null) {
2241 // We are already actively counting this name in the overflow object.
2242 obj = mCurOverflow;
2243 if (obj != null) {
2244 over.value--;
2245 if (over.value <= 0) {
2246 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002247 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002248 }
2249 return obj;
2250 }
2251 }
2252 }
2253
2254 // Huh, they are stopping an active operation but we can't find one!
2255 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002256 StringBuilder sb = new StringBuilder();
2257 sb.append("Unable to find object for ");
2258 sb.append(name);
2259 sb.append(" in uid ");
2260 sb.append(mUid);
2261 sb.append(" mapsize=");
2262 sb.append(mMap.size());
2263 sb.append(" activeoverflow=");
2264 sb.append(mActiveOverflow);
2265 sb.append(" curoverflow=");
2266 sb.append(mCurOverflow);
2267 long now = SystemClock.elapsedRealtime();
2268 if (mLastOverflowTime != 0) {
2269 sb.append(" lastOverflowTime=");
2270 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2271 }
2272 if (mLastOverflowFinishTime != 0) {
2273 sb.append(" lastOverflowFinishTime=");
2274 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2275 }
2276 if (mLastClearTime != 0) {
2277 sb.append(" lastClearTime=");
2278 TimeUtils.formatDuration(mLastClearTime-now, sb);
2279 }
2280 if (mLastCleanupTime != 0) {
2281 sb.append(" lastCleanupTime=");
2282 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2283 }
2284 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002285 return null;
2286 }
2287
2288 public abstract T instantiateObject();
2289 }
2290
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002291 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2292 implements Parcelable {
2293 private final LongSamplingCounter mIdleTimeMillis;
2294 private final LongSamplingCounter mRxTimeMillis;
2295 private final LongSamplingCounter[] mTxTimeMillis;
2296 private final LongSamplingCounter mPowerDrainMaMs;
2297
2298 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2299 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2300 mRxTimeMillis = new LongSamplingCounter(timeBase);
2301 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2302 for (int i = 0; i < numTxStates; i++) {
2303 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2304 }
2305 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2306 }
2307
2308 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2309 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2310 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2311 final int recordedTxStates = in.readInt();
2312 if (recordedTxStates != numTxStates) {
2313 throw new ParcelFormatException("inconsistent tx state lengths");
2314 }
2315
2316 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2317 for (int i = 0; i < numTxStates; i++) {
2318 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2319 }
2320 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2321 }
2322
2323 public void readSummaryFromParcel(Parcel in) {
2324 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2325 mRxTimeMillis.readSummaryFromParcelLocked(in);
2326 final int recordedTxStates = in.readInt();
2327 if (recordedTxStates != mTxTimeMillis.length) {
2328 throw new ParcelFormatException("inconsistent tx state lengths");
2329 }
2330 for (LongSamplingCounter counter : mTxTimeMillis) {
2331 counter.readSummaryFromParcelLocked(in);
2332 }
2333 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2334 }
2335
2336 @Override
2337 public int describeContents() {
2338 return 0;
2339 }
2340
2341 public void writeSummaryToParcel(Parcel dest) {
2342 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2343 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2344 dest.writeInt(mTxTimeMillis.length);
2345 for (LongSamplingCounter counter : mTxTimeMillis) {
2346 counter.writeSummaryFromParcelLocked(dest);
2347 }
2348 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2349 }
2350
2351 @Override
2352 public void writeToParcel(Parcel dest, int flags) {
2353 mIdleTimeMillis.writeToParcel(dest);
2354 mRxTimeMillis.writeToParcel(dest);
2355 dest.writeInt(mTxTimeMillis.length);
2356 for (LongSamplingCounter counter : mTxTimeMillis) {
2357 counter.writeToParcel(dest);
2358 }
2359 mPowerDrainMaMs.writeToParcel(dest);
2360 }
2361
2362 public void reset(boolean detachIfReset) {
2363 mIdleTimeMillis.reset(detachIfReset);
2364 mRxTimeMillis.reset(detachIfReset);
2365 for (LongSamplingCounter counter : mTxTimeMillis) {
2366 counter.reset(detachIfReset);
2367 }
2368 mPowerDrainMaMs.reset(detachIfReset);
2369 }
2370
2371 public void detach() {
2372 mIdleTimeMillis.detach();
2373 mRxTimeMillis.detach();
2374 for (LongSamplingCounter counter : mTxTimeMillis) {
2375 counter.detach();
2376 }
2377 mPowerDrainMaMs.detach();
2378 }
2379
2380 /**
2381 * @return a LongSamplingCounter, measuring time spent in the idle state in
2382 * milliseconds.
2383 */
2384 @Override
2385 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002386 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002387 }
2388
2389 /**
2390 * @return a LongSamplingCounter, measuring time spent in the receive state in
2391 * milliseconds.
2392 */
2393 @Override
2394 public LongSamplingCounter getRxTimeCounter() {
2395 return mRxTimeMillis;
2396 }
2397
2398 /**
2399 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2400 * milliseconds.
2401 */
2402 @Override
2403 public LongSamplingCounter[] getTxTimeCounters() {
2404 return mTxTimeMillis;
2405 }
2406
2407 /**
2408 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2409 */
2410 @Override
2411 public LongSamplingCounter getPowerCounter() {
2412 return mPowerDrainMaMs;
2413 }
2414 }
2415
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002416 /*
2417 * Get the wakeup reason counter, and create a new one if one
2418 * doesn't already exist.
2419 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002420 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2421 SamplingTimer timer = mWakeupReasonStats.get(name);
2422 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002423 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002424 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002425 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002426 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002427 }
2428
Evan Millarc64edde2009-04-18 12:26:32 -07002429 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002430 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002431 * doesn't already exist.
2432 */
2433 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2434 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2435 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002436 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002437 mKernelWakelockStats.put(name, kwlt);
2438 }
2439 return kwlt;
2440 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002441
James Carr3a226052016-07-01 14:49:52 -07002442 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2443 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2444 if (kmt == null) {
2445 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2446 mKernelMemoryStats.put(bucket, kmt);
2447 }
2448 return kmt;
2449 }
2450
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002451 private int writeHistoryTag(HistoryTag tag) {
2452 Integer idxObj = mHistoryTagPool.get(tag);
2453 int idx;
2454 if (idxObj != null) {
2455 idx = idxObj;
2456 } else {
2457 idx = mNextHistoryTagIdx;
2458 HistoryTag key = new HistoryTag();
2459 key.setTo(tag);
2460 tag.poolIdx = idx;
2461 mHistoryTagPool.put(key, idx);
2462 mNextHistoryTagIdx++;
2463 mNumHistoryTagChars += key.string.length() + 1;
2464 }
2465 return idx;
2466 }
2467
2468 private void readHistoryTag(int index, HistoryTag tag) {
2469 tag.string = mReadHistoryStrings[index];
2470 tag.uid = mReadHistoryUids[index];
2471 tag.poolIdx = index;
2472 }
2473
Adam Lesinski926969b2016-04-28 17:31:12 -07002474 /*
2475 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2476
2477 There is always the first token, which may contain the delta time, or an indicator of
2478 the length of the time (int or long) following this token.
2479
2480 First token: always present,
2481 31 23 15 7 0
2482 â–ˆ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â–ˆ
2483
2484 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2485 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2486 delta time.
2487 A: battery level changed and an int follows with battery data.
2488 B: state changed and an int follows with state change data.
2489 C: state2 has changed and an int follows with state2 change data.
2490 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2491 E: event data has changed and an event struct follows.
2492 F: battery charge in coulombs has changed and an int with the charge follows.
2493 G: state flag denoting that the mobile radio was active.
2494 H: state flag denoting that the wifi radio was active.
2495 I: state flag denoting that a wifi scan occurred.
2496 J: state flag denoting that a wifi full lock was held.
2497 K: state flag denoting that the gps was on.
2498 L: state flag denoting that a wakelock was held.
2499 M: state flag denoting that the cpu was running.
2500
2501 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2502 with the time delta.
2503
2504 Battery level int: if A in the first token is set,
2505 31 23 15 7 0
2506 â–ˆ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â–ˆ
2507
2508 D: indicates that extra history details follow.
2509 V: the battery voltage.
2510 T: the battery temperature.
2511 L: the battery level (out of 100).
2512
2513 State change int: if B in the first token is set,
2514 31 23 15 7 0
2515 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2516
2517 A: wifi multicast was on.
2518 B: battery was plugged in.
2519 C: screen was on.
2520 D: phone was scanning for signal.
2521 E: audio was on.
2522 F: a sensor was active.
2523
2524 State2 change int: if C in the first token is set,
2525 31 23 15 7 0
2526 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2527
2528 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2529 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2530 C: a bluetooth scan was active.
2531 D: the camera was active.
2532 E: bluetooth was on.
2533 F: a phone call was active.
2534 G: the device was charging.
2535 H: 2 bits indicating the device-idle (doze) state: off, light, full
2536 I: the flashlight was on.
2537 J: wifi was on.
2538 K: wifi was running.
2539 L: video was playing.
2540 M: power save mode was on.
2541
2542 Wakelock/wakereason struct: if D in the first token is set,
2543 TODO(adamlesinski): describe wakelock/wakereason struct.
2544
2545 Event struct: if E in the first token is set,
2546 TODO(adamlesinski): describe the event struct.
2547
2548 History step details struct: if D in the battery level int is set,
2549 TODO(adamlesinski): describe the history step details struct.
2550
2551 Battery charge int: if F in the first token is set, an int representing the battery charge
2552 in coulombs follows.
2553 */
2554
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002555 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002556 static final int DELTA_TIME_MASK = 0x7ffff;
2557 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2558 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2559 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002560 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002561 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002562 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002563 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002564 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002565 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002566 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002567 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002568 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002569 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002570 // Flag in delta int: contains the battery charge count in uAh.
2571 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002572 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002573 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002574
2575 // These are the pieces of battery state that are packed in to the upper bits of
2576 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002577 // in STATE_BATTERY_MASK.
2578 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002579 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2580 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2581 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2582 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2583 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2584 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002585
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002586 // We use the low bit of the battery state int to indicate that we have full details
2587 // from a battery level change.
2588 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2589
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002590 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002591 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002592 dest.writeInt(DELTA_TIME_ABS);
2593 cur.writeToParcel(dest, 0);
2594 return;
2595 }
2596
2597 final long deltaTime = cur.time - last.time;
2598 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2599 final int lastStateInt = buildStateInt(last);
2600
2601 int deltaTimeToken;
2602 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2603 deltaTimeToken = DELTA_TIME_LONG;
2604 } else if (deltaTime >= DELTA_TIME_ABS) {
2605 deltaTimeToken = DELTA_TIME_INT;
2606 } else {
2607 deltaTimeToken = (int)deltaTime;
2608 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002609 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002610 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2611 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2612 final boolean computeStepDetails = includeStepDetails != 0
2613 || mLastHistoryStepDetails == null;
2614 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002615 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2616 if (batteryLevelIntChanged) {
2617 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2618 }
2619 final int stateInt = buildStateInt(cur);
2620 final boolean stateIntChanged = stateInt != lastStateInt;
2621 if (stateIntChanged) {
2622 firstToken |= DELTA_STATE_FLAG;
2623 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002624 final boolean state2IntChanged = cur.states2 != last.states2;
2625 if (state2IntChanged) {
2626 firstToken |= DELTA_STATE2_FLAG;
2627 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002628 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002629 firstToken |= DELTA_WAKELOCK_FLAG;
2630 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002631 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2632 firstToken |= DELTA_EVENT_FLAG;
2633 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002634
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002635 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2636 if (batteryChargeChanged) {
2637 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002638 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002639 dest.writeInt(firstToken);
2640 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2641 + " deltaTime=" + deltaTime);
2642
2643 if (deltaTimeToken >= DELTA_TIME_INT) {
2644 if (deltaTimeToken == DELTA_TIME_INT) {
2645 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2646 dest.writeInt((int)deltaTime);
2647 } else {
2648 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2649 dest.writeLong(deltaTime);
2650 }
2651 }
2652 if (batteryLevelIntChanged) {
2653 dest.writeInt(batteryLevelInt);
2654 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2655 + Integer.toHexString(batteryLevelInt)
2656 + " batteryLevel=" + cur.batteryLevel
2657 + " batteryTemp=" + cur.batteryTemperature
2658 + " batteryVolt=" + (int)cur.batteryVoltage);
2659 }
2660 if (stateIntChanged) {
2661 dest.writeInt(stateInt);
2662 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2663 + Integer.toHexString(stateInt)
2664 + " batteryStatus=" + cur.batteryStatus
2665 + " batteryHealth=" + cur.batteryHealth
2666 + " batteryPlugType=" + cur.batteryPlugType
2667 + " states=0x" + Integer.toHexString(cur.states));
2668 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002669 if (state2IntChanged) {
2670 dest.writeInt(cur.states2);
2671 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2672 + Integer.toHexString(cur.states2));
2673 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002674 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2675 int wakeLockIndex;
2676 int wakeReasonIndex;
2677 if (cur.wakelockTag != null) {
2678 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2679 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2680 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2681 } else {
2682 wakeLockIndex = 0xffff;
2683 }
2684 if (cur.wakeReasonTag != null) {
2685 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2686 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2687 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2688 } else {
2689 wakeReasonIndex = 0xffff;
2690 }
2691 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002692 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002693 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002694 int index = writeHistoryTag(cur.eventTag);
2695 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002696 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002697 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2698 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2699 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002700 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002701 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002702 if (mPlatformIdleStateCallback != null) {
2703 mCurHistoryStepDetails.statPlatformIdleState =
2704 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2705 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2706 mCurHistoryStepDetails.statPlatformIdleState);
2707 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002708 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2709 if (includeStepDetails != 0) {
2710 mCurHistoryStepDetails.writeToParcel(dest);
2711 }
2712 cur.stepDetails = mCurHistoryStepDetails;
2713 mLastHistoryStepDetails = mCurHistoryStepDetails;
2714 } else {
2715 cur.stepDetails = null;
2716 }
2717 if (mLastHistoryStepLevel < cur.batteryLevel) {
2718 mLastHistoryStepDetails = null;
2719 }
2720 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002721
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002722 if (batteryChargeChanged) {
2723 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2724 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002725 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002726 }
2727
2728 private int buildBatteryLevelInt(HistoryItem h) {
2729 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002730 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2731 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2732 }
2733
2734 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2735 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2736 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2737 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002738 }
2739
2740 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002741 int plugType = 0;
2742 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2743 plugType = 1;
2744 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2745 plugType = 2;
2746 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2747 plugType = 3;
2748 }
2749 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2750 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2751 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002752 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002753 }
2754
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002755 private void computeHistoryStepDetails(final HistoryStepDetails out,
2756 final HistoryStepDetails last) {
2757 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2758
2759 // Perform a CPU update right after we do this collection, so we have started
2760 // collecting good data for the next step.
2761 requestImmediateCpuUpdate();
2762
2763 if (last == null) {
2764 // We are not generating a delta, so all we need to do is reset the stats
2765 // we will later be doing a delta from.
2766 final int NU = mUidStats.size();
2767 for (int i=0; i<NU; i++) {
2768 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2769 uid.mLastStepUserTime = uid.mCurStepUserTime;
2770 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2771 }
2772 mLastStepCpuUserTime = mCurStepCpuUserTime;
2773 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2774 mLastStepStatUserTime = mCurStepStatUserTime;
2775 mLastStepStatSystemTime = mCurStepStatSystemTime;
2776 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2777 mLastStepStatIrqTime = mCurStepStatIrqTime;
2778 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2779 mLastStepStatIdleTime = mCurStepStatIdleTime;
2780 tmp.clear();
2781 return;
2782 }
2783 if (DEBUG) {
2784 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2785 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2786 + " irq=" + mLastStepStatIrqTime + " sirq="
2787 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2788 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2789 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2790 + " irq=" + mCurStepStatIrqTime + " sirq="
2791 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2792 }
2793 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2794 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2795 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2796 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2797 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2798 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2799 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2800 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2801 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2802 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2803 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2804 final int NU = mUidStats.size();
2805 for (int i=0; i<NU; i++) {
2806 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2807 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2808 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2809 final int totalTime = totalUTime + totalSTime;
2810 uid.mLastStepUserTime = uid.mCurStepUserTime;
2811 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2812 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2813 continue;
2814 }
2815 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2816 out.appCpuUid3 = uid.mUid;
2817 out.appCpuUTime3 = totalUTime;
2818 out.appCpuSTime3 = totalSTime;
2819 } else {
2820 out.appCpuUid3 = out.appCpuUid2;
2821 out.appCpuUTime3 = out.appCpuUTime2;
2822 out.appCpuSTime3 = out.appCpuSTime2;
2823 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2824 out.appCpuUid2 = uid.mUid;
2825 out.appCpuUTime2 = totalUTime;
2826 out.appCpuSTime2 = totalSTime;
2827 } else {
2828 out.appCpuUid2 = out.appCpuUid1;
2829 out.appCpuUTime2 = out.appCpuUTime1;
2830 out.appCpuSTime2 = out.appCpuSTime1;
2831 out.appCpuUid1 = uid.mUid;
2832 out.appCpuUTime1 = totalUTime;
2833 out.appCpuSTime1 = totalSTime;
2834 }
2835 }
2836 }
2837 mLastStepCpuUserTime = mCurStepCpuUserTime;
2838 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2839 mLastStepStatUserTime = mCurStepStatUserTime;
2840 mLastStepStatSystemTime = mCurStepStatSystemTime;
2841 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2842 mLastStepStatIrqTime = mCurStepStatIrqTime;
2843 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2844 mLastStepStatIdleTime = mCurStepStatIdleTime;
2845 }
2846
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002847 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2848 int firstToken = src.readInt();
2849 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002850 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002851 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002852 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2853 + " deltaTimeToken=" + deltaTimeToken);
2854
2855 if (deltaTimeToken < DELTA_TIME_ABS) {
2856 cur.time += deltaTimeToken;
2857 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2858 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002859 cur.numReadInts += 2;
2860 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002861 cur.readFromParcel(src);
2862 return;
2863 } else if (deltaTimeToken == DELTA_TIME_INT) {
2864 int delta = src.readInt();
2865 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002866 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002867 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2868 } else {
2869 long delta = src.readLong();
2870 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2871 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002872 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002873 }
2874
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002875 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002876 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002877 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002878 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002879 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002880 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2881 + Integer.toHexString(batteryLevelInt)
2882 + " batteryLevel=" + cur.batteryLevel
2883 + " batteryTemp=" + cur.batteryTemperature
2884 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002885 } else {
2886 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002887 }
2888
2889 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2890 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002891 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002892 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2893 & STATE_BATTERY_STATUS_MASK);
2894 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2895 & STATE_BATTERY_HEALTH_MASK);
2896 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2897 & STATE_BATTERY_PLUG_MASK);
2898 switch (cur.batteryPlugType) {
2899 case 1:
2900 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2901 break;
2902 case 2:
2903 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2904 break;
2905 case 3:
2906 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2907 break;
2908 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002909 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002910 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2911 + Integer.toHexString(stateInt)
2912 + " batteryStatus=" + cur.batteryStatus
2913 + " batteryHealth=" + cur.batteryHealth
2914 + " batteryPlugType=" + cur.batteryPlugType
2915 + " states=0x" + Integer.toHexString(cur.states));
2916 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002917 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002918 }
2919
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002920 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2921 cur.states2 = src.readInt();
2922 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2923 + Integer.toHexString(cur.states2));
2924 }
2925
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002926 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002927 int indexes = src.readInt();
2928 int wakeLockIndex = indexes&0xffff;
2929 int wakeReasonIndex = (indexes>>16)&0xffff;
2930 if (wakeLockIndex != 0xffff) {
2931 cur.wakelockTag = cur.localWakelockTag;
2932 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2933 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2934 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2935 } else {
2936 cur.wakelockTag = null;
2937 }
2938 if (wakeReasonIndex != 0xffff) {
2939 cur.wakeReasonTag = cur.localWakeReasonTag;
2940 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2941 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2942 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2943 } else {
2944 cur.wakeReasonTag = null;
2945 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002946 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002947 } else {
2948 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002949 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002950 }
2951
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002952 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002953 cur.eventTag = cur.localEventTag;
2954 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002955 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002956 final int index = ((codeAndIndex>>16)&0xffff);
2957 readHistoryTag(index, cur.eventTag);
2958 cur.numReadInts += 1;
2959 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2960 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2961 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002962 } else {
2963 cur.eventCode = HistoryItem.EVENT_NONE;
2964 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002965
2966 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2967 cur.stepDetails = mReadHistoryStepDetails;
2968 cur.stepDetails.readFromParcel(src);
2969 } else {
2970 cur.stepDetails = null;
2971 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002972
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002973 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2974 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002975 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002976 }
2977
Dianne Hackbornfc064132014-06-02 12:42:12 -07002978 @Override
2979 public void commitCurrentHistoryBatchLocked() {
2980 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2981 }
2982
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002983 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002984 if (!mHaveBatteryLevel || !mRecordingHistory) {
2985 return;
2986 }
2987
Dianne Hackborn40c87252014-03-19 16:55:40 -07002988 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002989 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2990 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002991 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002992 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002993 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2994 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002995 + Integer.toHexString(lastDiffStates) + " diff2="
2996 + Integer.toHexString(diffStates2) + " lastDiff2="
2997 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002998 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002999 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003000 && (diffStates2&lastDiffStates2) == 0
3001 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3002 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003003 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003004 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003005 || cur.eventCode == HistoryItem.EVENT_NONE)
3006 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3007 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3008 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3009 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3010 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3011 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003012 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003013 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003014 // as long as no bit has changed both between now and the last entry, as
3015 // well as the last entry and the one before it (so we capture any toggles).
3016 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003017 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3018 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3019 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003020 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003021 // If the last written history had a wakelock tag, we need to retain it.
3022 // Note that the condition above made sure that we aren't in a case where
3023 // both it and the current history item have a wakelock tag.
3024 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003025 cur.wakelockTag = cur.localWakelockTag;
3026 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003027 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003028 // If the last written history had a wake reason tag, we need to retain it.
3029 // Note that the condition above made sure that we aren't in a case where
3030 // both it and the current history item have a wakelock tag.
3031 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003032 cur.wakeReasonTag = cur.localWakeReasonTag;
3033 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003034 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003035 // If the last written history had an event, we need to retain it.
3036 // Note that the condition above made sure that we aren't in a case where
3037 // both it and the current history item have an event.
3038 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003039 cur.eventCode = mHistoryLastWritten.eventCode;
3040 cur.eventTag = cur.localEventTag;
3041 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003042 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003043 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003044 }
3045
Adam Lesinski45489782016-12-15 23:45:17 -08003046 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003047 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003048 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3049 // Clients can't deal with history buffers this large. This only
3050 // really happens when the device is on charger and interacted with
3051 // for long periods of time, like in retail mode. Since the device is
3052 // most likely charged, when unplugged, stats would have reset anyways.
3053 // Reset the stats and mark that we overflowed.
3054 // b/32540341
3055 resetAllStatsLocked();
3056
3057 // Mark that we want to set *OVERFLOW* event and the RESET:START
3058 // events.
3059 recordResetDueToOverflow = true;
3060
3061 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003062 if (!mHistoryOverflow) {
3063 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003064 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3065 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003066 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003067 }
3068
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003069 // After overflow, we allow various bit-wise states to settle to 0.
3070 boolean writeAnyway = false;
3071 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3072 & mActiveHistoryStates;
3073 if (mHistoryLastWritten.states != curStates) {
3074 // mActiveHistoryStates keeps track of which bits in .states are now being
3075 // forced to 0.
3076 int old = mActiveHistoryStates;
3077 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3078 writeAnyway |= old != mActiveHistoryStates;
3079 }
3080 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3081 & mActiveHistoryStates2;
3082 if (mHistoryLastWritten.states2 != curStates2) {
3083 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3084 // forced to 0.
3085 int old = mActiveHistoryStates2;
3086 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3087 writeAnyway |= old != mActiveHistoryStates2;
3088 }
3089
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003090 // Once we've reached the maximum number of items, we only
3091 // record changes to the battery level and the most interesting states.
3092 // Once we've reached the maximum maximum number of items, we only
3093 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003094 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003095 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003096 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003097 & HistoryItem.MOST_INTERESTING_STATES) == 0
3098 || ((mHistoryLastWritten.states2^cur.states2)
3099 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003100 return;
3101 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003102
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003103 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003104 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003105 }
3106
Adam Lesinski45489782016-12-15 23:45:17 -08003107 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003108 // The history is currently empty; we need it to start with a time stamp.
3109 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003110 if (recordResetDueToOverflow) {
3111 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3112 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003113 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3114 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003115 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003116 }
3117
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003118 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3119 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003120 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003121 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003122 }
3123 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3124 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003125 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003126 mHistoryLastWritten.states &= mActiveHistoryStates;
3127 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003128 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003129 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003130 cur.wakelockTag = null;
3131 cur.wakeReasonTag = null;
3132 cur.eventCode = HistoryItem.EVENT_NONE;
3133 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003134 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3135 + " now " + mHistoryBuffer.dataPosition()
3136 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003137 }
3138
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003139 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003140 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003141
Dianne Hackborn40c87252014-03-19 16:55:40 -07003142 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003143 if (mTrackRunningHistoryElapsedRealtime != 0) {
3144 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3145 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3146 if (diffUptime < (diffElapsed-20)) {
3147 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3148 mHistoryAddTmp.setTo(mHistoryLastWritten);
3149 mHistoryAddTmp.wakelockTag = null;
3150 mHistoryAddTmp.wakeReasonTag = null;
3151 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3152 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3153 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3154 }
3155 }
3156 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3157 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3158 mTrackRunningHistoryUptime = uptimeMs;
3159 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3160 }
3161
3162 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3163 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003164
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003165 if (!USE_OLD_HISTORY) {
3166 return;
3167 }
3168
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003169 if (!mHaveBatteryLevel || !mRecordingHistory) {
3170 return;
3171 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003172
3173 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003174 // and no states have since the last recorded entry changed and
3175 // are now resetting back to their original value, then just collapse
3176 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003177 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003178 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003179 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3180 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003181 // If the current is the same as the one before, then we no
3182 // longer need the entry.
3183 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003184 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003185 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003186 mHistoryLastEnd.next = null;
3187 mHistoryEnd.next = mHistoryCache;
3188 mHistoryCache = mHistoryEnd;
3189 mHistoryEnd = mHistoryLastEnd;
3190 mHistoryLastEnd = null;
3191 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003192 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3193 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003194 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003195 }
3196 return;
3197 }
3198
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003199 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003200 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003201
3202 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3203 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003204 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003205 }
3206
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003207 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3208 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003209 // record changes to the battery level and the most interesting states.
3210 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003211 // record changes to the battery level.
3212 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003213 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003214 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003215 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003216 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003217 return;
3218 }
3219 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003220
Dianne Hackborn40c87252014-03-19 16:55:40 -07003221 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003222 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003223
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003224 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003225 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003226 mHistoryCur.eventCode = code;
3227 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3228 mHistoryCur.eventTag.string = name;
3229 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003230 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003231 }
3232
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003233 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003234 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003235 if (rec != null) {
3236 mHistoryCache = rec.next;
3237 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003238 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003239 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003240 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003241
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003242 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003243 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003244
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003245 void addHistoryRecordLocked(HistoryItem rec) {
3246 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003247 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003248 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003249 if (mHistoryEnd != null) {
3250 mHistoryEnd.next = rec;
3251 mHistoryEnd = rec;
3252 } else {
3253 mHistory = mHistoryEnd = rec;
3254 }
3255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003256
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003257 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003258 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003259 if (USE_OLD_HISTORY) {
3260 if (mHistory != null) {
3261 mHistoryEnd.next = mHistoryCache;
3262 mHistoryCache = mHistory;
3263 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3264 }
3265 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003266 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003267
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003268 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003269 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003270 mTrackRunningHistoryElapsedRealtime = 0;
3271 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003272
3273 mHistoryBuffer.setDataSize(0);
3274 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003275 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003276 mHistoryLastLastWritten.clear();
3277 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003278 mHistoryTagPool.clear();
3279 mNextHistoryTagIdx = 0;
3280 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003281 mHistoryBufferLastPos = -1;
3282 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003283 mActiveHistoryStates = 0xffffffff;
3284 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003285 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003286
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003287 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
3288 long realtime) {
Bookatz867c0d72017-03-07 18:23:42 -08003289 boolean batteryStatusChanged = mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
3290
3291 if (batteryStatusChanged) {
3292 for (int i=0; i<mUidStats.size(); i++) {
3293 mUidStats.valueAt(i).updateBgTimeBase(uptime, realtime);
3294 }
3295 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003296
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003297 boolean unpluggedScreenOff = unplugged && screenOff;
3298 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
3299 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07003300 if (DEBUG_ENERGY_CPU) {
3301 Slog.d(TAG, "Updating cpu time because screen is now " +
3302 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003303 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003304 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003305 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 }
3307 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003308
Dianne Hackborn099bc622014-01-22 13:39:16 -08003309 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3310 mIsolatedUids.put(isolatedUid, appUid);
3311 }
3312
Adam Lesinski61db88f2015-07-01 15:05:07 -07003313 /**
3314 * Schedules a read of the latest cpu times before removing the isolated UID.
3315 * @see #removeIsolatedUidLocked(int)
3316 */
3317 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003318 int curUid = mIsolatedUids.get(isolatedUid, -1);
3319 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003320 if (mExternalSync != null) {
3321 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3322 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003323 }
3324 }
3325
Adam Lesinski61db88f2015-07-01 15:05:07 -07003326 /**
3327 * This should only be called after the cpu times have been read.
3328 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3329 */
3330 public void removeIsolatedUidLocked(int isolatedUid) {
3331 mIsolatedUids.delete(isolatedUid);
3332 mKernelUidCpuTimeReader.removeUid(isolatedUid);
3333 }
3334
Dianne Hackborn099bc622014-01-22 13:39:16 -08003335 public int mapUid(int uid) {
3336 int isolated = mIsolatedUids.get(uid, -1);
3337 return isolated > 0 ? isolated : uid;
3338 }
3339
3340 public void noteEventLocked(int code, String name, int uid) {
3341 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003342 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3343 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003344 }
Joe Onoratoabded112016-02-08 16:49:39 -08003345 final long elapsedRealtime = mClocks.elapsedRealtime();
3346 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003347 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003348 }
3349
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003350 boolean ensureStartClockTime(final long currentTime) {
3351 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3352 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3353 // If the start clock time has changed by more than a year, then presumably
3354 // the previous time was completely bogus. So we are going to figure out a
3355 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003356 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003357 return true;
3358 }
3359 return false;
3360 }
3361
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003362 public void noteCurrentTimeChangedLocked() {
3363 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003364 final long elapsedRealtime = mClocks.elapsedRealtime();
3365 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003366 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003367 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003368 }
3369
Dianne Hackborn61659e52014-07-09 16:13:01 -07003370 public void noteProcessStartLocked(String name, int uid) {
3371 uid = mapUid(uid);
3372 if (isOnBattery()) {
3373 Uid u = getUidStatsLocked(uid);
3374 u.getProcessStatsLocked(name).incStartsLocked();
3375 }
3376 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3377 return;
3378 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003379 if (!mRecordAllHistory) {
3380 return;
3381 }
Joe Onoratoabded112016-02-08 16:49:39 -08003382 final long elapsedRealtime = mClocks.elapsedRealtime();
3383 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003384 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3385 }
3386
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003387 public void noteProcessCrashLocked(String name, int uid) {
3388 uid = mapUid(uid);
3389 if (isOnBattery()) {
3390 Uid u = getUidStatsLocked(uid);
3391 u.getProcessStatsLocked(name).incNumCrashesLocked();
3392 }
3393 }
3394
3395 public void noteProcessAnrLocked(String name, int uid) {
3396 uid = mapUid(uid);
3397 if (isOnBattery()) {
3398 Uid u = getUidStatsLocked(uid);
3399 u.getProcessStatsLocked(name).incNumAnrsLocked();
3400 }
3401 }
3402
Dianne Hackborna8d10942015-11-19 17:55:19 -08003403 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003404 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003405 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003406 }
3407
3408 public void noteProcessFinishLocked(String name, int uid) {
3409 uid = mapUid(uid);
3410 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3411 return;
3412 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003413 if (!mRecordAllHistory) {
3414 return;
3415 }
Joe Onoratoabded112016-02-08 16:49:39 -08003416 final long elapsedRealtime = mClocks.elapsedRealtime();
3417 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003418 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003419 }
3420
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003421 public void noteSyncStartLocked(String name, int uid) {
3422 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003423 final long elapsedRealtime = mClocks.elapsedRealtime();
3424 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003425 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3426 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3427 return;
3428 }
3429 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3430 }
3431
3432 public void noteSyncFinishLocked(String name, int uid) {
3433 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003434 final long elapsedRealtime = mClocks.elapsedRealtime();
3435 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003436 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3437 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3438 return;
3439 }
3440 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3441 }
3442
3443 public void noteJobStartLocked(String name, int uid) {
3444 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003445 final long elapsedRealtime = mClocks.elapsedRealtime();
3446 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003447 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3448 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3449 return;
3450 }
3451 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3452 }
3453
3454 public void noteJobFinishLocked(String name, int uid) {
3455 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003456 final long elapsedRealtime = mClocks.elapsedRealtime();
3457 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003458 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3459 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3460 return;
3461 }
3462 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3463 }
3464
Dianne Hackborn1e383822015-04-10 14:02:33 -07003465 public void noteAlarmStartLocked(String name, int uid) {
3466 if (!mRecordAllHistory) {
3467 return;
3468 }
3469 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003470 final long elapsedRealtime = mClocks.elapsedRealtime();
3471 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003472 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3473 return;
3474 }
3475 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3476 }
3477
3478 public void noteAlarmFinishLocked(String name, int uid) {
3479 if (!mRecordAllHistory) {
3480 return;
3481 }
3482 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003483 final long elapsedRealtime = mClocks.elapsedRealtime();
3484 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003485 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3486 return;
3487 }
3488 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3489 }
3490
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003491 private void requestWakelockCpuUpdate() {
3492 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3493 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3494 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3495 }
3496 }
3497
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003498 private void requestImmediateCpuUpdate() {
3499 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3500 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3501 }
3502
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003503 public void setRecordAllHistoryLocked(boolean enabled) {
3504 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003505 if (!enabled) {
3506 // Clear out any existing state.
3507 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003508 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003509 // Record the currently running processes as stopping, now that we are no
3510 // longer tracking them.
3511 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3512 HistoryItem.EVENT_PROC);
3513 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003514 long mSecRealtime = mClocks.elapsedRealtime();
3515 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003516 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3517 SparseIntArray uids = ent.getValue();
3518 for (int j=0; j<uids.size(); j++) {
3519 addHistoryEventLocked(mSecRealtime, mSecUptime,
3520 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3521 }
3522 }
3523 }
3524 } else {
3525 // Record the currently running processes as starting, now that we are tracking them.
3526 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3527 HistoryItem.EVENT_PROC);
3528 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003529 long mSecRealtime = mClocks.elapsedRealtime();
3530 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003531 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3532 SparseIntArray uids = ent.getValue();
3533 for (int j=0; j<uids.size(); j++) {
3534 addHistoryEventLocked(mSecRealtime, mSecUptime,
3535 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3536 }
3537 }
3538 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003539 }
3540 }
3541
Dianne Hackborn9a755432014-05-15 17:05:22 -07003542 public void setNoAutoReset(boolean enabled) {
3543 mNoAutoReset = enabled;
3544 }
3545
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003546 public void setPretendScreenOff(boolean pretendScreenOff) {
3547 mPretendScreenOff = pretendScreenOff;
3548 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3549 }
3550
Dianne Hackborn9a755432014-05-15 17:05:22 -07003551 private String mInitialAcquireWakeName;
3552 private int mInitialAcquireWakeUid = -1;
3553
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003554 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003555 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003556 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003557 if (type == WAKE_TYPE_PARTIAL) {
3558 // Only care about partial wake locks, since full wake locks
3559 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003560 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003561 if (historyName == null) {
3562 historyName = name;
3563 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003564 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003565 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3566 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003567 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003568 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003569 }
3570 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003571 if (mWakeLockNesting == 0) {
3572 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3573 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3574 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003575 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003576 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003577 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003578 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003579 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003580 } else if (!mWakeLockImportant && !unimportantForLogging
3581 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003582 if (mHistoryLastWritten.wakelockTag != null) {
3583 // We'll try to update the last tag.
3584 mHistoryLastWritten.wakelockTag = null;
3585 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003586 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003587 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003588 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003589 }
3590 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003591 }
3592 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003593 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003594 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003595 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3596 // We only update the cpu time when a wake lock is acquired if the screen is off.
3597 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3598 if (DEBUG_ENERGY_CPU) {
3599 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3600 }
3601 requestWakelockCpuUpdate();
3602 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003603 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003604 }
3605 }
3606
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003607 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3608 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003609 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003610 if (type == WAKE_TYPE_PARTIAL) {
3611 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003612 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003613 if (historyName == null) {
3614 historyName = name;
3615 }
3616 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3617 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003618 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003619 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003620 }
3621 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003622 if (mWakeLockNesting == 0) {
3623 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3624 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3625 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003626 mInitialAcquireWakeName = null;
3627 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003628 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003629 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003630 }
3631 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003632 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3633 if (DEBUG_ENERGY_CPU) {
3634 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3635 }
3636 requestWakelockCpuUpdate();
3637 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003638 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003639 }
3640 }
3641
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003642 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3643 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003644 final long elapsedRealtime = mClocks.elapsedRealtime();
3645 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003646 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003647 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003648 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003649 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003650 }
3651 }
3652
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003653 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3654 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003655 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003656 final long elapsedRealtime = mClocks.elapsedRealtime();
3657 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003658 // For correct semantics, we start the need worksources first, so that we won't
3659 // make inappropriate history items as if all wake locks went away and new ones
3660 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003661 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003662 for (int i=0; i<NN; i++) {
3663 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003664 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003665 }
3666 final int NO = ws.size();
3667 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003668 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003669 }
3670 }
3671
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003672 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3673 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003674 final long elapsedRealtime = mClocks.elapsedRealtime();
3675 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003676 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003677 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003678 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003679 }
3680 }
3681
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003682 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3683 uid = mapUid(uid);
3684 final long elapsedRealtime = mClocks.elapsedRealtime();
3685 final long uptime = mClocks.uptimeMillis();
3686 if (historyName == null) {
3687 historyName = name;
3688 }
3689 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3690 0)) {
3691 return;
3692 }
3693 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3694 historyName, uid);
3695 }
3696
3697 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3698 uid = mapUid(uid);
3699 final long elapsedRealtime = mClocks.elapsedRealtime();
3700 final long uptime = mClocks.uptimeMillis();
3701 if (historyName == null) {
3702 historyName = name;
3703 }
3704 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3705 0)) {
3706 return;
3707 }
3708 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3709 historyName, uid);
3710 }
3711
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003712 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3713 if (mLastWakeupReason != null) {
3714 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003715 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003716 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003717 mLastWakeupReason = null;
3718 }
3719 }
3720
3721 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003722 final long elapsedRealtime = mClocks.elapsedRealtime();
3723 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003724 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003725 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003726 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003727 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3728 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003729 mHistoryCur.wakeReasonTag.uid = 0;
3730 mLastWakeupReason = reason;
3731 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003732 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003733 }
3734
Adam Lesinski72478f02015-06-17 15:39:43 -07003735 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003736 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003737 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003738 }
3739
Adam Lesinski72478f02015-06-17 15:39:43 -07003740 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3741 int statSystemTime, int statIOWaitTime, int statIrqTime,
3742 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003743 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3744 + " user=" + statUserTime + " sys=" + statSystemTime
3745 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3746 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3747 mCurStepCpuUserTime += totalUTime;
3748 mCurStepCpuSystemTime += totalSTime;
3749 mCurStepStatUserTime += statUserTime;
3750 mCurStepStatSystemTime += statSystemTime;
3751 mCurStepStatIOWaitTime += statIOWaitTime;
3752 mCurStepStatIrqTime += statIrqTime;
3753 mCurStepStatSoftIrqTime += statSoftIrqTime;
3754 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003755 }
3756
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003757 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003758 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003759 Uid u = mUidStats.get(uid);
3760 if (u != null) {
3761 u.mPids.remove(pid);
3762 }
3763 }
3764
3765 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003766 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003767 Uid u = mUidStats.get(uid);
3768 if (u != null) {
3769 Uid.Pid p = u.mPids.get(pid);
3770 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003771 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003772 }
3773 }
3774 return 0;
3775 }
3776
3777 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003778 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003779 Uid u = mUidStats.get(uid);
3780 if (u != null) {
3781 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3782 }
3783 }
3784
Dianne Hackborn287952c2010-09-22 22:34:31 -07003785 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003786 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003787 Uid u = mUidStats.get(uid);
3788 if (u != null) {
3789 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3790 }
3791 }
3792
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003793 int mSensorNesting;
3794
3795 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003796 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003797 final long elapsedRealtime = mClocks.elapsedRealtime();
3798 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003799 if (mSensorNesting == 0) {
3800 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3801 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3802 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003803 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003804 }
3805 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003806 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003807 }
3808
3809 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003810 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003811 final long elapsedRealtime = mClocks.elapsedRealtime();
3812 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003813 mSensorNesting--;
3814 if (mSensorNesting == 0) {
3815 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3816 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3817 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003818 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003819 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003820 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003821 }
3822
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003823 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003824
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003825 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003826 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003827 final long elapsedRealtime = mClocks.elapsedRealtime();
3828 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003829 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003830 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003831 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3832 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003833 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003834 }
3835 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003836 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003838
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003839 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003840 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003841 final long elapsedRealtime = mClocks.elapsedRealtime();
3842 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003843 mGpsNesting--;
3844 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003845 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003846 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3847 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003848 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003849 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003850 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003852
Jeff Browne95c3cd2014-05-02 16:59:26 -07003853 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003854 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08003855
3856 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
3857 // original 4 are mapped to one of the originals.
3858 if (state > MAX_TRACKED_SCREEN_STATE) {
3859 switch (state) {
3860 case Display.STATE_VR:
3861 state = Display.STATE_ON;
3862 break;
3863 default:
3864 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
3865 break;
3866 }
3867 }
3868
Jeff Browne95c3cd2014-05-02 16:59:26 -07003869 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003870 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003871 final int oldState = mScreenState;
3872 mScreenState = state;
3873 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3874 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003875
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003876 if (state != Display.STATE_UNKNOWN) {
3877 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08003878 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
3879 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3880 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003881 } else {
3882 Slog.wtf(TAG, "Unexpected screen state: " + state);
3883 }
3884 }
3885
Jeff Browne95c3cd2014-05-02 16:59:26 -07003886 if (state == Display.STATE_ON) {
3887 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003888 final long elapsedRealtime = mClocks.elapsedRealtime();
3889 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003890 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3891 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3892 + Integer.toHexString(mHistoryCur.states));
3893 addHistoryRecordLocked(elapsedRealtime, uptime);
3894 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3895 if (mScreenBrightnessBin >= 0) {
3896 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3897 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003898
Jeff Browne95c3cd2014-05-02 16:59:26 -07003899 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003900 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003901
Jeff Browne95c3cd2014-05-02 16:59:26 -07003902 // Fake a wake lock, so we consider the device waked as long
3903 // as the screen is on.
3904 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3905 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003906
Jeff Browne95c3cd2014-05-02 16:59:26 -07003907 // Update discharge amounts.
3908 if (mOnBatteryInternal) {
3909 updateDischargeScreenLevelsLocked(false, true);
3910 }
3911 } else if (oldState == Display.STATE_ON) {
3912 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003913 final long elapsedRealtime = mClocks.elapsedRealtime();
3914 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003915 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3916 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3917 + Integer.toHexString(mHistoryCur.states));
3918 addHistoryRecordLocked(elapsedRealtime, uptime);
3919 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3920 if (mScreenBrightnessBin >= 0) {
3921 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3922 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003923
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003924 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003925 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003926
Jeff Browne95c3cd2014-05-02 16:59:26 -07003927 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003928 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003929
Jeff Browne95c3cd2014-05-02 16:59:26 -07003930 // Update discharge amounts.
3931 if (mOnBatteryInternal) {
3932 updateDischargeScreenLevelsLocked(true, false);
3933 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003934 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003935 }
3936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003937
Dianne Hackborn617f8772009-03-31 15:04:46 -07003938 public void noteScreenBrightnessLocked(int brightness) {
3939 // Bin the brightness.
3940 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3941 if (bin < 0) bin = 0;
3942 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3943 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003944 final long elapsedRealtime = mClocks.elapsedRealtime();
3945 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003946 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3947 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003948 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3949 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003950 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003951 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003952 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003953 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003954 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003955 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003956 }
3957 mScreenBrightnessBin = bin;
3958 }
3959 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003960
Dianne Hackborn617f8772009-03-31 15:04:46 -07003961 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003962 if (mOnBatteryInternal) {
3963 uid = mapUid(uid);
3964 getUidStatsLocked(uid).noteUserActivityLocked(event);
3965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003967
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003968 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003969 final long elapsedRealtime = mClocks.elapsedRealtime();
3970 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003971 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3972 reason, reasonUid);
3973 }
3974
Jeff Browne95c3cd2014-05-02 16:59:26 -07003975 public void noteInteractiveLocked(boolean interactive) {
3976 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003977 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003978 mInteractive = interactive;
3979 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3980 if (interactive) {
3981 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3982 } else {
3983 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3984 }
3985 }
3986 }
3987
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003988 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003989 final long elapsedRealtime = mClocks.elapsedRealtime();
3990 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003991 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3992 extra, type);
3993 mNumConnectivityChange++;
3994 }
3995
Adam Lesinski5f056f62016-07-14 16:56:08 -07003996 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
3997 final long uptimeMillis, int uid) {
3998 uid = mapUid(uid);
3999 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4000 uid);
4001 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4002 }
4003
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -07004004 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004005 final long elapsedRealtime = mClocks.elapsedRealtime();
4006 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004007 if (mMobileRadioPowerState != powerState) {
4008 long realElapsedRealtimeMs;
4009 final boolean active =
4010 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4011 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4012 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004013 if (uid > 0) {
4014 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4015 }
4016
Adam Lesinski9acfd812016-04-19 18:29:50 -07004017 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004018 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4019 } else {
4020 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004021 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004022 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4023 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4024 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004025 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004026 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4027 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4028 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004029 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004030 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4031 }
4032 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4033 + Integer.toHexString(mHistoryCur.states));
4034 addHistoryRecordLocked(elapsedRealtime, uptime);
4035 mMobileRadioPowerState = powerState;
4036 if (active) {
4037 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4038 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4039 } else {
4040 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004041 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004042 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004043 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004044 }
4045 }
4046
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004047 public void notePowerSaveMode(boolean enabled) {
4048 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004049 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4050 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4051 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004052 final long elapsedRealtime = mClocks.elapsedRealtime();
4053 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004054 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004055 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004056 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4057 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004058 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004059 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004060 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004061 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4062 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004063 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004064 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004065 }
4066 addHistoryRecordLocked(elapsedRealtime, uptime);
4067 }
4068 }
4069
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004070 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004071 final long elapsedRealtime = mClocks.elapsedRealtime();
4072 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004073 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004074 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004075 // We don't go out of general idling mode until explicitly taken out of
4076 // device idle through going active or significant motion.
4077 nowIdling = true;
4078 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004079 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4080 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4081 // We don't go out of general light idling mode until explicitly taken out of
4082 // device idle through going active or significant motion.
4083 nowLightIdling = true;
4084 }
4085 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4086 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4087 activeReason, activeUid);
4088 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004089 if (mDeviceIdling != nowIdling) {
4090 mDeviceIdling = nowIdling;
4091 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4092 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4093 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004094 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004095 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4096 } else {
4097 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4098 }
4099 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004100 if (mDeviceLightIdling != nowLightIdling) {
4101 mDeviceLightIdling = nowLightIdling;
4102 if (nowLightIdling) {
4103 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004104 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004105 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004106 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004107 }
4108 if (mDeviceIdleMode != mode) {
4109 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4110 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4111 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4112 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004113 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004114 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4115 mLastIdleTimeStart = elapsedRealtime;
4116 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4117 if (lastDuration > mLongestLightIdleTime) {
4118 mLongestLightIdleTime = lastDuration;
4119 }
4120 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004121 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004122 if (lastDuration > mLongestFullIdleTime) {
4123 mLongestFullIdleTime = lastDuration;
4124 }
4125 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4126 }
4127 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4128 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004129 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004130 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4131 }
4132 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004133 }
4134 }
4135
4136 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004137 final long elapsedRealtime = mClocks.elapsedRealtime();
4138 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004139 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4140 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004141 PackageChange pc = new PackageChange();
4142 pc.mPackageName = pkgName;
4143 pc.mUpdate = true;
4144 pc.mVersionCode = versionCode;
4145 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004146 }
4147
4148 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004149 final long elapsedRealtime = mClocks.elapsedRealtime();
4150 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004151 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4152 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004153 PackageChange pc = new PackageChange();
4154 pc.mPackageName = pkgName;
4155 pc.mUpdate = true;
4156 addPackageChange(pc);
4157 }
4158
4159 private void addPackageChange(PackageChange pc) {
4160 if (mDailyPackageChanges == null) {
4161 mDailyPackageChanges = new ArrayList<>();
4162 }
4163 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004164 }
4165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 public void notePhoneOnLocked() {
4167 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004168 final long elapsedRealtime = mClocks.elapsedRealtime();
4169 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004170 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004171 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4172 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004173 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004175 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 }
4177 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 public void notePhoneOffLocked() {
4180 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004181 final long elapsedRealtime = mClocks.elapsedRealtime();
4182 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004183 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004184 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4185 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004186 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004188 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 }
4190 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004191
Dianne Hackborn3251b902014-06-20 14:40:53 -07004192 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004193 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004194 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004195 if (i == except) {
4196 continue;
4197 }
4198 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004199 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004200 }
4201 }
4202 }
4203
Dianne Hackborne4a59512010-12-07 11:08:07 -08004204 private int fixPhoneServiceState(int state, int signalBin) {
4205 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4206 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4207 // to infer that we are scanning from other data.
4208 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004209 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004210 state = ServiceState.STATE_IN_SERVICE;
4211 }
4212 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004213
Dianne Hackborne4a59512010-12-07 11:08:07 -08004214 return state;
4215 }
4216
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004217 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004218 boolean scanning = false;
4219 boolean newHistory = false;
4220
4221 mPhoneServiceStateRaw = state;
4222 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004223 mPhoneSignalStrengthBinRaw = strengthBin;
4224
Joe Onoratoabded112016-02-08 16:49:39 -08004225 final long elapsedRealtime = mClocks.elapsedRealtime();
4226 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004227
4228 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4229 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4230 // to infer that we are scanning from other data.
4231 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004232 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004233 state = ServiceState.STATE_IN_SERVICE;
4234 }
4235 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004236
4237 // If the phone is powered off, stop all timers.
4238 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004239 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004240
Dianne Hackborne4a59512010-12-07 11:08:07 -08004241 // If we are in service, make sure the correct signal string timer is running.
4242 } else if (state == ServiceState.STATE_IN_SERVICE) {
4243 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004244
4245 // If we're out of service, we are in the lowest signal strength
4246 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004247 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004248 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004249 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004250 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004251 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004252 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004253 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4254 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004255 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004256 }
4257 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004258
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004259 if (!scanning) {
4260 // If we are no longer scanning, then stop the scanning timer.
4261 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4262 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4263 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4264 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004265 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004266 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004267 }
4268 }
4269
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004270 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004271 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4272 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004273 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004274 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004275 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004276 mPhoneServiceState = state;
4277 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004278
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004279 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004280 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004281 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004282 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004283 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004284 if (strengthBin >= 0) {
4285 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004286 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004287 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004288 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4289 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004290 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004291 + Integer.toHexString(mHistoryCur.states));
4292 newHistory = true;
4293 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004294 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004295 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004296 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004297 }
4298
4299 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004300 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004301 }
4302 }
4303
4304 /**
4305 * Telephony stack updates the phone state.
4306 * @param state phone state from ServiceState.getState()
4307 */
4308 public void notePhoneStateLocked(int state, int simState) {
4309 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004310 }
4311
Wink Savillee9b06d72009-05-18 21:47:50 -07004312 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004313 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004314 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004315 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004316 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004317
Dianne Hackborn627bba72009-03-24 22:32:56 -07004318 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4319 int bin = DATA_CONNECTION_NONE;
4320 if (hasData) {
4321 switch (dataType) {
4322 case TelephonyManager.NETWORK_TYPE_EDGE:
4323 bin = DATA_CONNECTION_EDGE;
4324 break;
4325 case TelephonyManager.NETWORK_TYPE_GPRS:
4326 bin = DATA_CONNECTION_GPRS;
4327 break;
4328 case TelephonyManager.NETWORK_TYPE_UMTS:
4329 bin = DATA_CONNECTION_UMTS;
4330 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004331 case TelephonyManager.NETWORK_TYPE_CDMA:
4332 bin = DATA_CONNECTION_CDMA;
4333 break;
4334 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4335 bin = DATA_CONNECTION_EVDO_0;
4336 break;
4337 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4338 bin = DATA_CONNECTION_EVDO_A;
4339 break;
4340 case TelephonyManager.NETWORK_TYPE_1xRTT:
4341 bin = DATA_CONNECTION_1xRTT;
4342 break;
4343 case TelephonyManager.NETWORK_TYPE_HSDPA:
4344 bin = DATA_CONNECTION_HSDPA;
4345 break;
4346 case TelephonyManager.NETWORK_TYPE_HSUPA:
4347 bin = DATA_CONNECTION_HSUPA;
4348 break;
4349 case TelephonyManager.NETWORK_TYPE_HSPA:
4350 bin = DATA_CONNECTION_HSPA;
4351 break;
4352 case TelephonyManager.NETWORK_TYPE_IDEN:
4353 bin = DATA_CONNECTION_IDEN;
4354 break;
4355 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4356 bin = DATA_CONNECTION_EVDO_B;
4357 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004358 case TelephonyManager.NETWORK_TYPE_LTE:
4359 bin = DATA_CONNECTION_LTE;
4360 break;
4361 case TelephonyManager.NETWORK_TYPE_EHRPD:
4362 bin = DATA_CONNECTION_EHRPD;
4363 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004364 case TelephonyManager.NETWORK_TYPE_HSPAP:
4365 bin = DATA_CONNECTION_HSPAP;
4366 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004367 default:
4368 bin = DATA_CONNECTION_OTHER;
4369 break;
4370 }
4371 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004372 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004373 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004374 final long elapsedRealtime = mClocks.elapsedRealtime();
4375 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004376 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4377 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004378 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4379 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004380 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004381 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004382 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004383 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004384 }
4385 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004386 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004387 }
4388 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004389
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004390 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004391 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004392 final long elapsedRealtime = mClocks.elapsedRealtime();
4393 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004394 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004395 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4396 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004397 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004398 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004399 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004400 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004401 }
4402 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004403
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004404 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004405 final long elapsedRealtime = mClocks.elapsedRealtime();
4406 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004407 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004408 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004409 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4410 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004411 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004412 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004413 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004414 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004415 }
4416 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004417
4418 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004419 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004420 final long elapsedRealtime = mClocks.elapsedRealtime();
4421 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004422 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004423 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004424 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4425 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004426 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004427 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004428 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004429 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004430 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004431 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004432
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004433 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004434 if (mAudioOnNesting == 0) {
4435 return;
4436 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004437 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004438 final long elapsedRealtime = mClocks.elapsedRealtime();
4439 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004440 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004441 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004442 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4443 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004444 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004445 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004446 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004447 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004448 }
4449
4450 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004451 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004452 final long elapsedRealtime = mClocks.elapsedRealtime();
4453 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004454 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004455 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004456 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4457 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004458 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004459 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004460 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004461 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004462 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004463 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004464
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004465 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004466 if (mVideoOnNesting == 0) {
4467 return;
4468 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004469 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004470 final long elapsedRealtime = mClocks.elapsedRealtime();
4471 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004472 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004473 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004474 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4475 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004476 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004477 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004478 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004479 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004480 }
4481
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004482 public void noteResetAudioLocked() {
4483 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004484 final long elapsedRealtime = mClocks.elapsedRealtime();
4485 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004486 mAudioOnNesting = 0;
4487 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4488 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4489 + Integer.toHexString(mHistoryCur.states));
4490 addHistoryRecordLocked(elapsedRealtime, uptime);
4491 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4492 for (int i=0; i<mUidStats.size(); i++) {
4493 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4494 uid.noteResetAudioLocked(elapsedRealtime);
4495 }
4496 }
4497 }
4498
4499 public void noteResetVideoLocked() {
4500 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004501 final long elapsedRealtime = mClocks.elapsedRealtime();
4502 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004503 mAudioOnNesting = 0;
4504 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4505 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4506 + Integer.toHexString(mHistoryCur.states));
4507 addHistoryRecordLocked(elapsedRealtime, uptime);
4508 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4509 for (int i=0; i<mUidStats.size(); i++) {
4510 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4511 uid.noteResetVideoLocked(elapsedRealtime);
4512 }
4513 }
4514 }
4515
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004516 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004517 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004518 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004519 }
4520
4521 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004522 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004523 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004524 }
4525
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004526 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004527 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004528 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4529 }
4530
4531 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004532 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004533 getUidStatsLocked(uid).noteVibratorOffLocked();
4534 }
4535
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004536 public void noteFlashlightOnLocked(int uid) {
4537 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004538 final long elapsedRealtime = mClocks.elapsedRealtime();
4539 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004540 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004541 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4542 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004543 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004544 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004545 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4546 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004547 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4548 }
4549
4550 public void noteFlashlightOffLocked(int uid) {
4551 if (mFlashlightOnNesting == 0) {
4552 return;
4553 }
4554 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004555 final long elapsedRealtime = mClocks.elapsedRealtime();
4556 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004557 if (--mFlashlightOnNesting == 0) {
4558 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4559 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4560 + Integer.toHexString(mHistoryCur.states2));
4561 addHistoryRecordLocked(elapsedRealtime, uptime);
4562 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4563 }
4564 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4565 }
4566
4567 public void noteCameraOnLocked(int uid) {
4568 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004569 final long elapsedRealtime = mClocks.elapsedRealtime();
4570 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004571 if (mCameraOnNesting++ == 0) {
4572 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4573 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4574 + Integer.toHexString(mHistoryCur.states2));
4575 addHistoryRecordLocked(elapsedRealtime, uptime);
4576 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4577 }
4578 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4579 }
4580
4581 public void noteCameraOffLocked(int uid) {
4582 if (mCameraOnNesting == 0) {
4583 return;
4584 }
4585 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004586 final long elapsedRealtime = mClocks.elapsedRealtime();
4587 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004588 if (--mCameraOnNesting == 0) {
4589 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4590 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4591 + Integer.toHexString(mHistoryCur.states2));
4592 addHistoryRecordLocked(elapsedRealtime, uptime);
4593 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4594 }
4595 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4596 }
4597
4598 public void noteResetCameraLocked() {
4599 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004600 final long elapsedRealtime = mClocks.elapsedRealtime();
4601 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004602 mCameraOnNesting = 0;
4603 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4604 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4605 + Integer.toHexString(mHistoryCur.states2));
4606 addHistoryRecordLocked(elapsedRealtime, uptime);
4607 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4608 for (int i=0; i<mUidStats.size(); i++) {
4609 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4610 uid.noteResetCameraLocked(elapsedRealtime);
4611 }
4612 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004613 }
4614
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004615 public void noteResetFlashlightLocked() {
4616 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004617 final long elapsedRealtime = mClocks.elapsedRealtime();
4618 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004619 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004620 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4621 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004622 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004623 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004624 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4625 for (int i=0; i<mUidStats.size(); i++) {
4626 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4627 uid.noteResetFlashlightLocked(elapsedRealtime);
4628 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004629 }
4630 }
4631
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004632 private void noteBluetoothScanStartedLocked(int uid) {
4633 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004634 final long elapsedRealtime = mClocks.elapsedRealtime();
4635 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004636 if (mBluetoothScanNesting == 0) {
4637 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4638 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4639 + Integer.toHexString(mHistoryCur.states2));
4640 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004641 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004642 }
4643 mBluetoothScanNesting++;
4644 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4645 }
4646
4647 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4648 final int N = ws.size();
4649 for (int i = 0; i < N; i++) {
4650 noteBluetoothScanStartedLocked(ws.get(i));
4651 }
4652 }
4653
4654 private void noteBluetoothScanStoppedLocked(int uid) {
4655 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004656 final long elapsedRealtime = mClocks.elapsedRealtime();
4657 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004658 mBluetoothScanNesting--;
4659 if (mBluetoothScanNesting == 0) {
4660 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4661 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4662 + Integer.toHexString(mHistoryCur.states2));
4663 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004664 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004665 }
4666 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4667 }
4668
4669 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4670 final int N = ws.size();
4671 for (int i = 0; i < N; i++) {
4672 noteBluetoothScanStoppedLocked(ws.get(i));
4673 }
4674 }
4675
4676 public void noteResetBluetoothScanLocked() {
4677 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004678 final long elapsedRealtime = mClocks.elapsedRealtime();
4679 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004680 mBluetoothScanNesting = 0;
4681 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4682 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4683 + Integer.toHexString(mHistoryCur.states2));
4684 addHistoryRecordLocked(elapsedRealtime, uptime);
4685 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4686 for (int i=0; i<mUidStats.size(); i++) {
4687 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4688 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4689 }
4690 }
4691 }
4692
Adam Lesinski5f056f62016-07-14 16:56:08 -07004693 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4694 final long uptimeMillis, int uid) {
4695 uid = mapUid(uid);
4696 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4697 uid);
4698 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4699 }
4700
4701 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004702 final long elapsedRealtime = mClocks.elapsedRealtime();
4703 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004704 if (mWifiRadioPowerState != powerState) {
4705 final boolean active =
4706 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4707 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4708 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004709 if (uid > 0) {
4710 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4711 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004712 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4713 } else {
4714 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4715 }
4716 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4717 + Integer.toHexString(mHistoryCur.states));
4718 addHistoryRecordLocked(elapsedRealtime, uptime);
4719 mWifiRadioPowerState = powerState;
4720 }
4721 }
4722
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004723 public void noteWifiRunningLocked(WorkSource ws) {
4724 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004725 final long elapsedRealtime = mClocks.elapsedRealtime();
4726 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004727 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004728 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4729 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004730 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004731 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004732 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004733 int N = ws.size();
4734 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004735 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004736 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004737 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004738 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004739 } else {
4740 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004741 }
4742 }
4743
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004744 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4745 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004746 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004747 int N = oldWs.size();
4748 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004749 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004750 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004751 }
4752 N = newWs.size();
4753 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004754 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004755 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004756 }
4757 } else {
4758 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4759 }
4760 }
4761
4762 public void noteWifiStoppedLocked(WorkSource ws) {
4763 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004764 final long elapsedRealtime = mClocks.elapsedRealtime();
4765 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004766 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004767 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4768 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004769 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004770 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004771 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004772 int N = ws.size();
4773 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004774 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004775 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004776 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004777 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004778 } else {
4779 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004780 }
4781 }
4782
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004783 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4784 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4785 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004786 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004787 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004788 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004789 }
4790 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004791 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004792 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004793 }
4794 }
4795
Dianne Hackborn3251b902014-06-20 14:40:53 -07004796 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4797 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4798 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004799 final long elapsedRealtime = mClocks.elapsedRealtime();
4800 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004801 if (mWifiSupplState >= 0) {
4802 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4803 }
4804 mWifiSupplState = supplState;
4805 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4806 mHistoryCur.states2 =
4807 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4808 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4809 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4810 + Integer.toHexString(mHistoryCur.states2));
4811 addHistoryRecordLocked(elapsedRealtime, uptime);
4812 }
4813 }
4814
4815 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004816 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004817 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4818 if (i == except) {
4819 continue;
4820 }
4821 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4822 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4823 }
4824 }
4825 }
4826
4827 public void noteWifiRssiChangedLocked(int newRssi) {
4828 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4829 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4830 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004831 final long elapsedRealtime = mClocks.elapsedRealtime();
4832 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004833 if (mWifiSignalStrengthBin >= 0) {
4834 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4835 elapsedRealtime);
4836 }
4837 if (strengthBin >= 0) {
4838 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4839 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4840 }
4841 mHistoryCur.states2 =
4842 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4843 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4844 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4845 + Integer.toHexString(mHistoryCur.states2));
4846 addHistoryRecordLocked(elapsedRealtime, uptime);
4847 } else {
4848 stopAllWifiSignalStrengthTimersLocked(-1);
4849 }
4850 mWifiSignalStrengthBin = strengthBin;
4851 }
4852 }
4853
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004854 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004855
The Android Open Source Project10592532009-03-18 17:39:46 -07004856 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004857 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004858 final long elapsedRealtime = mClocks.elapsedRealtime();
4859 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004860 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004861 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004862 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4863 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004864 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004865 }
4866 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004867 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004868 }
4869
4870 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004871 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004872 final long elapsedRealtime = mClocks.elapsedRealtime();
4873 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004874 mWifiFullLockNesting--;
4875 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004876 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004877 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4878 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004879 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004880 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004881 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004882 }
4883
Nick Pelly6ccaa542012-06-15 15:22:47 -07004884 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004885
Nick Pelly6ccaa542012-06-15 15:22:47 -07004886 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004887 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004888 final long elapsedRealtime = mClocks.elapsedRealtime();
4889 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004890 if (mWifiScanNesting == 0) {
4891 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4892 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004893 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004894 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004895 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004896 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004897 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004898 }
4899
Nick Pelly6ccaa542012-06-15 15:22:47 -07004900 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004901 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004902 final long elapsedRealtime = mClocks.elapsedRealtime();
4903 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004904 mWifiScanNesting--;
4905 if (mWifiScanNesting == 0) {
4906 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4907 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004908 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004909 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004910 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004911 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004912 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004913
Robert Greenwalta029ea12013-09-25 16:38:12 -07004914 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004915 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004916 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004917 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004918 }
4919
4920 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004921 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004922 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004923 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004924 }
4925
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004926 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004927
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004928 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004929 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004930 final long elapsedRealtime = mClocks.elapsedRealtime();
4931 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004932 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004933 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004934 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4935 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004936 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004937 }
4938 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004939 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004940 }
4941
4942 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004943 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004944 final long elapsedRealtime = mClocks.elapsedRealtime();
4945 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004946 mWifiMulticastNesting--;
4947 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004948 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004949 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4950 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004951 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004952 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004953 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004954 }
4955
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004956 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4957 int N = ws.size();
4958 for (int i=0; i<N; i++) {
4959 noteFullWifiLockAcquiredLocked(ws.get(i));
4960 }
4961 }
4962
4963 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4964 int N = ws.size();
4965 for (int i=0; i<N; i++) {
4966 noteFullWifiLockReleasedLocked(ws.get(i));
4967 }
4968 }
4969
Nick Pelly6ccaa542012-06-15 15:22:47 -07004970 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004971 int N = ws.size();
4972 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004973 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004974 }
4975 }
4976
Nick Pelly6ccaa542012-06-15 15:22:47 -07004977 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004978 int N = ws.size();
4979 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004980 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004981 }
4982 }
4983
Robert Greenwalta029ea12013-09-25 16:38:12 -07004984 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4985 int N = ws.size();
4986 for (int i=0; i<N; i++) {
4987 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4988 }
4989 }
4990
4991 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4992 int N = ws.size();
4993 for (int i=0; i<N; i++) {
4994 noteWifiBatchedScanStoppedLocked(ws.get(i));
4995 }
4996 }
4997
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004998 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4999 int N = ws.size();
5000 for (int i=0; i<N; i++) {
5001 noteWifiMulticastEnabledLocked(ws.get(i));
5002 }
5003 }
5004
5005 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5006 int N = ws.size();
5007 for (int i=0; i<N; i++) {
5008 noteWifiMulticastDisabledLocked(ws.get(i));
5009 }
5010 }
5011
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005012 private static String[] includeInStringArray(String[] array, String str) {
5013 if (ArrayUtils.indexOf(array, str) >= 0) {
5014 return array;
5015 }
5016 String[] newArray = new String[array.length+1];
5017 System.arraycopy(array, 0, newArray, 0, array.length);
5018 newArray[array.length] = str;
5019 return newArray;
5020 }
5021
5022 private static String[] excludeFromStringArray(String[] array, String str) {
5023 int index = ArrayUtils.indexOf(array, str);
5024 if (index >= 0) {
5025 String[] newArray = new String[array.length-1];
5026 if (index > 0) {
5027 System.arraycopy(array, 0, newArray, 0, index);
5028 }
5029 if (index < array.length-1) {
5030 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5031 }
5032 return newArray;
5033 }
5034 return array;
5035 }
5036
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005037 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005038 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005039 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005040 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
5041 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005042 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005043 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
5044 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005045 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005046 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005047 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5048 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005049 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005050 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5051 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005052 }
5053 }
5054
5055 public void noteNetworkStatsEnabledLocked() {
5056 // During device boot, qtaguid isn't enabled until after the inital
5057 // loading of battery stats. Now that they're enabled, take our initial
5058 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08005059 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07005060 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005061 }
5062
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005063 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5064 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005066
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005067 @Override public int getScreenOnCount(int which) {
5068 return mScreenOnTimer.getCountLocked(which);
5069 }
5070
Dianne Hackborn617f8772009-03-31 15:04:46 -07005071 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005072 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005073 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005074 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005075 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005076
Jeff Browne95c3cd2014-05-02 16:59:26 -07005077 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5078 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005079 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005080
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005081 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5082 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005083 }
5084
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005085 @Override public int getPowerSaveModeEnabledCount(int which) {
5086 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5087 }
5088
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005089 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5090 int which) {
5091 switch (mode) {
5092 case DEVICE_IDLE_MODE_LIGHT:
5093 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005094 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005095 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5096 }
5097 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005098 }
5099
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005100 @Override public int getDeviceIdleModeCount(int mode, int which) {
5101 switch (mode) {
5102 case DEVICE_IDLE_MODE_LIGHT:
5103 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005104 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005105 return mDeviceIdleModeFullTimer.getCountLocked(which);
5106 }
5107 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005108 }
5109
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005110 @Override public long getLongestDeviceIdleModeTime(int mode) {
5111 switch (mode) {
5112 case DEVICE_IDLE_MODE_LIGHT:
5113 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005114 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005115 return mLongestFullIdleTime;
5116 }
5117 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005118 }
5119
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005120 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5121 switch (mode) {
5122 case DEVICE_IDLE_MODE_LIGHT:
5123 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005124 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005125 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5126 }
5127 return 0;
5128 }
5129
5130 @Override public int getDeviceIdlingCount(int mode, int which) {
5131 switch (mode) {
5132 case DEVICE_IDLE_MODE_LIGHT:
5133 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005134 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005135 return mDeviceIdlingTimer.getCountLocked(which);
5136 }
5137 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005138 }
5139
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005140 @Override public int getNumConnectivityChange(int which) {
5141 int val = mNumConnectivityChange;
5142 if (which == STATS_CURRENT) {
5143 val -= mLoadedNumConnectivityChange;
5144 } else if (which == STATS_SINCE_UNPLUGGED) {
5145 val -= mUnpluggedNumConnectivityChange;
5146 }
5147 return val;
5148 }
5149
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005150 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5151 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005153
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005154 @Override public int getPhoneOnCount(int which) {
5155 return mPhoneOnTimer.getCountLocked(which);
5156 }
5157
Dianne Hackborn627bba72009-03-24 22:32:56 -07005158 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005159 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005160 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005161 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005162 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005163
5164 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005165 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005166 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005167 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005168 }
5169
Catherine Liufb900812012-07-17 14:12:56 -05005170 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5171 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005172 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005173
Dianne Hackborn627bba72009-03-24 22:32:56 -07005174 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005175 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005176 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005177 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005178 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005179
Dianne Hackborn617f8772009-03-31 15:04:46 -07005180 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005181 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005182 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005183
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005184 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5185 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005186 }
5187
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005188 @Override public int getMobileRadioActiveCount(int which) {
5189 return mMobileRadioActiveTimer.getCountLocked(which);
5190 }
5191
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005192 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5193 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5194 }
5195
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005196 @Override public long getMobileRadioActiveUnknownTime(int which) {
5197 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5198 }
5199
5200 @Override public int getMobileRadioActiveUnknownCount(int which) {
5201 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5202 }
5203
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005204 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5205 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005206 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005207
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005208 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5209 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005210 }
5211
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005212 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005213 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005214 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005215 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005216 }
5217
5218 @Override public int getWifiStateCount(int wifiState, int which) {
5219 return mWifiStateTimer[wifiState].getCountLocked(which);
5220 }
5221
Dianne Hackborn3251b902014-06-20 14:40:53 -07005222 @Override public long getWifiSupplStateTime(int state,
5223 long elapsedRealtimeUs, int which) {
5224 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5225 elapsedRealtimeUs, which);
5226 }
5227
5228 @Override public int getWifiSupplStateCount(int state, int which) {
5229 return mWifiSupplStateTimer[state].getCountLocked(which);
5230 }
5231
5232 @Override public long getWifiSignalStrengthTime(int strengthBin,
5233 long elapsedRealtimeUs, int which) {
5234 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5235 elapsedRealtimeUs, which);
5236 }
5237
5238 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5239 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5240 }
5241
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005242 @Override
5243 public ControllerActivityCounter getBluetoothControllerActivity() {
5244 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005245 }
5246
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005247 @Override
5248 public ControllerActivityCounter getWifiControllerActivity() {
5249 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005250 }
5251
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005252 @Override
5253 public ControllerActivityCounter getModemControllerActivity() {
5254 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005255 }
5256
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005257 @Override
5258 public boolean hasBluetoothActivityReporting() {
5259 return mHasBluetoothReporting;
5260 }
5261
5262 @Override
5263 public boolean hasWifiActivityReporting() {
5264 return mHasWifiReporting;
5265 }
5266
5267 @Override
5268 public boolean hasModemActivityReporting() {
5269 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005270 }
5271
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005272 @Override
5273 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005274 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5275 }
5276
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005277 @Override
5278 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005279 return mFlashlightOnTimer.getCountLocked(which);
5280 }
5281
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005282 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005283 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5284 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5285 }
5286
5287 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005288 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5289 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5290 }
5291
5292 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005293 public long getNetworkActivityBytes(int type, int which) {
5294 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5295 return mNetworkByteActivityCounters[type].getCountLocked(which);
5296 } else {
5297 return 0;
5298 }
5299 }
5300
5301 @Override
5302 public long getNetworkActivityPackets(int type, int which) {
5303 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5304 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005305 } else {
5306 return 0;
5307 }
5308 }
5309
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005310 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005311 final long currentTime = System.currentTimeMillis();
5312 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005313 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5314 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005315 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005316 return mStartClockTime;
5317 }
5318
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005319 @Override public String getStartPlatformVersion() {
5320 return mStartPlatformVersion;
5321 }
5322
5323 @Override public String getEndPlatformVersion() {
5324 return mEndPlatformVersion;
5325 }
5326
5327 @Override public int getParcelVersion() {
5328 return VERSION;
5329 }
5330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 @Override public boolean getIsOnBattery() {
5332 return mOnBattery;
5333 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5336 return mUidStats;
5337 }
5338
Adam Lesinski5f056f62016-07-14 16:56:08 -07005339 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5340 if (timer != null) {
5341 timer.detach();
5342 }
5343 }
5344
5345 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5346 boolean detachIfReset) {
5347 if (timer != null) {
5348 return timer.reset(detachIfReset);
5349 }
5350 return true;
5351 }
5352
Bookatz867c0d72017-03-07 18:23:42 -08005353 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5354 if (timer != null) {
5355 return timer.reset(detachIfReset);
5356 }
5357 return true;
5358 }
5359
Adam Lesinski5f056f62016-07-14 16:56:08 -07005360 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5361 if (counter != null) {
5362 counter.detach();
5363 }
5364 }
5365
5366 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5367 boolean detachIfReset) {
5368 if (counter != null) {
5369 counter.reset(detachIfReset);
5370 }
5371 }
5372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 /**
5374 * The statistics associated with a particular uid.
5375 */
Joe Onoratoabded112016-02-08 16:49:39 -08005376 public static class Uid extends BatteryStats.Uid {
5377 /**
5378 * BatteryStatsImpl that we are associated with.
5379 */
5380 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005383
Bookatz867c0d72017-03-07 18:23:42 -08005384 /** TimeBase for when uid is in background and device is on battery. */
5385 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5386 public final TimeBase mOnBatteryBackgroundTimeBase;
5387
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005388 boolean mWifiRunning;
5389 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005390
The Android Open Source Project10592532009-03-18 17:39:46 -07005391 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005392 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005393
Nick Pelly6ccaa542012-06-15 15:22:47 -07005394 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005395 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005396
Dianne Hackborn61659e52014-07-09 16:13:01 -07005397 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005398 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5399 StopwatchTimer[] mWifiBatchedScanTimer;
5400
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005401 boolean mWifiMulticastEnabled;
5402 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005403
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005404 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005405 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005406 StopwatchTimer mFlashlightTurnedOnTimer;
5407 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005408 StopwatchTimer mForegroundActivityTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005409 DualTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005410
Dianne Hackborna8d10942015-11-19 17:55:19 -08005411 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005412 StopwatchTimer[] mProcessStateTimer;
5413
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005414 BatchTimer mVibratorOnTimer;
5415
Dianne Hackborn617f8772009-03-31 15:04:46 -07005416 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005417
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005418 LongSamplingCounter[] mNetworkByteActivityCounters;
5419 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005420 LongSamplingCounter mMobileRadioActiveTime;
5421 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005424 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5425 */
5426 private LongSamplingCounter mMobileRadioApWakeupCount;
5427
5428 /**
5429 * How many times this UID woke up the Application Processor due to a Wifi packet.
5430 */
5431 private LongSamplingCounter mWifiRadioApWakeupCount;
5432
5433 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005434 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005435 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005436 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005437 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005438
5439 /**
5440 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005441 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005442 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005443 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5444
5445 /**
5446 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5447 * Can be null if the UID has had no such activity.
5448 */
5449 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005450
5451 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005452 * The CPU times we had at the last history details update.
5453 */
5454 long mLastStepUserTime;
5455 long mLastStepSystemTime;
5456 long mCurStepUserTime;
5457 long mCurStepSystemTime;
5458
Joe Onoratoabded112016-02-08 16:49:39 -08005459 LongSamplingCounter mUserCpuTime;
5460 LongSamplingCounter mSystemCpuTime;
5461 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07005462 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005463
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 * The statistics we have collected for this uid's wake locks.
5466 */
Joe Onoratoabded112016-02-08 16:49:39 -08005467 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468
5469 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005470 * The statistics we have collected for this uid's syncs.
5471 */
Joe Onoratoabded112016-02-08 16:49:39 -08005472 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005473
5474 /**
5475 * The statistics we have collected for this uid's jobs.
5476 */
Joe Onoratoabded112016-02-08 16:49:39 -08005477 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005478
5479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 * The statistics we have collected for this uid's sensor activations.
5481 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005482 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483
5484 /**
5485 * The statistics we have collected for this uid's processes.
5486 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005487 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488
5489 /**
5490 * The statistics we have collected for this uid's processes.
5491 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005492 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005493
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005494 /**
5495 * The transient wake stats we have collected for this uid's pids.
5496 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005497 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005498
Joe Onoratoabded112016-02-08 16:49:39 -08005499 public Uid(BatteryStatsImpl bsi, int uid) {
5500 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005502
Bookatz867c0d72017-03-07 18:23:42 -08005503 mOnBatteryBackgroundTimeBase = new TimeBase();
5504 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5505 mBsi.mClocks.elapsedRealtime() * 1000);
5506
Joe Onoratoabded112016-02-08 16:49:39 -08005507 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5508 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5509 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5510
Dianne Hackborn657153b2016-07-29 14:54:14 -07005511 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005512 @Override public Wakelock instantiateObject() {
5513 return new Wakelock(mBsi, Uid.this);
5514 }
5515 };
Dianne Hackborn657153b2016-07-29 14:54:14 -07005516 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005517 @Override public StopwatchTimer instantiateObject() {
5518 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
5519 mBsi.mOnBatteryTimeBase);
5520 }
5521 };
Dianne Hackborn657153b2016-07-29 14:54:14 -07005522 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005523 @Override public StopwatchTimer instantiateObject() {
5524 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5525 mBsi.mOnBatteryTimeBase);
5526 }
5527 };
5528
5529 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5530 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5531 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5532 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005533 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5534 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005535 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005536 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5537 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005538 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 }
5540
5541 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005542 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005543 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 }
5545
5546 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005547 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005548 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005549 }
5550
5551 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005552 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005553 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005554 }
5555
5556 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005557 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 return mSensorStats;
5559 }
5560
5561 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005562 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 return mProcessStats;
5564 }
5565
5566 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005567 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 return mPackageStats;
5569 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005570
5571 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 public int getUid() {
5573 return mUid;
5574 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005575
5576 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005577 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005578 if (!mWifiRunning) {
5579 mWifiRunning = true;
5580 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005581 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5582 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005583 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005584 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005585 }
5586 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005587
Dianne Hackborn617f8772009-03-31 15:04:46 -07005588 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005589 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005590 if (mWifiRunning) {
5591 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005592 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005593 }
5594 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005595
Dianne Hackborn617f8772009-03-31 15:04:46 -07005596 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005597 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005598 if (!mFullWifiLockOut) {
5599 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005600 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005601 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5602 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005603 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005604 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005605 }
5606 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005607
The Android Open Source Project10592532009-03-18 17:39:46 -07005608 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005609 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005610 if (mFullWifiLockOut) {
5611 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005612 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005613 }
5614 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005615
The Android Open Source Project10592532009-03-18 17:39:46 -07005616 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005617 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005618 if (!mWifiScanStarted) {
5619 mWifiScanStarted = true;
5620 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005621 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5622 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
5623 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005624 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005625 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005626 }
5627 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005628
The Android Open Source Project10592532009-03-18 17:39:46 -07005629 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005630 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005631 if (mWifiScanStarted) {
5632 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005633 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005634 }
5635 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005636
5637 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005638 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005639 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005640 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005641 csph = csph >> 3;
5642 bin++;
5643 }
5644
5645 if (mWifiBatchedScanBinStarted == bin) return;
5646
5647 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5648 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005649 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005650 }
5651 mWifiBatchedScanBinStarted = bin;
5652 if (mWifiBatchedScanTimer[bin] == null) {
5653 makeWifiBatchedScanBin(bin, null);
5654 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005655 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005656 }
5657
5658 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005659 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005660 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5661 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005662 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005663 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5664 }
5665 }
5666
5667 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005668 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005669 if (!mWifiMulticastEnabled) {
5670 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005671 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005672 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5673 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005674 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005675 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005676 }
5677 }
5678
5679 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005680 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005681 if (mWifiMulticastEnabled) {
5682 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005683 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005684 }
5685 }
5686
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005687 @Override
5688 public ControllerActivityCounter getWifiControllerActivity() {
5689 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005690 }
5691
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005692 @Override
5693 public ControllerActivityCounter getBluetoothControllerActivity() {
5694 return mBluetoothControllerActivity;
5695 }
5696
5697 @Override
5698 public ControllerActivityCounter getModemControllerActivity() {
5699 return mModemControllerActivity;
5700 }
5701
5702 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5703 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005704 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005705 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005706 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005707 return mWifiControllerActivity;
5708 }
5709
5710 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5711 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005712 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005713 NUM_BT_TX_LEVELS);
5714 }
5715 return mBluetoothControllerActivity;
5716 }
5717
5718 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5719 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005720 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005721 ModemActivityInfo.TX_POWER_LEVELS);
5722 }
5723 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005724 }
5725
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005726 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5727 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005728 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5729 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005730 }
5731 return mAudioTurnedOnTimer;
5732 }
5733
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005734 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005735 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5736 }
5737
5738 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5739 if (mAudioTurnedOnTimer != null) {
5740 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005741 }
5742 }
5743
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005744 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5745 if (mAudioTurnedOnTimer != null) {
5746 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005747 }
5748 }
5749
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005750 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5751 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005752 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5753 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005754 }
5755 return mVideoTurnedOnTimer;
5756 }
5757
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005758 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005759 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5760 }
5761
5762 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5763 if (mVideoTurnedOnTimer != null) {
5764 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005765 }
5766 }
5767
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005768 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5769 if (mVideoTurnedOnTimer != null) {
5770 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005771 }
5772 }
5773
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005774 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5775 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005776 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5777 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005778 }
5779 return mFlashlightTurnedOnTimer;
5780 }
5781
5782 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5783 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5784 }
5785
5786 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5787 if (mFlashlightTurnedOnTimer != null) {
5788 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5789 }
5790 }
5791
5792 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5793 if (mFlashlightTurnedOnTimer != null) {
5794 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5795 }
5796 }
5797
5798 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5799 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005800 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5801 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005802 }
5803 return mCameraTurnedOnTimer;
5804 }
5805
5806 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5807 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5808 }
5809
5810 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5811 if (mCameraTurnedOnTimer != null) {
5812 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5813 }
5814 }
5815
5816 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5817 if (mCameraTurnedOnTimer != null) {
5818 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5819 }
5820 }
5821
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005822 public StopwatchTimer createForegroundActivityTimerLocked() {
5823 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005824 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5825 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005826 }
5827 return mForegroundActivityTimer;
5828 }
5829
Bookatz867c0d72017-03-07 18:23:42 -08005830 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005831 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005832 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5833 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
5834 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005835 }
5836 return mBluetoothScanTimer;
5837 }
5838
5839 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5840 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5841 }
5842
5843 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5844 if (mBluetoothScanTimer != null) {
5845 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5846 }
5847 }
5848
5849 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5850 if (mBluetoothScanTimer != null) {
5851 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5852 }
5853 }
5854
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005855 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005856 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005857 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005858 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005859 }
5860
5861 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005862 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005863 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005864 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005865 }
5866 }
5867
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005868 public BatchTimer createVibratorOnTimerLocked() {
5869 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005870 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5871 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005872 }
5873 return mVibratorOnTimer;
5874 }
5875
5876 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005877 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005878 }
5879
5880 public void noteVibratorOffLocked() {
5881 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005882 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005883 }
5884 }
5885
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005886 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005887 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005888 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005889 return 0;
5890 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005891 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005892 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005893
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005894 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005895 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005896 if (mFullWifiLockTimer == null) {
5897 return 0;
5898 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005899 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005900 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005901
5902 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005903 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005904 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005905 return 0;
5906 }
Bookatz867c0d72017-03-07 18:23:42 -08005907 return mWifiScanTimer.getMainTimer().getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005908 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005909
5910 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005911 public int getWifiScanCount(int which) {
5912 if (mWifiScanTimer == null) {
5913 return 0;
5914 }
Bookatz867c0d72017-03-07 18:23:42 -08005915 return mWifiScanTimer.getMainTimer().getCountLocked(which);
5916 }
5917
5918 @Override
5919 public int getWifiScanBackgroundCount(int which) {
5920 if (mWifiScanTimer == null) {
5921 return 0;
5922 }
5923 return mWifiScanTimer.getSubTimer().getCountLocked(which);
5924 }
5925
5926 @Override
5927 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
5928 if (mWifiScanTimer == null) {
5929 return 0;
5930 }
5931 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
5932 return mWifiScanTimer.getMainTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
5933 }
5934
5935 @Override
5936 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
5937 if (mWifiScanTimer == null) {
5938 return 0;
5939 }
5940 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
5941 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07005942 }
5943
5944 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005945 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005946 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5947 if (mWifiBatchedScanTimer[csphBin] == null) {
5948 return 0;
5949 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005950 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005951 }
5952
5953 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005954 public int getWifiBatchedScanCount(int csphBin, int which) {
5955 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5956 if (mWifiBatchedScanTimer[csphBin] == null) {
5957 return 0;
5958 }
5959 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5960 }
5961
5962 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005963 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005964 if (mWifiMulticastTimer == null) {
5965 return 0;
5966 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005967 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005968 }
5969
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005970 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005971 public Timer getAudioTurnedOnTimer() {
5972 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005973 }
5974
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005975 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005976 public Timer getVideoTurnedOnTimer() {
5977 return mVideoTurnedOnTimer;
5978 }
5979
5980 @Override
5981 public Timer getFlashlightTurnedOnTimer() {
5982 return mFlashlightTurnedOnTimer;
5983 }
5984
5985 @Override
5986 public Timer getCameraTurnedOnTimer() {
5987 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005988 }
5989
Dianne Hackborn617f8772009-03-31 15:04:46 -07005990 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005991 public Timer getForegroundActivityTimer() {
5992 return mForegroundActivityTimer;
5993 }
5994
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005995 @Override
5996 public Timer getBluetoothScanTimer() {
Bookatz867c0d72017-03-07 18:23:42 -08005997 if (mBluetoothScanTimer == null) {
5998 return null;
5999 }
6000 return mBluetoothScanTimer.getMainTimer();
6001 }
6002
6003 @Override
6004 public Timer getBluetoothScanBackgroundTimer() {
6005 if (mBluetoothScanTimer == null) {
6006 return null;
6007 }
6008 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006009 }
6010
Dianne Hackborn61659e52014-07-09 16:13:01 -07006011 void makeProcessState(int i, Parcel in) {
6012 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6013
6014 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006015 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6016 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006017 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006018 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6019 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006020 }
6021 }
6022
6023 @Override
6024 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6025 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6026 if (mProcessStateTimer[state] == null) {
6027 return 0;
6028 }
6029 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6030 }
6031
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006032 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006033 public Timer getProcessStateTimer(int state) {
6034 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6035 return mProcessStateTimer[state];
6036 }
6037
6038 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006039 public Timer getVibratorOnTimer() {
6040 return mVibratorOnTimer;
6041 }
6042
6043 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006044 public void noteUserActivityLocked(int type) {
6045 if (mUserActivityCounters == null) {
6046 initUserActivityLocked();
6047 }
Jeff Browndf693de2012-07-27 12:03:38 -07006048 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6049 mUserActivityCounters[type].stepAtomic();
6050 } else {
6051 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6052 new Throwable());
6053 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006054 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006055
Dianne Hackborn617f8772009-03-31 15:04:46 -07006056 @Override
6057 public boolean hasUserActivity() {
6058 return mUserActivityCounters != null;
6059 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006060
Dianne Hackborn617f8772009-03-31 15:04:46 -07006061 @Override
6062 public int getUserActivityCount(int type, int which) {
6063 if (mUserActivityCounters == null) {
6064 return 0;
6065 }
Evan Millarc64edde2009-04-18 12:26:32 -07006066 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006067 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006068
Robert Greenwalta029ea12013-09-25 16:38:12 -07006069 void makeWifiBatchedScanBin(int i, Parcel in) {
6070 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6071
Joe Onoratoabded112016-02-08 16:49:39 -08006072 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006073 if (collected == null) {
6074 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006075 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006076 }
6077 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006078 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6079 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006080 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006081 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6082 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006083 }
6084 }
6085
6086
Dianne Hackborn617f8772009-03-31 15:04:46 -07006087 void initUserActivityLocked() {
6088 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6089 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006090 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006091 }
6092 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006093
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006094 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6095 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006096 initNetworkActivityLocked();
6097 }
6098 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006099 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6100 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006101 } else {
6102 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6103 new Throwable());
6104 }
6105 }
6106
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006107 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6108 if (mNetworkByteActivityCounters == null) {
6109 initNetworkActivityLocked();
6110 }
6111 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6112 mMobileRadioActiveCount.addCountLocked(1);
6113 }
6114
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006115 @Override
6116 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006117 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006118 }
6119
6120 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006121 public long getNetworkActivityBytes(int type, int which) {
6122 if (mNetworkByteActivityCounters != null && type >= 0
6123 && type < mNetworkByteActivityCounters.length) {
6124 return mNetworkByteActivityCounters[type].getCountLocked(which);
6125 } else {
6126 return 0;
6127 }
6128 }
6129
6130 @Override
6131 public long getNetworkActivityPackets(int type, int which) {
6132 if (mNetworkPacketActivityCounters != null && type >= 0
6133 && type < mNetworkPacketActivityCounters.length) {
6134 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006135 } else {
6136 return 0;
6137 }
6138 }
6139
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006140 @Override
6141 public long getMobileRadioActiveTime(int which) {
6142 return mMobileRadioActiveTime != null
6143 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6144 }
6145
6146 @Override
6147 public int getMobileRadioActiveCount(int which) {
6148 return mMobileRadioActiveCount != null
6149 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6150 }
6151
Adam Lesinskie08af192015-03-25 16:42:59 -07006152 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006153 public long getUserCpuTimeUs(int which) {
6154 return mUserCpuTime.getCountLocked(which);
6155 }
6156
6157 @Override
6158 public long getSystemCpuTimeUs(int which) {
6159 return mSystemCpuTime.getCountLocked(which);
6160 }
6161
6162 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006163 public long getCpuPowerMaUs(int which) {
6164 return mCpuPower.getCountLocked(which);
6165 }
6166
6167 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006168 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
6169 if (mCpuClusterSpeed != null) {
6170 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
6171 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
6172 if (cpuSpeeds != null) {
6173 if (step >= 0 && step < cpuSpeeds.length) {
6174 final LongSamplingCounter c = cpuSpeeds[step];
6175 if (c != null) {
6176 return c.getCountLocked(which);
6177 }
6178 }
6179 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006180 }
6181 }
6182 return 0;
6183 }
6184
Adam Lesinski5f056f62016-07-14 16:56:08 -07006185 public void noteMobileRadioApWakeupLocked() {
6186 if (mMobileRadioApWakeupCount == null) {
6187 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6188 }
6189 mMobileRadioApWakeupCount.addCountLocked(1);
6190 }
6191
6192 @Override
6193 public long getMobileRadioApWakeupCount(int which) {
6194 if (mMobileRadioApWakeupCount != null) {
6195 return mMobileRadioApWakeupCount.getCountLocked(which);
6196 }
6197 return 0;
6198 }
6199
6200 public void noteWifiRadioApWakeupLocked() {
6201 if (mWifiRadioApWakeupCount == null) {
6202 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6203 }
6204 mWifiRadioApWakeupCount.addCountLocked(1);
6205 }
6206
6207 @Override
6208 public long getWifiRadioApWakeupCount(int which) {
6209 if (mWifiRadioApWakeupCount != null) {
6210 return mWifiRadioApWakeupCount.getCountLocked(which);
6211 }
6212 return 0;
6213 }
6214
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006215 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006216 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6217 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006218 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006219 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6220 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006221 }
Joe Onoratoabded112016-02-08 16:49:39 -08006222 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6223 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006224 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006225
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006226 /**
6227 * Clear all stats for this uid. Returns true if the uid is completely
6228 * inactive so can be dropped.
6229 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006230 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
6231 public boolean reset() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006232 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006233
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006234 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006235 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006236 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006237 }
6238 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006239 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006240 active |= mFullWifiLockOut;
6241 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006242 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006243 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006244 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006245 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006246 if (mWifiBatchedScanTimer != null) {
6247 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6248 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006249 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006250 }
6251 }
6252 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6253 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006254 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006255 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006256 active |= mWifiMulticastEnabled;
6257 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006258
6259 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6260 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6261 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6262 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6263 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
6264 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
6265
Dianne Hackborn61659e52014-07-09 16:13:01 -07006266 if (mProcessStateTimer != null) {
6267 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6268 if (mProcessStateTimer[i] != null) {
6269 active |= !mProcessStateTimer[i].reset(false);
6270 }
6271 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006272 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006273 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006274 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006275 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006276 mVibratorOnTimer.detach();
6277 mVibratorOnTimer = null;
6278 } else {
6279 active = true;
6280 }
6281 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006282
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006283 if (mUserActivityCounters != null) {
6284 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6285 mUserActivityCounters[i].reset(false);
6286 }
6287 }
6288
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006289 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006290 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006291 mNetworkByteActivityCounters[i].reset(false);
6292 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006293 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006294 mMobileRadioActiveTime.reset(false);
6295 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006296 }
6297
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006298 if (mWifiControllerActivity != null) {
6299 mWifiControllerActivity.reset(false);
6300 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006301
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006302 if (mBluetoothControllerActivity != null) {
6303 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006304 }
6305
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006306 if (mModemControllerActivity != null) {
6307 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006308 }
6309
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006310 mUserCpuTime.reset(false);
6311 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006312 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006313
6314 if (mCpuClusterSpeed != null) {
6315 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
6316 if (speeds != null) {
6317 for (LongSamplingCounter speed : speeds) {
6318 if (speed != null) {
6319 speed.reset(false);
6320 }
6321 }
6322 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006323 }
6324 }
6325
Adam Lesinski5f056f62016-07-14 16:56:08 -07006326 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6327 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6328
Dianne Hackbornd953c532014-08-16 18:17:38 -07006329 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6330 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6331 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006332 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006333 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006334 } else {
6335 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006336 }
6337 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006338 mWakelockStats.cleanup();
6339 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6340 for (int is=syncStats.size()-1; is>=0; is--) {
6341 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006342 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006343 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006344 timer.detach();
6345 } else {
6346 active = true;
6347 }
6348 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006349 mSyncStats.cleanup();
6350 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6351 for (int ij=jobStats.size()-1; ij>=0; ij--) {
6352 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006353 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006354 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006355 timer.detach();
6356 } else {
6357 active = true;
6358 }
6359 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006360 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006361 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6362 Sensor s = mSensorStats.valueAt(ise);
6363 if (s.reset()) {
6364 mSensorStats.removeAt(ise);
6365 } else {
6366 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006367 }
6368 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006369 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6370 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006371 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006372 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006373 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006374 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006375 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006376 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006377 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006378 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006379 } else {
6380 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006381 }
6382 }
6383 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006384 if (mPackageStats.size() > 0) {
6385 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6386 while (it.hasNext()) {
6387 Map.Entry<String, Pkg> pkgEntry = it.next();
6388 Pkg p = pkgEntry.getValue();
6389 p.detach();
6390 if (p.mServiceStats.size() > 0) {
6391 Iterator<Map.Entry<String, Pkg.Serv>> it2
6392 = p.mServiceStats.entrySet().iterator();
6393 while (it2.hasNext()) {
6394 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6395 servEntry.getValue().detach();
6396 }
6397 }
6398 }
6399 mPackageStats.clear();
6400 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006401
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006402 mLastStepUserTime = mLastStepSystemTime = 0;
6403 mCurStepUserTime = mCurStepSystemTime = 0;
6404
Bookatz867c0d72017-03-07 18:23:42 -08006405 mOnBatteryBackgroundTimeBase.reset(mBsi.mClocks.elapsedRealtime() * 1000,
6406 mBsi.mClocks.uptimeMillis() * 1000);
6407
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006408 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006409 if (mWifiRunningTimer != null) {
6410 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006411 }
6412 if (mFullWifiLockTimer != null) {
6413 mFullWifiLockTimer.detach();
6414 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006415 if (mWifiScanTimer != null) {
6416 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006417 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006418 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6419 if (mWifiBatchedScanTimer[i] != null) {
6420 mWifiBatchedScanTimer[i].detach();
6421 }
6422 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006423 if (mWifiMulticastTimer != null) {
6424 mWifiMulticastTimer.detach();
6425 }
6426 if (mAudioTurnedOnTimer != null) {
6427 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006428 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006429 }
6430 if (mVideoTurnedOnTimer != null) {
6431 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006432 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006433 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006434 if (mFlashlightTurnedOnTimer != null) {
6435 mFlashlightTurnedOnTimer.detach();
6436 mFlashlightTurnedOnTimer = null;
6437 }
6438 if (mCameraTurnedOnTimer != null) {
6439 mCameraTurnedOnTimer.detach();
6440 mCameraTurnedOnTimer = null;
6441 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006442 if (mForegroundActivityTimer != null) {
6443 mForegroundActivityTimer.detach();
6444 mForegroundActivityTimer = null;
6445 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006446 if (mBluetoothScanTimer != null) {
6447 mBluetoothScanTimer.detach();
6448 mBluetoothScanTimer = null;
6449 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006450 if (mUserActivityCounters != null) {
6451 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6452 mUserActivityCounters[i].detach();
6453 }
6454 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006455 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006456 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006457 mNetworkByteActivityCounters[i].detach();
6458 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006459 }
6460 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006461
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006462 if (mWifiControllerActivity != null) {
6463 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006464 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006465
6466 if (mBluetoothControllerActivity != null) {
6467 mBluetoothControllerActivity.detach();
6468 }
6469
6470 if (mModemControllerActivity != null) {
6471 mModemControllerActivity.detach();
6472 }
6473
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006474 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006475
6476 mUserCpuTime.detach();
6477 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006478 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006479
6480 if (mCpuClusterSpeed != null) {
6481 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6482 if (cpuSpeeds != null) {
6483 for (LongSamplingCounter c : cpuSpeeds) {
6484 if (c != null) {
6485 c.detach();
6486 }
6487 }
6488 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006489 }
6490 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006491
6492 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6493 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006494 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006495
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006496 return !active;
6497 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006498
Bookatz867c0d72017-03-07 18:23:42 -08006499 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
6500 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
6501
Dianne Hackbornd953c532014-08-16 18:17:38 -07006502 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6503 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006504 out.writeInt(NW);
6505 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006506 out.writeString(wakeStats.keyAt(iw));
6507 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006508 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 }
6510
Dianne Hackbornd953c532014-08-16 18:17:38 -07006511 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6512 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006513 out.writeInt(NS);
6514 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006515 out.writeString(syncStats.keyAt(is));
6516 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006517 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6518 }
6519
Dianne Hackbornd953c532014-08-16 18:17:38 -07006520 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6521 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006522 out.writeInt(NJ);
6523 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006524 out.writeString(jobStats.keyAt(ij));
6525 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006526 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6527 }
6528
Dianne Hackborn61659e52014-07-09 16:13:01 -07006529 int NSE = mSensorStats.size();
6530 out.writeInt(NSE);
6531 for (int ise=0; ise<NSE; ise++) {
6532 out.writeInt(mSensorStats.keyAt(ise));
6533 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006534 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 }
6536
Dianne Hackborn61659e52014-07-09 16:13:01 -07006537 int NP = mProcessStats.size();
6538 out.writeInt(NP);
6539 for (int ip=0; ip<NP; ip++) {
6540 out.writeString(mProcessStats.keyAt(ip));
6541 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 proc.writeToParcelLocked(out);
6543 }
6544
6545 out.writeInt(mPackageStats.size());
6546 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6547 out.writeString(pkgEntry.getKey());
6548 Uid.Pkg pkg = pkgEntry.getValue();
6549 pkg.writeToParcelLocked(out);
6550 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006551
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006552 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006553 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006554 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006555 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006556 out.writeInt(0);
6557 }
6558 if (mFullWifiLockTimer != null) {
6559 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006560 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006561 } else {
6562 out.writeInt(0);
6563 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006564 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006565 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006566 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006567 } else {
6568 out.writeInt(0);
6569 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006570 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6571 if (mWifiBatchedScanTimer[i] != null) {
6572 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006573 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006574 } else {
6575 out.writeInt(0);
6576 }
6577 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006578 if (mWifiMulticastTimer != null) {
6579 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006580 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006581 } else {
6582 out.writeInt(0);
6583 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006584
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006585 if (mAudioTurnedOnTimer != null) {
6586 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006587 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006588 } else {
6589 out.writeInt(0);
6590 }
6591 if (mVideoTurnedOnTimer != null) {
6592 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006593 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006594 } else {
6595 out.writeInt(0);
6596 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006597 if (mFlashlightTurnedOnTimer != null) {
6598 out.writeInt(1);
6599 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6600 } else {
6601 out.writeInt(0);
6602 }
6603 if (mCameraTurnedOnTimer != null) {
6604 out.writeInt(1);
6605 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6606 } else {
6607 out.writeInt(0);
6608 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006609 if (mForegroundActivityTimer != null) {
6610 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006611 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006612 } else {
6613 out.writeInt(0);
6614 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006615 if (mBluetoothScanTimer != null) {
6616 out.writeInt(1);
6617 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6618 } else {
6619 out.writeInt(0);
6620 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006621 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6622 if (mProcessStateTimer[i] != null) {
6623 out.writeInt(1);
6624 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6625 } else {
6626 out.writeInt(0);
6627 }
6628 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006629 if (mVibratorOnTimer != null) {
6630 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006631 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006632 } else {
6633 out.writeInt(0);
6634 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006635 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006636 out.writeInt(1);
6637 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6638 mUserActivityCounters[i].writeToParcel(out);
6639 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006640 } else {
6641 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006642 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006643 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006644 out.writeInt(1);
6645 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006646 mNetworkByteActivityCounters[i].writeToParcel(out);
6647 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006648 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006649 mMobileRadioActiveTime.writeToParcel(out);
6650 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006651 } else {
6652 out.writeInt(0);
6653 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006654
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006655 if (mWifiControllerActivity != null) {
6656 out.writeInt(1);
6657 mWifiControllerActivity.writeToParcel(out, 0);
6658 } else {
6659 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006660 }
6661
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006662 if (mBluetoothControllerActivity != null) {
6663 out.writeInt(1);
6664 mBluetoothControllerActivity.writeToParcel(out, 0);
6665 } else {
6666 out.writeInt(0);
6667 }
6668
6669 if (mModemControllerActivity != null) {
6670 out.writeInt(1);
6671 mModemControllerActivity.writeToParcel(out, 0);
6672 } else {
6673 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006674 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006675
6676 mUserCpuTime.writeToParcel(out);
6677 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006678 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006679
Adam Lesinski6832f392015-09-05 18:05:40 -07006680 if (mCpuClusterSpeed != null) {
6681 out.writeInt(1);
6682 out.writeInt(mCpuClusterSpeed.length);
6683 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6684 if (cpuSpeeds != null) {
6685 out.writeInt(1);
6686 out.writeInt(cpuSpeeds.length);
6687 for (LongSamplingCounter c : cpuSpeeds) {
6688 if (c != null) {
6689 out.writeInt(1);
6690 c.writeToParcel(out);
6691 } else {
6692 out.writeInt(0);
6693 }
6694 }
6695 } else {
6696 out.writeInt(0);
6697 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006698 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006699 } else {
6700 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006701 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006702
6703 if (mMobileRadioApWakeupCount != null) {
6704 out.writeInt(1);
6705 mMobileRadioApWakeupCount.writeToParcel(out);
6706 } else {
6707 out.writeInt(0);
6708 }
6709
6710 if (mWifiRadioApWakeupCount != null) {
6711 out.writeInt(1);
6712 mWifiRadioApWakeupCount.writeToParcel(out);
6713 } else {
6714 out.writeInt(0);
6715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 }
6717
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006718 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08006719 mOnBatteryBackgroundTimeBase.readFromParcel(in);
6720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006721 int numWakelocks = in.readInt();
6722 mWakelockStats.clear();
6723 for (int j = 0; j < numWakelocks; j++) {
6724 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006725 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006726 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006727 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 }
6729
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006730 int numSyncs = in.readInt();
6731 mSyncStats.clear();
6732 for (int j = 0; j < numSyncs; j++) {
6733 String syncName = in.readString();
6734 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006735 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006736 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006737 }
6738 }
6739
6740 int numJobs = in.readInt();
6741 mJobStats.clear();
6742 for (int j = 0; j < numJobs; j++) {
6743 String jobName = in.readString();
6744 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006745 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
6746 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006747 }
6748 }
6749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 int numSensors = in.readInt();
6751 mSensorStats.clear();
6752 for (int k = 0; k < numSensors; k++) {
6753 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006754 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08006755 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
6756 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 mSensorStats.put(sensorNumber, sensor);
6758 }
6759
6760 int numProcs = in.readInt();
6761 mProcessStats.clear();
6762 for (int k = 0; k < numProcs; k++) {
6763 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006764 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 proc.readFromParcelLocked(in);
6766 mProcessStats.put(processName, proc);
6767 }
6768
6769 int numPkgs = in.readInt();
6770 mPackageStats.clear();
6771 for (int l = 0; l < numPkgs; l++) {
6772 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006773 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 pkg.readFromParcelLocked(in);
6775 mPackageStats.put(packageName, pkg);
6776 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006777
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006778 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006779 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006780 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6781 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006782 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006783 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006784 }
6785 mFullWifiLockOut = false;
6786 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006787 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6788 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006789 } else {
6790 mFullWifiLockTimer = null;
6791 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006792 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006793 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08006794 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6795 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
6796 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006797 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006798 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006799 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006800 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6801 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6802 if (in.readInt() != 0) {
6803 makeWifiBatchedScanBin(i, in);
6804 } else {
6805 mWifiBatchedScanTimer[i] = null;
6806 }
6807 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006808 mWifiMulticastEnabled = false;
6809 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006810 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6811 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006812 } else {
6813 mWifiMulticastTimer = null;
6814 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006815 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006816 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6817 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006818 } else {
6819 mAudioTurnedOnTimer = null;
6820 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006821 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006822 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6823 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006824 } else {
6825 mVideoTurnedOnTimer = null;
6826 }
6827 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006828 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6829 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006830 } else {
6831 mFlashlightTurnedOnTimer = null;
6832 }
6833 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006834 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6835 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006836 } else {
6837 mCameraTurnedOnTimer = null;
6838 }
6839 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006840 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6841 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006842 } else {
6843 mForegroundActivityTimer = null;
6844 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006845 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08006846 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6847 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6848 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006849 } else {
6850 mBluetoothScanTimer = null;
6851 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006852 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006853 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6854 if (in.readInt() != 0) {
6855 makeProcessState(i, in);
6856 } else {
6857 mProcessStateTimer[i] = null;
6858 }
6859 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006860 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006861 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6862 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006863 } else {
6864 mVibratorOnTimer = null;
6865 }
6866 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006867 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6868 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006869 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006870 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006871 } else {
6872 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006873 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006874 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006875 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6876 mNetworkPacketActivityCounters
6877 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006878 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006879 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006880 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006881 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006882 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006883 }
Joe Onoratoabded112016-02-08 16:49:39 -08006884 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6885 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006886 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006887 mNetworkByteActivityCounters = null;
6888 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006889 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006890
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006891 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006892 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006893 NUM_WIFI_TX_LEVELS, in);
6894 } else {
6895 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006896 }
6897
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006898 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006899 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006900 NUM_BT_TX_LEVELS, in);
6901 } else {
6902 mBluetoothControllerActivity = null;
6903 }
6904
6905 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006906 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006907 ModemActivityInfo.TX_POWER_LEVELS, in);
6908 } else {
6909 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006910 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006911
Joe Onoratoabded112016-02-08 16:49:39 -08006912 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6913 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6914 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006915
Adam Lesinski6832f392015-09-05 18:05:40 -07006916 if (in.readInt() != 0) {
6917 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006918 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006919 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006920 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006921
6922 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6923 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6924 if (in.readInt() != 0) {
6925 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006926 if (mBsi.mPowerProfile != null &&
6927 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006928 throw new ParcelFormatException("Incompatible number of cpu speeds");
6929 }
6930
6931 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6932 mCpuClusterSpeed[cluster] = cpuSpeeds;
6933 for (int speed = 0; speed < numSpeeds; speed++) {
6934 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006935 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006936 }
6937 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006938 } else {
6939 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006940 }
6941 }
6942 } else {
6943 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006944 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006945
6946 if (in.readInt() != 0) {
6947 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6948 } else {
6949 mMobileRadioApWakeupCount = null;
6950 }
6951
6952 if (in.readInt() != 0) {
6953 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6954 } else {
6955 mWifiRadioApWakeupCount = null;
6956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006957 }
6958
6959 /**
6960 * The statistics associated with a particular wake lock.
6961 */
Joe Onoratoabded112016-02-08 16:49:39 -08006962 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6963 /**
6964 * BatteryStatsImpl that we are associated with.
6965 */
6966 protected BatteryStatsImpl mBsi;
6967
6968 /**
6969 * BatteryStatsImpl that we are associated with.
6970 */
6971 protected Uid mUid;
6972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 /**
6974 * How long (in ms) this uid has been keeping the device partially awake.
6975 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07006976 DurationTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977
6978 /**
6979 * How long (in ms) this uid has been keeping the device fully awake.
6980 */
Evan Millarc64edde2009-04-18 12:26:32 -07006981 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982
6983 /**
6984 * How long (in ms) this uid has had a window keeping the device awake.
6985 */
Evan Millarc64edde2009-04-18 12:26:32 -07006986 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987
6988 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006989 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006990 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006991 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006992
Joe Onoratoabded112016-02-08 16:49:39 -08006993 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6994 mBsi = bsi;
6995 mUid = uid;
6996 }
6997
Adam Lesinski9425fe22015-06-19 12:02:13 -07006998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7000 * proper timer pool from the given BatteryStatsImpl object.
7001 *
7002 * @param in the Parcel to be read from.
7003 * return a new Timer, or null.
7004 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007005 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7006 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 if (in.readInt() == 0) {
7008 return null;
7009 }
7010
Joe Onoratoabded112016-02-08 16:49:39 -08007011 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 }
7013
Joe Onorato92fd23f2016-07-25 11:18:42 -07007014 /**
7015 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7016 * proper timer pool from the given BatteryStatsImpl object.
7017 *
7018 * @param in the Parcel to be read from.
7019 * return a new Timer, or null.
7020 */
7021 private DurationTimer readDurationTimerFromParcel(int type,
7022 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
7023 if (in.readInt() == 0) {
7024 return null;
7025 }
7026
7027 return new DurationTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
7028 }
7029
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007030 boolean reset() {
7031 boolean wlactive = false;
7032 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007033 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007034 }
7035 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007036 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007037 }
7038 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007039 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007040 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007041 if (mTimerDraw != null) {
7042 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007043 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007044 if (!wlactive) {
7045 if (mTimerFull != null) {
7046 mTimerFull.detach();
7047 mTimerFull = null;
7048 }
7049 if (mTimerPartial != null) {
7050 mTimerPartial.detach();
7051 mTimerPartial = null;
7052 }
7053 if (mTimerWindow != null) {
7054 mTimerWindow.detach();
7055 mTimerWindow = null;
7056 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007057 if (mTimerDraw != null) {
7058 mTimerDraw.detach();
7059 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007060 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007061 }
7062 return !wlactive;
7063 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007064
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007065 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007066 mTimerPartial = readDurationTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08007067 mBsi.mPartialTimers, screenOffTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007068 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7069 mBsi.mFullTimers, timeBase, in);
7070 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7071 mBsi.mWindowTimers, timeBase, in);
7072 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7073 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 }
7075
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007076 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7077 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7078 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7079 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007080 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 }
7082
7083 @Override
7084 public Timer getWakeTime(int type) {
7085 switch (type) {
7086 case WAKE_TYPE_FULL: return mTimerFull;
7087 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7088 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007089 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 default: throw new IllegalArgumentException("type = " + type);
7091 }
7092 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007093
7094 public StopwatchTimer getStopwatchTimer(int type) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007095 switch (type) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007096 case WAKE_TYPE_PARTIAL: {
7097 DurationTimer t = mTimerPartial;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007098 if (t == null) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007099 t = new DurationTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08007100 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007101 mTimerPartial = t;
7102 }
7103 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007104 }
7105 case WAKE_TYPE_FULL: {
7106 StopwatchTimer t = mTimerFull;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007107 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007108 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
7109 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007110 mTimerFull = t;
7111 }
7112 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007113 }
7114 case WAKE_TYPE_WINDOW: {
7115 StopwatchTimer t = mTimerWindow;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007116 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007117 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
7118 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007119 mTimerWindow = t;
7120 }
7121 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007122 }
7123 case WAKE_TYPE_DRAW: {
7124 StopwatchTimer t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007125 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007126 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
7127 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007128 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007129 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007130 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007131 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007132 default:
7133 throw new IllegalArgumentException("type=" + type);
7134 }
7135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 }
7137
Joe Onoratoabded112016-02-08 16:49:39 -08007138 public static class Sensor extends BatteryStats.Uid.Sensor {
7139 /**
7140 * BatteryStatsImpl that we are associated with.
7141 */
7142 protected BatteryStatsImpl mBsi;
7143
7144 /**
Bookatz867c0d72017-03-07 18:23:42 -08007145 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007146 */
7147 protected Uid mUid;
7148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007150 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007151
Joe Onoratoabded112016-02-08 16:49:39 -08007152 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7153 mBsi = bsi;
7154 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 mHandle = handle;
7156 }
7157
Bookatz867c0d72017-03-07 18:23:42 -08007158 private DualTimer readTimersFromParcel(
7159 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 if (in.readInt() == 0) {
7161 return null;
7162 }
7163
Joe Onoratoabded112016-02-08 16:49:39 -08007164 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007166 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007167 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 }
Bookatz867c0d72017-03-07 18:23:42 -08007169 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007170 }
7171
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007172 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007173 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007174 mTimer = null;
7175 return true;
7176 }
7177 return false;
7178 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007179
Bookatz867c0d72017-03-07 18:23:42 -08007180 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7181 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 }
7183
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007184 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatz867c0d72017-03-07 18:23:42 -08007185 DualTimer.writeDualTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 }
7187
7188 @Override
7189 public Timer getSensorTime() {
Bookatz867c0d72017-03-07 18:23:42 -08007190 if (mTimer == null) {
7191 return null;
7192 }
7193 return mTimer.getMainTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007195
7196 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007197 public Timer getSensorBackgroundTime() {
7198 if (mTimer == null) {
7199 return null;
7200 }
7201 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007202 }
7203
7204 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205 public int getHandle() {
7206 return mHandle;
7207 }
7208 }
7209
7210 /**
7211 * The statistics associated with a particular process.
7212 */
Joe Onoratoabded112016-02-08 16:49:39 -08007213 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7214 /**
7215 * BatteryStatsImpl that we are associated with.
7216 */
7217 protected BatteryStatsImpl mBsi;
7218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007220 * The name of this process.
7221 */
7222 final String mName;
7223
7224 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007225 * Remains true until removed from the stats.
7226 */
7227 boolean mActive = true;
7228
7229 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007230 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 */
7232 long mUserTime;
7233
7234 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007235 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 */
7237 long mSystemTime;
7238
7239 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007240 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007241 */
7242 long mForegroundTime;
7243
7244 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007245 * Number of times the process has been started.
7246 */
7247 int mStarts;
7248
7249 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007250 * Number of times the process has crashed.
7251 */
7252 int mNumCrashes;
7253
7254 /**
7255 * Number of times the process has had an ANR.
7256 */
7257 int mNumAnrs;
7258
7259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 * The amount of user time loaded from a previous save.
7261 */
7262 long mLoadedUserTime;
7263
7264 /**
7265 * The amount of system time loaded from a previous save.
7266 */
7267 long mLoadedSystemTime;
7268
7269 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007270 * The amount of foreground time loaded from a previous save.
7271 */
7272 long mLoadedForegroundTime;
7273
7274 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007275 * The number of times the process has started from a previous save.
7276 */
7277 int mLoadedStarts;
7278
7279 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007280 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007282 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283
7284 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007285 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007287 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007288
7289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 * The amount of user time when last unplugged.
7291 */
7292 long mUnpluggedUserTime;
7293
7294 /**
7295 * The amount of system time when last unplugged.
7296 */
7297 long mUnpluggedSystemTime;
7298
7299 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007300 * The amount of foreground time since unplugged.
7301 */
7302 long mUnpluggedForegroundTime;
7303
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007304 /**
7305 * The number of times the process has started before unplugged.
7306 */
7307 int mUnpluggedStarts;
7308
Dianne Hackborn61659e52014-07-09 16:13:01 -07007309 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007310 * Number of times the process has crashed before unplugged.
7311 */
7312 int mUnpluggedNumCrashes;
7313
7314 /**
7315 * Number of times the process has had an ANR before unplugged.
7316 */
7317 int mUnpluggedNumAnrs;
7318
Dianne Hackborn287952c2010-09-22 22:34:31 -07007319 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007320
Joe Onoratoabded112016-02-08 16:49:39 -08007321 public Proc(BatteryStatsImpl bsi, String name) {
7322 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007323 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007324 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007326
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007327 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 mUnpluggedUserTime = mUserTime;
7329 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007330 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007331 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007332 mUnpluggedNumCrashes = mNumCrashes;
7333 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 }
7335
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007336 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007338
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007339 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007340 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007341 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007342 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007343
Dianne Hackborn287952c2010-09-22 22:34:31 -07007344 public int countExcessivePowers() {
7345 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007346 }
7347
Dianne Hackborn287952c2010-09-22 22:34:31 -07007348 public ExcessivePower getExcessivePower(int i) {
7349 if (mExcessivePower != null) {
7350 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007351 }
7352 return null;
7353 }
7354
7355 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007356 if (mExcessivePower == null) {
7357 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007358 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007359 ExcessivePower ew = new ExcessivePower();
7360 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007361 ew.overTime = overTime;
7362 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007363 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007364 }
7365
Dianne Hackborn287952c2010-09-22 22:34:31 -07007366 public void addExcessiveCpu(long overTime, long usedTime) {
7367 if (mExcessivePower == null) {
7368 mExcessivePower = new ArrayList<ExcessivePower>();
7369 }
7370 ExcessivePower ew = new ExcessivePower();
7371 ew.type = ExcessivePower.TYPE_CPU;
7372 ew.overTime = overTime;
7373 ew.usedTime = usedTime;
7374 mExcessivePower.add(ew);
7375 }
7376
7377 void writeExcessivePowerToParcelLocked(Parcel out) {
7378 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007379 out.writeInt(0);
7380 return;
7381 }
7382
Dianne Hackborn287952c2010-09-22 22:34:31 -07007383 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007384 out.writeInt(N);
7385 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007386 ExcessivePower ew = mExcessivePower.get(i);
7387 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007388 out.writeLong(ew.overTime);
7389 out.writeLong(ew.usedTime);
7390 }
7391 }
7392
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007393 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007394 final int N = in.readInt();
7395 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007396 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007397 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007398 }
7399
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007400 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007401 throw new ParcelFormatException(
7402 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007403 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007404
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007405 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007406 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007407 ExcessivePower ew = new ExcessivePower();
7408 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007409 ew.overTime = in.readLong();
7410 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007411 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007412 }
7413 }
7414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 void writeToParcelLocked(Parcel out) {
7416 out.writeLong(mUserTime);
7417 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007418 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007419 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007420 out.writeInt(mNumCrashes);
7421 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007422 out.writeLong(mLoadedUserTime);
7423 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007424 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007426 out.writeInt(mLoadedNumCrashes);
7427 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 out.writeLong(mUnpluggedUserTime);
7429 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007430 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007432 out.writeInt(mUnpluggedNumCrashes);
7433 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007434 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 }
7436
7437 void readFromParcelLocked(Parcel in) {
7438 mUserTime = in.readLong();
7439 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007440 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007442 mNumCrashes = in.readInt();
7443 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007444 mLoadedUserTime = in.readLong();
7445 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007446 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007448 mLoadedNumCrashes = in.readInt();
7449 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 mUnpluggedUserTime = in.readLong();
7451 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007452 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007454 mUnpluggedNumCrashes = in.readInt();
7455 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007456 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 }
7458
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007459 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 mUserTime += utime;
7461 mSystemTime += stime;
7462 }
7463
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007464 public void addForegroundTimeLocked(long ttime) {
7465 mForegroundTime += ttime;
7466 }
7467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 public void incStartsLocked() {
7469 mStarts++;
7470 }
7471
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007472 public void incNumCrashesLocked() {
7473 mNumCrashes++;
7474 }
7475
7476 public void incNumAnrsLocked() {
7477 mNumAnrs++;
7478 }
7479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007481 public boolean isActive() {
7482 return mActive;
7483 }
7484
7485 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007487 long val = mUserTime;
7488 if (which == STATS_CURRENT) {
7489 val -= mLoadedUserTime;
7490 } else if (which == STATS_SINCE_UNPLUGGED) {
7491 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 }
7493 return val;
7494 }
7495
7496 @Override
7497 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007498 long val = mSystemTime;
7499 if (which == STATS_CURRENT) {
7500 val -= mLoadedSystemTime;
7501 } else if (which == STATS_SINCE_UNPLUGGED) {
7502 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 }
7504 return val;
7505 }
7506
7507 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007508 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007509 long val = mForegroundTime;
7510 if (which == STATS_CURRENT) {
7511 val -= mLoadedForegroundTime;
7512 } else if (which == STATS_SINCE_UNPLUGGED) {
7513 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007514 }
7515 return val;
7516 }
7517
7518 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007520 int val = mStarts;
7521 if (which == STATS_CURRENT) {
7522 val -= mLoadedStarts;
7523 } else if (which == STATS_SINCE_UNPLUGGED) {
7524 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 }
7526 return val;
7527 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007528
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007529 @Override
7530 public int getNumCrashes(int which) {
7531 int val = mNumCrashes;
7532 if (which == STATS_CURRENT) {
7533 val -= mLoadedNumCrashes;
7534 } else if (which == STATS_SINCE_UNPLUGGED) {
7535 val -= mUnpluggedNumCrashes;
7536 }
7537 return val;
7538 }
7539
7540 @Override
7541 public int getNumAnrs(int which) {
7542 int val = mNumAnrs;
7543 if (which == STATS_CURRENT) {
7544 val -= mLoadedNumAnrs;
7545 } else if (which == STATS_SINCE_UNPLUGGED) {
7546 val -= mUnpluggedNumAnrs;
7547 }
7548 return val;
7549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007550 }
7551
7552 /**
7553 * The statistics associated with a particular package.
7554 */
Joe Onoratoabded112016-02-08 16:49:39 -08007555 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7556 /**
7557 * BatteryStatsImpl that we are associated with.
7558 */
7559 protected BatteryStatsImpl mBsi;
7560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007562 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007564 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565
7566 /**
7567 * The statics we have collected for this package's services.
7568 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007569 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570
Joe Onoratoabded112016-02-08 16:49:39 -08007571 public Pkg(BatteryStatsImpl bsi) {
7572 mBsi = bsi;
7573 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007575
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007576 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
7578
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007579 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007581
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007582 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007583 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007584 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007587 int numWA = in.readInt();
7588 mWakeupAlarms.clear();
7589 for (int i=0; i<numWA; i++) {
7590 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007591 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007593
7594 int numServs = in.readInt();
7595 mServiceStats.clear();
7596 for (int m = 0; m < numServs; m++) {
7597 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007598 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 mServiceStats.put(serviceName, serv);
7600
7601 serv.readFromParcelLocked(in);
7602 }
7603 }
7604
7605 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007606 int numWA = mWakeupAlarms.size();
7607 out.writeInt(numWA);
7608 for (int i=0; i<numWA; i++) {
7609 out.writeString(mWakeupAlarms.keyAt(i));
7610 mWakeupAlarms.valueAt(i).writeToParcel(out);
7611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007613 final int NS = mServiceStats.size();
7614 out.writeInt(NS);
7615 for (int i=0; i<NS; i++) {
7616 out.writeString(mServiceStats.keyAt(i));
7617 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 serv.writeToParcelLocked(out);
7619 }
7620 }
7621
7622 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007623 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7624 return mWakeupAlarms;
7625 }
7626
7627 public void noteWakeupAlarmLocked(String tag) {
7628 Counter c = mWakeupAlarms.get(tag);
7629 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007630 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007631 mWakeupAlarms.put(tag, c);
7632 }
7633 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 }
7635
7636 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007637 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
7638 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 }
7640
7641 /**
7642 * The statistics associated with a particular service.
7643 */
Joe Onoratoabded112016-02-08 16:49:39 -08007644 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
7645 /**
7646 * BatteryStatsImpl that we are associated with.
7647 */
7648 protected BatteryStatsImpl mBsi;
7649
7650 /**
7651 * The android package in which this service resides.
7652 */
7653 protected Pkg mPkg;
7654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 /**
7656 * Total time (ms in battery uptime) the service has been left started.
7657 */
Joe Onoratoabded112016-02-08 16:49:39 -08007658 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659
7660 /**
7661 * If service has been started and not yet stopped, this is
7662 * when it was started.
7663 */
Joe Onoratoabded112016-02-08 16:49:39 -08007664 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665
7666 /**
7667 * True if we are currently running.
7668 */
Joe Onoratoabded112016-02-08 16:49:39 -08007669 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670
7671 /**
7672 * Total number of times startService() has been called.
7673 */
Joe Onoratoabded112016-02-08 16:49:39 -08007674 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007675
7676 /**
7677 * Total time (ms in battery uptime) the service has been left launched.
7678 */
Joe Onoratoabded112016-02-08 16:49:39 -08007679 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680
7681 /**
7682 * If service has been launched and not yet exited, this is
7683 * when it was launched (ms in battery uptime).
7684 */
Joe Onoratoabded112016-02-08 16:49:39 -08007685 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686
7687 /**
7688 * True if we are currently launched.
7689 */
Joe Onoratoabded112016-02-08 16:49:39 -08007690 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691
7692 /**
7693 * Total number times the service has been launched.
7694 */
Joe Onoratoabded112016-02-08 16:49:39 -08007695 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696
7697 /**
7698 * The amount of time spent started loaded from a previous save
7699 * (ms in battery uptime).
7700 */
Joe Onoratoabded112016-02-08 16:49:39 -08007701 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007702
7703 /**
7704 * The number of starts loaded from a previous save.
7705 */
Joe Onoratoabded112016-02-08 16:49:39 -08007706 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707
7708 /**
7709 * The number of launches loaded from a previous save.
7710 */
Joe Onoratoabded112016-02-08 16:49:39 -08007711 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712
7713 /**
7714 * The amount of time spent started as of the last run (ms
7715 * in battery uptime).
7716 */
Joe Onoratoabded112016-02-08 16:49:39 -08007717 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718
7719 /**
7720 * The number of starts as of the last run.
7721 */
Joe Onoratoabded112016-02-08 16:49:39 -08007722 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723
7724 /**
7725 * The number of launches as of the last run.
7726 */
Joe Onoratoabded112016-02-08 16:49:39 -08007727 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728
7729 /**
7730 * The amount of time spent started when last unplugged (ms
7731 * in battery uptime).
7732 */
Joe Onoratoabded112016-02-08 16:49:39 -08007733 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734
7735 /**
7736 * The number of starts when last unplugged.
7737 */
Joe Onoratoabded112016-02-08 16:49:39 -08007738 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739
7740 /**
7741 * The number of launches when last unplugged.
7742 */
Joe Onoratoabded112016-02-08 16:49:39 -08007743 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744
Joe Onoratoabded112016-02-08 16:49:39 -08007745 /**
7746 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7747 */
7748 public Serv(BatteryStatsImpl bsi) {
7749 mBsi = bsi;
7750 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007752
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007753 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7754 long baseRealtime) {
7755 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007756 mUnpluggedStarts = mStarts;
7757 mUnpluggedLaunches = mLaunches;
7758 }
7759
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007760 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7761 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007763
Joe Onoratoabded112016-02-08 16:49:39 -08007764 /**
7765 * Remove this Serv as a listener from the time base.
7766 */
7767 public void detach() {
7768 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007769 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007770
Joe Onoratoabded112016-02-08 16:49:39 -08007771 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 mStartTime = in.readLong();
7773 mRunningSince = in.readLong();
7774 mRunning = in.readInt() != 0;
7775 mStarts = in.readInt();
7776 mLaunchedTime = in.readLong();
7777 mLaunchedSince = in.readLong();
7778 mLaunched = in.readInt() != 0;
7779 mLaunches = in.readInt();
7780 mLoadedStartTime = in.readLong();
7781 mLoadedStarts = in.readInt();
7782 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007783 mLastStartTime = 0;
7784 mLastStarts = 0;
7785 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007786 mUnpluggedStartTime = in.readLong();
7787 mUnpluggedStarts = in.readInt();
7788 mUnpluggedLaunches = in.readInt();
7789 }
7790
Joe Onoratoabded112016-02-08 16:49:39 -08007791 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 out.writeLong(mStartTime);
7793 out.writeLong(mRunningSince);
7794 out.writeInt(mRunning ? 1 : 0);
7795 out.writeInt(mStarts);
7796 out.writeLong(mLaunchedTime);
7797 out.writeLong(mLaunchedSince);
7798 out.writeInt(mLaunched ? 1 : 0);
7799 out.writeInt(mLaunches);
7800 out.writeLong(mLoadedStartTime);
7801 out.writeInt(mLoadedStarts);
7802 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 out.writeLong(mUnpluggedStartTime);
7804 out.writeInt(mUnpluggedStarts);
7805 out.writeInt(mUnpluggedLaunches);
7806 }
7807
Joe Onoratoabded112016-02-08 16:49:39 -08007808 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 if (!mLaunched) return mLaunchedTime;
7810 return mLaunchedTime + batteryUptime - mLaunchedSince;
7811 }
7812
Joe Onoratoabded112016-02-08 16:49:39 -08007813 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007814 if (!mRunning) return mStartTime;
7815 return mStartTime + batteryUptime - mRunningSince;
7816 }
7817
7818 public void startLaunchedLocked() {
7819 if (!mLaunched) {
7820 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007821 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 mLaunched = true;
7823 }
7824 }
7825
7826 public void stopLaunchedLocked() {
7827 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007828 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 if (time > 0) {
7830 mLaunchedTime += time;
7831 } else {
7832 mLaunches--;
7833 }
7834 mLaunched = false;
7835 }
7836 }
7837
7838 public void startRunningLocked() {
7839 if (!mRunning) {
7840 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007841 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 mRunning = true;
7843 }
7844 }
7845
7846 public void stopRunningLocked() {
7847 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007848 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 if (time > 0) {
7850 mStartTime += time;
7851 } else {
7852 mStarts--;
7853 }
7854 mRunning = false;
7855 }
7856 }
7857
7858 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007859 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 }
7861
7862 @Override
7863 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007864 int val = mLaunches;
7865 if (which == STATS_CURRENT) {
7866 val -= mLoadedLaunches;
7867 } else if (which == STATS_SINCE_UNPLUGGED) {
7868 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007870 return val;
7871 }
7872
7873 @Override
7874 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007875 long val = getStartTimeToNowLocked(now);
7876 if (which == STATS_CURRENT) {
7877 val -= mLoadedStartTime;
7878 } else if (which == STATS_SINCE_UNPLUGGED) {
7879 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 return val;
7882 }
7883
7884 @Override
7885 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007886 int val = mStarts;
7887 if (which == STATS_CURRENT) {
7888 val -= mLoadedStarts;
7889 } else if (which == STATS_SINCE_UNPLUGGED) {
7890 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 }
7892
7893 return val;
7894 }
7895 }
7896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007898 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 }
7900 }
7901
7902 /**
7903 * Retrieve the statistics object for a particular process, creating
7904 * if needed.
7905 */
7906 public Proc getProcessStatsLocked(String name) {
7907 Proc ps = mProcessStats.get(name);
7908 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007909 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 mProcessStats.put(name, ps);
7911 }
7912
7913 return ps;
7914 }
7915
Dianne Hackborna8d10942015-11-19 17:55:19 -08007916 public void updateUidProcessStateLocked(int procState) {
7917 int uidRunningState;
7918 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7919 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7920 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7921 uidRunningState = PROCESS_STATE_TOP;
7922 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7923 // Persistent and other foreground states go here.
7924 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7925 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7926 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7927 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7928 // Persistent and other foreground states go here.
7929 uidRunningState = PROCESS_STATE_FOREGROUND;
7930 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7931 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007932 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007933 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007934 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007935
Dianne Hackborna8d10942015-11-19 17:55:19 -08007936 if (mProcessState == uidRunningState) return;
7937
Bookatz867c0d72017-03-07 18:23:42 -08007938 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
7939 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007940
7941 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
Bookatz867c0d72017-03-07 18:23:42 -08007942 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborna8d10942015-11-19 17:55:19 -08007943 }
7944 mProcessState = uidRunningState;
7945 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7946 if (mProcessStateTimer[uidRunningState] == null) {
7947 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007948 }
Bookatz867c0d72017-03-07 18:23:42 -08007949 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007950 }
Bookatz867c0d72017-03-07 18:23:42 -08007951
7952 updateBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
7953 }
7954
7955 public boolean updateBgTimeBase(long uptimeUs, long realtimeUs) {
7956 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
7957 // also considered to be 'background' for our purposes, because it's not foreground.
7958 boolean isBgAndUnplugged = mBsi.mOnBatteryTimeBase.isRunning()
7959 && mProcessState >= PROCESS_STATE_BACKGROUND;
7960 return mOnBatteryBackgroundTimeBase.setRunning(isBgAndUnplugged, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007961 }
7962
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007963 public SparseArray<? extends Pid> getPidStats() {
7964 return mPids;
7965 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007966
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007967 public Pid getPidStatsLocked(int pid) {
7968 Pid p = mPids.get(pid);
7969 if (p == null) {
7970 p = new Pid();
7971 mPids.put(pid, p);
7972 }
7973 return p;
7974 }
7975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 /**
7977 * Retrieve the statistics object for a particular service, creating
7978 * if needed.
7979 */
7980 public Pkg getPackageStatsLocked(String name) {
7981 Pkg ps = mPackageStats.get(name);
7982 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007983 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 mPackageStats.put(name, ps);
7985 }
7986
7987 return ps;
7988 }
7989
7990 /**
7991 * Retrieve the statistics object for a particular service, creating
7992 * if needed.
7993 */
7994 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7995 Pkg ps = getPackageStatsLocked(pkg);
7996 Pkg.Serv ss = ps.mServiceStats.get(serv);
7997 if (ss == null) {
7998 ss = ps.newServiceStatsLocked();
7999 ps.mServiceStats.put(serv, ss);
8000 }
8001
8002 return ss;
8003 }
8004
Dianne Hackbornd953c532014-08-16 18:17:38 -07008005 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
8006 StopwatchTimer timer = mSyncStats.instantiateObject();
8007 timer.readSummaryFromParcelLocked(in);
8008 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008009 }
8010
Dianne Hackbornd953c532014-08-16 18:17:38 -07008011 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
8012 StopwatchTimer timer = mJobStats.instantiateObject();
8013 timer.readSummaryFromParcelLocked(in);
8014 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008015 }
8016
Dianne Hackbornd953c532014-08-16 18:17:38 -07008017 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008018 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008019 mWakelockStats.add(wlName, wl);
8020 if (in.readInt() != 0) {
8021 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008023 if (in.readInt() != 0) {
8024 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
8025 }
8026 if (in.readInt() != 0) {
8027 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008029 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07008030 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 }
8033
Bookatz867c0d72017-03-07 18:23:42 -08008034 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 Sensor se = mSensorStats.get(sensor);
8036 if (se == null) {
8037 if (!create) {
8038 return null;
8039 }
Joe Onoratoabded112016-02-08 16:49:39 -08008040 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 mSensorStats.put(sensor, se);
8042 }
Bookatz867c0d72017-03-07 18:23:42 -08008043 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 if (t != null) {
8045 return t;
8046 }
Joe Onoratoabded112016-02-08 16:49:39 -08008047 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008049 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008050 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 }
Bookatz867c0d72017-03-07 18:23:42 -08008052 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8053 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 se.mTimer = t;
8055 return t;
8056 }
8057
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008058 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008059 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008060 if (t != null) {
8061 t.startRunningLocked(elapsedRealtimeMs);
8062 }
8063 }
8064
8065 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008066 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008067 if (t != null) {
8068 t.stopRunningLocked(elapsedRealtimeMs);
8069 }
8070 }
8071
8072 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008073 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008074 if (t != null) {
8075 t.startRunningLocked(elapsedRealtimeMs);
8076 }
8077 }
8078
8079 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008080 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008081 if (t != null) {
8082 t.stopRunningLocked(elapsedRealtimeMs);
8083 }
8084 }
8085
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008086 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008087 Wakelock wl = mWakelockStats.startObject(name);
8088 if (wl != null) {
8089 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07008091 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008092 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008093 if (p.mWakeNesting++ == 0) {
8094 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008095 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 }
8098
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008099 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008100 Wakelock wl = mWakelockStats.stopObject(name);
8101 if (wl != null) {
8102 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07008104 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008105 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008106 if (p != null && p.mWakeNesting > 0) {
8107 if (p.mWakeNesting-- == 1) {
8108 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8109 p.mWakeStartMs = 0;
8110 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008111 }
8112 }
8113 }
8114
8115 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
8116 Proc p = getProcessStatsLocked(proc);
8117 if (p != null) {
8118 p.addExcessiveWake(overTime, usedTime);
8119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008121
Dianne Hackborn287952c2010-09-22 22:34:31 -07008122 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8123 Proc p = getProcessStatsLocked(proc);
8124 if (p != null) {
8125 p.addExcessiveCpu(overTime, usedTime);
8126 }
8127 }
8128
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008129 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008130 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008131 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 }
8133
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008134 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008136 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008138 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008141
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008142 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008143 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008145
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008146 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008147 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008148 }
8149
8150 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008151 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 }
8153 }
8154
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008155 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008156 this(new SystemClocks(), systemDir, handler, externalSync, null);
8157 }
8158
8159 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
8160 PlatformIdleStateCallback cb) {
8161 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08008162 }
8163
8164 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008165 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08008166 init(clocks);
8167
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008168 if (systemDir != null) {
8169 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
8170 new File(systemDir, "batterystats.bin.tmp"));
8171 } else {
8172 mFile = null;
8173 }
8174 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008175 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008176 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08008177 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08008179 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008180 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008181 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008182 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008183 }
Joe Onoratoabded112016-02-08 16:49:39 -08008184 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8185 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8186 mOnBatteryTimeBase);
8187 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8188 mOnBatteryTimeBase);
8189 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8190 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8191 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8192 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8193 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8194 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8195 mOnBatteryTimeBase);
8196 }
8197 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8198 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008199 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008200 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008201 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008202 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008203 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008204 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8205 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008206 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008207 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8208 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8209 NUM_BT_TX_LEVELS);
8210 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8211 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008212 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8213 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8214 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008215 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008216 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8217 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008218 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8219 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008220 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008221 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008222 mOnBatteryTimeBase);
8223 }
Joe Onoratoabded112016-02-08 16:49:39 -08008224 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8225 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8226 mOnBatteryTimeBase);
8227 }
8228 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8229 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8230 mOnBatteryTimeBase);
8231 }
8232 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8233 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8234 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8235 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8236 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008237 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8238 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008240 long uptime = mClocks.uptimeMillis() * 1000;
8241 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008242 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008243 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008244 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008245 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008246 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008247 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008248 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008249 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008250 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008251 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008252 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 }
8254
8255 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008256 this(new SystemClocks(), p);
8257 }
8258
8259 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8260 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008261 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008262 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008263 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008264 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008265 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008266 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008268 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 }
8270
Adam Lesinskie08af192015-03-25 16:42:59 -07008271 public void setPowerProfile(PowerProfile profile) {
8272 synchronized (this) {
8273 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008274
8275 // We need to initialize the KernelCpuSpeedReaders to read from
8276 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8277 // information.
8278 final int numClusters = mPowerProfile.getNumCpuClusters();
8279 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8280 int firstCpuOfCluster = 0;
8281 for (int i = 0; i < numClusters; i++) {
8282 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8283 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8284 numSpeedSteps);
8285 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8286 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008287
8288 if (mEstimatedBatteryCapacity == -1) {
8289 // Initialize the estimated battery capacity to a known preset one.
8290 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8291 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008292 }
8293 }
8294
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008295 public void setCallback(BatteryCallback cb) {
8296 mCallback = cb;
8297 }
8298
Amith Yamasanif37447b2009-10-08 18:28:01 -07008299 public void setRadioScanningTimeout(long timeout) {
8300 if (mPhoneSignalScanningTimer != null) {
8301 mPhoneSignalScanningTimer.setTimeout(timeout);
8302 }
8303 }
8304
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008305 public void updateDailyDeadlineLocked() {
8306 // Get the current time.
8307 long currentTime = mDailyStartTime = System.currentTimeMillis();
8308 Calendar calDeadline = Calendar.getInstance();
8309 calDeadline.setTimeInMillis(currentTime);
8310
8311 // Move time up to the next day, ranging from 1am to 3pm.
8312 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8313 calDeadline.set(Calendar.MILLISECOND, 0);
8314 calDeadline.set(Calendar.SECOND, 0);
8315 calDeadline.set(Calendar.MINUTE, 0);
8316 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8317 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8318 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8319 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8320 }
8321
8322 public void recordDailyStatsIfNeededLocked(boolean settled) {
8323 long currentTime = System.currentTimeMillis();
8324 if (currentTime >= mNextMaxDailyDeadline) {
8325 recordDailyStatsLocked();
8326 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8327 recordDailyStatsLocked();
8328 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8329 recordDailyStatsLocked();
8330 }
8331 }
8332
8333 public void recordDailyStatsLocked() {
8334 DailyItem item = new DailyItem();
8335 item.mStartTime = mDailyStartTime;
8336 item.mEndTime = System.currentTimeMillis();
8337 boolean hasData = false;
8338 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8339 hasData = true;
8340 item.mDischargeSteps = new LevelStepTracker(
8341 mDailyDischargeStepTracker.mNumStepDurations,
8342 mDailyDischargeStepTracker.mStepDurations);
8343 }
8344 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8345 hasData = true;
8346 item.mChargeSteps = new LevelStepTracker(
8347 mDailyChargeStepTracker.mNumStepDurations,
8348 mDailyChargeStepTracker.mStepDurations);
8349 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008350 if (mDailyPackageChanges != null) {
8351 hasData = true;
8352 item.mPackageChanges = mDailyPackageChanges;
8353 mDailyPackageChanges = null;
8354 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008355 mDailyDischargeStepTracker.init();
8356 mDailyChargeStepTracker.init();
8357 updateDailyDeadlineLocked();
8358
8359 if (hasData) {
8360 mDailyItems.add(item);
8361 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8362 mDailyItems.remove(0);
8363 }
8364 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8365 try {
8366 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008367 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008368 writeDailyItemsLocked(out);
8369 BackgroundThread.getHandler().post(new Runnable() {
8370 @Override
8371 public void run() {
8372 synchronized (mCheckinFile) {
8373 FileOutputStream stream = null;
8374 try {
8375 stream = mDailyFile.startWrite();
8376 memStream.writeTo(stream);
8377 stream.flush();
8378 FileUtils.sync(stream);
8379 stream.close();
8380 mDailyFile.finishWrite(stream);
8381 } catch (IOException e) {
8382 Slog.w("BatteryStats",
8383 "Error writing battery daily items", e);
8384 mDailyFile.failWrite(stream);
8385 }
8386 }
8387 }
8388 });
8389 } catch (IOException e) {
8390 }
8391 }
8392 }
8393
8394 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8395 StringBuilder sb = new StringBuilder(64);
8396 out.startDocument(null, true);
8397 out.startTag(null, "daily-items");
8398 for (int i=0; i<mDailyItems.size(); i++) {
8399 final DailyItem dit = mDailyItems.get(i);
8400 out.startTag(null, "item");
8401 out.attribute(null, "start", Long.toString(dit.mStartTime));
8402 out.attribute(null, "end", Long.toString(dit.mEndTime));
8403 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8404 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008405 if (dit.mPackageChanges != null) {
8406 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8407 PackageChange pc = dit.mPackageChanges.get(j);
8408 if (pc.mUpdate) {
8409 out.startTag(null, "upd");
8410 out.attribute(null, "pkg", pc.mPackageName);
8411 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8412 out.endTag(null, "upd");
8413 } else {
8414 out.startTag(null, "rem");
8415 out.attribute(null, "pkg", pc.mPackageName);
8416 out.endTag(null, "rem");
8417 }
8418 }
8419 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008420 out.endTag(null, "item");
8421 }
8422 out.endTag(null, "daily-items");
8423 out.endDocument();
8424 }
8425
8426 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
8427 StringBuilder tmpBuilder) throws IOException {
8428 if (steps != null) {
8429 out.startTag(null, tag);
8430 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
8431 for (int i=0; i<steps.mNumStepDurations; i++) {
8432 out.startTag(null, "s");
8433 tmpBuilder.setLength(0);
8434 steps.encodeEntryAt(i, tmpBuilder);
8435 out.attribute(null, "v", tmpBuilder.toString());
8436 out.endTag(null, "s");
8437 }
8438 out.endTag(null, tag);
8439 }
8440 }
8441
8442 public void readDailyStatsLocked() {
8443 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
8444 mDailyItems.clear();
8445 FileInputStream stream;
8446 try {
8447 stream = mDailyFile.openRead();
8448 } catch (FileNotFoundException e) {
8449 return;
8450 }
8451 try {
8452 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008453 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008454 readDailyItemsLocked(parser);
8455 } catch (XmlPullParserException e) {
8456 } finally {
8457 try {
8458 stream.close();
8459 } catch (IOException e) {
8460 }
8461 }
8462 }
8463
8464 private void readDailyItemsLocked(XmlPullParser parser) {
8465 try {
8466 int type;
8467 while ((type = parser.next()) != XmlPullParser.START_TAG
8468 && type != XmlPullParser.END_DOCUMENT) {
8469 ;
8470 }
8471
8472 if (type != XmlPullParser.START_TAG) {
8473 throw new IllegalStateException("no start tag found");
8474 }
8475
8476 int outerDepth = parser.getDepth();
8477 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8478 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8479 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8480 continue;
8481 }
8482
8483 String tagName = parser.getName();
8484 if (tagName.equals("item")) {
8485 readDailyItemTagLocked(parser);
8486 } else {
8487 Slog.w(TAG, "Unknown element under <daily-items>: "
8488 + parser.getName());
8489 XmlUtils.skipCurrentTag(parser);
8490 }
8491 }
8492
8493 } catch (IllegalStateException e) {
8494 Slog.w(TAG, "Failed parsing daily " + e);
8495 } catch (NullPointerException e) {
8496 Slog.w(TAG, "Failed parsing daily " + e);
8497 } catch (NumberFormatException e) {
8498 Slog.w(TAG, "Failed parsing daily " + e);
8499 } catch (XmlPullParserException e) {
8500 Slog.w(TAG, "Failed parsing daily " + e);
8501 } catch (IOException e) {
8502 Slog.w(TAG, "Failed parsing daily " + e);
8503 } catch (IndexOutOfBoundsException e) {
8504 Slog.w(TAG, "Failed parsing daily " + e);
8505 }
8506 }
8507
8508 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8509 XmlPullParserException, IOException {
8510 DailyItem dit = new DailyItem();
8511 String attr = parser.getAttributeValue(null, "start");
8512 if (attr != null) {
8513 dit.mStartTime = Long.parseLong(attr);
8514 }
8515 attr = parser.getAttributeValue(null, "end");
8516 if (attr != null) {
8517 dit.mEndTime = Long.parseLong(attr);
8518 }
8519 int outerDepth = parser.getDepth();
8520 int type;
8521 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8522 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8523 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8524 continue;
8525 }
8526
8527 String tagName = parser.getName();
8528 if (tagName.equals("dis")) {
8529 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8530 } else if (tagName.equals("chg")) {
8531 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008532 } else if (tagName.equals("upd")) {
8533 if (dit.mPackageChanges == null) {
8534 dit.mPackageChanges = new ArrayList<>();
8535 }
8536 PackageChange pc = new PackageChange();
8537 pc.mUpdate = true;
8538 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8539 String verStr = parser.getAttributeValue(null, "ver");
8540 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8541 dit.mPackageChanges.add(pc);
8542 XmlUtils.skipCurrentTag(parser);
8543 } else if (tagName.equals("rem")) {
8544 if (dit.mPackageChanges == null) {
8545 dit.mPackageChanges = new ArrayList<>();
8546 }
8547 PackageChange pc = new PackageChange();
8548 pc.mUpdate = false;
8549 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8550 dit.mPackageChanges.add(pc);
8551 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008552 } else {
8553 Slog.w(TAG, "Unknown element under <item>: "
8554 + parser.getName());
8555 XmlUtils.skipCurrentTag(parser);
8556 }
8557 }
8558 mDailyItems.add(dit);
8559 }
8560
8561 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8562 String tag)
8563 throws NumberFormatException, XmlPullParserException, IOException {
8564 final String numAttr = parser.getAttributeValue(null, "n");
8565 if (numAttr == null) {
8566 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
8567 XmlUtils.skipCurrentTag(parser);
8568 return;
8569 }
8570 final int num = Integer.parseInt(numAttr);
8571 LevelStepTracker steps = new LevelStepTracker(num);
8572 if (isCharge) {
8573 dit.mChargeSteps = steps;
8574 } else {
8575 dit.mDischargeSteps = steps;
8576 }
8577 int i = 0;
8578 int outerDepth = parser.getDepth();
8579 int type;
8580 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8581 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8582 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8583 continue;
8584 }
8585
8586 String tagName = parser.getName();
8587 if ("s".equals(tagName)) {
8588 if (i < num) {
8589 String valueAttr = parser.getAttributeValue(null, "v");
8590 if (valueAttr != null) {
8591 steps.decodeEntryAt(i, valueAttr);
8592 i++;
8593 }
8594 }
8595 } else {
8596 Slog.w(TAG, "Unknown element under <" + tag + ">: "
8597 + parser.getName());
8598 XmlUtils.skipCurrentTag(parser);
8599 }
8600 }
8601 steps.mNumStepDurations = i;
8602 }
8603
8604 @Override
8605 public DailyItem getDailyItemLocked(int daysAgo) {
8606 int index = mDailyItems.size()-1-daysAgo;
8607 return index >= 0 ? mDailyItems.get(index) : null;
8608 }
8609
8610 @Override
8611 public long getCurrentDailyStartTime() {
8612 return mDailyStartTime;
8613 }
8614
8615 @Override
8616 public long getNextMinDailyDeadline() {
8617 return mNextMinDailyDeadline;
8618 }
8619
8620 @Override
8621 public long getNextMaxDailyDeadline() {
8622 return mNextMaxDailyDeadline;
8623 }
8624
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008625 @Override
8626 public boolean startIteratingOldHistoryLocked() {
8627 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8628 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008629 if ((mHistoryIterator = mHistory) == null) {
8630 return false;
8631 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008632 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008633 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008634 mReadOverflow = false;
8635 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008636 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008637 }
8638
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008639 @Override
8640 public boolean getNextOldHistoryLocked(HistoryItem out) {
8641 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
8642 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008643 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008644 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008645 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008646 HistoryItem cur = mHistoryIterator;
8647 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008648 if (!mReadOverflow && !end) {
8649 Slog.w(TAG, "Old history ends before new history!");
8650 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008651 return false;
8652 }
8653 out.setTo(cur);
8654 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008655 if (!mReadOverflow) {
8656 if (end) {
8657 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008658 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008659 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008660 pw.println("Histories differ!");
8661 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008662 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008663 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008664 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8665 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008666 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008667 }
8668 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008669 return true;
8670 }
8671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008672 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008673 public void finishIteratingOldHistoryLocked() {
8674 mIteratingHistory = false;
8675 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008676 mHistoryIterator = null;
8677 }
8678
8679 public int getHistoryTotalSize() {
8680 return MAX_HISTORY_BUFFER;
8681 }
8682
8683 public int getHistoryUsedSize() {
8684 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008685 }
8686
8687 @Override
8688 public boolean startIteratingHistoryLocked() {
8689 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8690 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008691 if (mHistoryBuffer.dataSize() <= 0) {
8692 return false;
8693 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008694 mHistoryBuffer.setDataPosition(0);
8695 mReadOverflow = false;
8696 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008697 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8698 mReadHistoryUids = new int[mHistoryTagPool.size()];
8699 mReadHistoryChars = 0;
8700 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8701 final HistoryTag tag = ent.getKey();
8702 final int idx = ent.getValue();
8703 mReadHistoryStrings[idx] = tag.string;
8704 mReadHistoryUids[idx] = tag.uid;
8705 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008706 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008707 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008708 }
8709
8710 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008711 public int getHistoryStringPoolSize() {
8712 return mReadHistoryStrings.length;
8713 }
8714
8715 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008716 public int getHistoryStringPoolBytes() {
8717 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8718 // Each string character is 2 bytes.
8719 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8720 }
8721
8722 @Override
8723 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008724 return mReadHistoryStrings[index];
8725 }
8726
8727 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008728 public int getHistoryTagPoolUid(int index) {
8729 return mReadHistoryUids[index];
8730 }
8731
8732 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008733 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008734 final int pos = mHistoryBuffer.dataPosition();
8735 if (pos == 0) {
8736 out.clear();
8737 }
8738 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008739 if (end) {
8740 return false;
8741 }
8742
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008743 final long lastRealtime = out.time;
8744 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008745 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008746 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8747 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008748 out.currentTime = lastWalltime + (out.time - lastRealtime);
8749 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008750 return true;
8751 }
8752
8753 @Override
8754 public void finishIteratingHistoryLocked() {
8755 mIteratingHistory = false;
8756 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008757 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008758 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008759
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008760 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008761 public long getHistoryBaseTime() {
8762 return mHistoryBaseTime;
8763 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008764
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008765 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 public int getStartCount() {
8767 return mStartCount;
8768 }
8769
8770 public boolean isOnBattery() {
8771 return mOnBattery;
8772 }
8773
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008774 public boolean isCharging() {
8775 return mCharging;
8776 }
8777
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008778 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008779 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008780 }
8781
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008782 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008783 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008784 mOnBatteryTimeBase.init(uptime, realtime);
8785 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008786 mRealtime = 0;
8787 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008788 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008789 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008790 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008791
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008792 void initDischarge() {
8793 mLowDischargeAmountSinceCharge = 0;
8794 mHighDischargeAmountSinceCharge = 0;
8795 mDischargeAmountScreenOn = 0;
8796 mDischargeAmountScreenOnSinceCharge = 0;
8797 mDischargeAmountScreenOff = 0;
8798 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008799 mDischargeStepTracker.init();
8800 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008801 mDischargeScreenOffCounter.reset(false);
8802 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008803 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008804
8805 public void resetAllStatsCmdLocked() {
8806 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008807 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008808 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008809 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008810 long realtime = mSecRealtime * 1000;
8811 mDischargeStartLevel = mHistoryCur.batteryLevel;
8812 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008813 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008814 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8815 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008816 mOnBatteryTimeBase.reset(uptime, realtime);
8817 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8818 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008819 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008820 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8821 mDischargeScreenOffUnplugLevel = 0;
8822 } else {
8823 mDischargeScreenOnUnplugLevel = 0;
8824 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8825 }
8826 mDischargeAmountScreenOn = 0;
8827 mDischargeAmountScreenOff = 0;
8828 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008829 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008830 }
8831
8832 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008833 final long uptimeMillis = mClocks.uptimeMillis();
8834 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008835 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008836 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008837 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008838 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008839 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008840 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008841
8842 if (mPowerProfile != null) {
8843 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8844 } else {
8845 mEstimatedBatteryCapacity = -1;
8846 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008847 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008848 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008849 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008850 mLongestLightIdleTime = 0;
8851 mLongestFullIdleTime = 0;
8852 mDeviceIdleModeLightTimer.reset(false);
8853 mDeviceIdleModeFullTimer.reset(false);
8854 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008855 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008856 mPhoneOnTimer.reset(false);
8857 mAudioOnTimer.reset(false);
8858 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008859 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008860 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008861 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008862 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008863 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008864 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008865 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008866 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008867 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008868 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008869 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008870 mNetworkByteActivityCounters[i].reset(false);
8871 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008872 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008873 mMobileRadioActiveTimer.reset(false);
8874 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008875 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008876 mMobileRadioActiveUnknownTime.reset(false);
8877 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008878 mWifiOnTimer.reset(false);
8879 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008880 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008881 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008882 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008883 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8884 mWifiSupplStateTimer[i].reset(false);
8885 }
8886 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8887 mWifiSignalStrengthsTimer[i].reset(false);
8888 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008889 mWifiActivity.reset(false);
8890 mBluetoothActivity.reset(false);
8891 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008892 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008893
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008894 for (int i=0; i<mUidStats.size(); i++) {
8895 if (mUidStats.valueAt(i).reset()) {
8896 mUidStats.remove(mUidStats.keyAt(i));
8897 i--;
8898 }
8899 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008900
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008901 if (mKernelWakelockStats.size() > 0) {
8902 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008903 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008904 }
8905 mKernelWakelockStats.clear();
8906 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008907
James Carr3a226052016-07-01 14:49:52 -07008908 if (mKernelMemoryStats.size() > 0) {
8909 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
8910 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
8911 }
8912 mKernelMemoryStats.clear();
8913 }
8914
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008915 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008916 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8917 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008918 }
8919 mWakeupReasonStats.clear();
8920 }
8921
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008922 mLastHistoryStepDetails = null;
8923 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8924 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8925 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8926 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8927 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8928 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8929 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8930 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8931 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8932 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8933
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008934 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008935
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008936 clearHistoryLocked();
8937 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008938
Dianne Hackborn40c87252014-03-19 16:55:40 -07008939 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008940 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008941 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8942 // Not recording process starts/stops.
8943 continue;
8944 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008945 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008946 if (active == null) {
8947 continue;
8948 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008949 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8950 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008951 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008952 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8953 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008954 }
8955 }
8956 }
8957 }
8958
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008959 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008960 if (oldScreenOn) {
8961 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8962 if (diff > 0) {
8963 mDischargeAmountScreenOn += diff;
8964 mDischargeAmountScreenOnSinceCharge += diff;
8965 }
8966 } else {
8967 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8968 if (diff > 0) {
8969 mDischargeAmountScreenOff += diff;
8970 mDischargeAmountScreenOffSinceCharge += diff;
8971 }
8972 }
8973 if (newScreenOn) {
8974 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8975 mDischargeScreenOffUnplugLevel = 0;
8976 } else {
8977 mDischargeScreenOnUnplugLevel = 0;
8978 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8979 }
8980 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008981
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008982 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008983 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008984 final boolean screenOn = mScreenState == Display.STATE_ON;
8985 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008986 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008987 }
8988
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008989 private String[] mMobileIfaces = EmptyArray.STRING;
8990 private String[] mWifiIfaces = EmptyArray.STRING;
8991
8992 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8993
8994 private static final int NETWORK_STATS_LAST = 0;
8995 private static final int NETWORK_STATS_NEXT = 1;
8996 private static final int NETWORK_STATS_DELTA = 2;
8997
Joe Onoratoabded112016-02-08 16:49:39 -08008998 private NetworkStats[] mMobileNetworkStats;
8999 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009000
9001 /**
9002 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
9003 * as a buffer of NetworkStats objects to cycle through when computing deltas.
9004 */
9005 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
9006 NetworkStats[] networkStatsBuffer)
9007 throws IOException {
9008 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
9009 false)) {
9010 return null;
9011 }
9012
9013 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
9014 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
9015 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
9016 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
9017 networkStatsBuffer[NETWORK_STATS_DELTA]);
9018 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
9019 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
9020 return networkStatsBuffer[NETWORK_STATS_DELTA];
9021 }
9022
9023 /**
9024 * Distribute WiFi energy info and network traffic to apps.
9025 * @param info The energy information from the WiFi controller.
9026 */
9027 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009028 if (DEBUG_ENERGY) {
9029 Slog.d(TAG, "Updating wifi stats");
9030 }
9031
Joe Onoratoabded112016-02-08 16:49:39 -08009032 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07009033 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009034 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009035 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
9036 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
9037 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009038 } catch (IOException e) {
9039 Slog.wtf(TAG, "Failed to get wifi network stats", e);
9040 return;
9041 }
9042
9043 if (!mOnBatteryInternal) {
9044 return;
9045 }
9046
Adam Lesinskie08af192015-03-25 16:42:59 -07009047 SparseLongArray rxPackets = new SparseLongArray();
9048 SparseLongArray txPackets = new SparseLongArray();
9049 long totalTxPackets = 0;
9050 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009051 if (delta != null) {
9052 final int size = delta.size();
9053 for (int i = 0; i < size; i++) {
9054 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9055
Adam Lesinskie08af192015-03-25 16:42:59 -07009056 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009057 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07009058 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9059 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009060 }
9061
Adam Lesinski6cca4142016-02-25 18:23:02 -08009062 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9063 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009064 continue;
9065 }
9066
9067 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08009068 if (entry.rxBytes != 0) {
9069 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
9070 entry.rxPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009071 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9072 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
9073 entry.rxPackets);
9074 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009075 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9076 entry.rxBytes);
9077 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9078 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009079
Adam Lesinskiba88e682015-12-08 12:06:55 -08009080 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009081
Adam Lesinskiba88e682015-12-08 12:06:55 -08009082 // Sum the total number of packets so that the Rx Power can
9083 // be evenly distributed amongst the apps.
9084 totalRxPackets += entry.rxPackets;
9085 }
9086
9087 if (entry.txBytes != 0) {
9088 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
9089 entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009090 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9091 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
9092 entry.txPackets);
9093 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009094 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9095 entry.txBytes);
9096 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9097 entry.txPackets);
9098
9099 txPackets.put(u.getUid(), entry.txPackets);
9100
9101 // Sum the total number of packets so that the Tx Power can
9102 // be evenly distributed amongst the apps.
9103 totalTxPackets += entry.txPackets;
9104 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009105 }
9106 }
9107
9108 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009109 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07009110
Adam Lesinskie08af192015-03-25 16:42:59 -07009111 // Measured in mAms
9112 final long txTimeMs = info.getControllerTxTimeMillis();
9113 final long rxTimeMs = info.getControllerRxTimeMillis();
9114 final long idleTimeMs = info.getControllerIdleTimeMillis();
9115 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
9116
9117 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07009118 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009119
9120 if (DEBUG_ENERGY) {
9121 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
9122 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9123 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9124 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
9125 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
9126 }
9127
9128 long totalWifiLockTimeMs = 0;
9129 long totalScanTimeMs = 0;
9130
9131 // On the first pass, collect some totals so that we can normalize power
9132 // calculations if we need to.
9133 final int uidStatsSize = mUidStats.size();
9134 for (int i = 0; i < uidStatsSize; i++) {
9135 final Uid uid = mUidStats.valueAt(i);
9136
9137 // Sum the total scan power for all apps.
Bookatz867c0d72017-03-07 18:23:42 -08009138 totalScanTimeMs += uid.mWifiScanTimer.getMainTimer().getTimeSinceMarkLocked(
Adam Lesinskie08af192015-03-25 16:42:59 -07009139 elapsedRealtimeMs * 1000) / 1000;
9140
9141 // Sum the total time holding wifi lock for all apps.
9142 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9143 elapsedRealtimeMs * 1000) / 1000;
9144 }
9145
9146 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
9147 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
9148 + rxTimeMs + " ms). Normalizing scan time.");
9149 }
Mitchell Willsf9016492015-07-29 17:47:44 -07009150 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
9151 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
9152 + txTimeMs + " ms). Normalizing scan time.");
9153 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009154
9155 // Actually assign and distribute power usage to apps.
9156 for (int i = 0; i < uidStatsSize; i++) {
9157 final Uid uid = mUidStats.valueAt(i);
9158
Bookatz867c0d72017-03-07 18:23:42 -08009159 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getMainTimer().getTimeSinceMarkLocked(
Adam Lesinskie08af192015-03-25 16:42:59 -07009160 elapsedRealtimeMs * 1000) / 1000;
9161 if (scanTimeSinceMarkMs > 0) {
9162 // Set the new mark so that next time we get new data since this point.
9163 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
9164
Mitchell Willsf9016492015-07-29 17:47:44 -07009165 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
9166 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
9167
9168 // Our total scan time is more than the reported Tx/Rx time.
9169 // This is possible because the cost of a scan is approximate.
9170 // Let's normalize the result so that we evenly blame each app
9171 // scanning.
9172 //
9173 // This means that we may have apps that transmitted/received packets not be
9174 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07009175 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07009176 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
9177 totalScanTimeMs;
9178 }
9179 if (totalScanTimeMs > txTimeMs) {
9180 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9181 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009182 }
9183
9184 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07009185 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9186 + scanRxTimeSinceMarkMs + " ms Tx:"
9187 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07009188 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009189
9190 ControllerActivityCounterImpl activityCounter =
9191 uid.getOrCreateWifiControllerActivityLocked();
9192 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9193 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07009194 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9195 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009196 }
9197
9198 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9199 // lock.
9200 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9201 elapsedRealtimeMs * 1000) / 1000;
9202 if (wifiLockTimeSinceMarkMs > 0) {
9203 // Set the new mark so that next time we get new data since this point.
9204 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9205
9206 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9207 / totalWifiLockTimeMs;
9208 if (DEBUG_ENERGY) {
9209 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9210 + myIdleTimeMs + " ms");
9211 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009212 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9213 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009214 }
9215 }
9216
9217 if (DEBUG_ENERGY) {
9218 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07009219 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009220 }
9221
Mitchell Willsf9016492015-07-29 17:47:44 -07009222 // Distribute the remaining Tx power appropriately between all apps that transmitted
9223 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07009224 for (int i = 0; i < txPackets.size(); i++) {
9225 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07009226 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07009227 if (DEBUG_ENERGY) {
9228 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9229 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009230 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9231 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009232 }
9233
9234 // Distribute the remaining Rx power appropriately between all apps that received
9235 // packets.
9236 for (int i = 0; i < rxPackets.size(); i++) {
9237 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9238 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
9239 if (DEBUG_ENERGY) {
9240 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9241 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009242 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9243 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009244 }
9245
9246 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9247
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009248 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009249 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9250 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
9251 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07009252
Adam Lesinski8576cf92015-06-09 12:48:25 -07009253 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9254 final double opVolt = mPowerProfile.getAveragePower(
9255 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9256 if (opVolt != 0) {
9257 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009258 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07009259 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07009260 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009261 }
9262 }
9263
9264 /**
9265 * Distribute Cell radio energy info and network traffic to apps.
9266 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009267 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
9268 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009269 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009270 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009271 }
9272
Adam Lesinskie08af192015-03-25 16:42:59 -07009273 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009274 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009275 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
9276 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
9277 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009278 } catch (IOException e) {
9279 Slog.wtf(TAG, "Failed to get mobile network stats", e);
9280 return;
9281 }
9282
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009283 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009284 return;
9285 }
9286
Adam Lesinskie08af192015-03-25 16:42:59 -07009287 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9288 elapsedRealtimeMs * 1000);
9289 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009290
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009291 long totalRxPackets = 0;
9292 long totalTxPackets = 0;
9293 if (delta != null) {
9294 final int size = delta.size();
9295 for (int i = 0; i < size; i++) {
9296 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08009297 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009298 continue;
9299 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009300
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009301 if (DEBUG_ENERGY) {
9302 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9303 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9304 + " txPackets=" + entry.txPackets);
9305 }
9306
9307 totalRxPackets += entry.rxPackets;
9308 totalTxPackets += entry.txPackets;
9309
9310 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9311 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
9312 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009313 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9314 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
9315 entry.rxBytes, entry.rxPackets);
9316 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
9317 entry.txBytes, entry.txPackets);
9318 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009319
9320 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9321 entry.rxBytes);
9322 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9323 entry.txBytes);
9324 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9325 entry.rxPackets);
9326 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9327 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009328 }
9329
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009330 // Now distribute proportional blame to the apps that did networking.
9331 long totalPackets = totalRxPackets + totalTxPackets;
9332 if (totalPackets > 0) {
9333 for (int i = 0; i < size; i++) {
9334 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9335 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9336 continue;
9337 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009338
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009339 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9340
9341 // Distribute total radio active time in to this app.
9342 final long appPackets = entry.rxPackets + entry.txPackets;
9343 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9344 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9345
9346 // Remove this app from the totals, so that we don't lose any time
9347 // due to rounding.
9348 radioTime -= appRadioTime;
9349 totalPackets -= appPackets;
9350
9351 if (activityInfo != null) {
9352 ControllerActivityCounterImpl activityCounter =
9353 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08009354 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009355 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9356 / totalRxPackets;
9357 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9358 }
9359
Adam Lesinskia4268172016-01-29 12:13:54 -08009360 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009361 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9362 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9363 txMs /= totalTxPackets;
9364 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9365 }
9366 }
9367 }
9368 }
9369 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009370
9371 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009372 // Whoops, there is some radio time we can't blame on an app!
9373 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9374 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009375 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009376 }
9377
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009378 if (activityInfo != null) {
9379 mHasModemReporting = true;
9380 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
9381 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9382 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9383 mModemActivity.getTxTimeCounters()[lvl]
9384 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9385 }
9386
9387 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9388 final double opVolt = mPowerProfile.getAveragePower(
9389 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9390 if (opVolt != 0) {
9391 // We store the power drain as mAms.
9392 mModemActivity.getPowerCounter().addCountLocked(
9393 (long) (activityInfo.getEnergyUsed() / opVolt));
9394 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009395 }
9396 }
9397
9398 /**
9399 * Distribute Bluetooth energy info and network traffic to apps.
9400 * @param info The energy information from the bluetooth controller.
9401 */
9402 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009403 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009404 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009405 }
9406
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009407 if (info == null || !mOnBatteryInternal) {
9408 return;
9409 }
Adam Lesinskie283d332015-04-16 12:29:25 -07009410
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009411 mHasBluetoothReporting = true;
9412
Bookatz867c0d72017-03-07 18:23:42 -08009413 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009414 final long rxTimeMs = info.getControllerRxTimeMillis();
9415 final long txTimeMs = info.getControllerTxTimeMillis();
9416
9417 if (DEBUG_ENERGY) {
9418 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
9419 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9420 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9421 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
9422 }
9423
9424 long totalScanTimeMs = 0;
9425
9426 final int uidCount = mUidStats.size();
9427 for (int i = 0; i < uidCount; i++) {
9428 final Uid u = mUidStats.valueAt(i);
9429 if (u.mBluetoothScanTimer == null) {
9430 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07009431 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009432
Bookatz867c0d72017-03-07 18:23:42 -08009433 totalScanTimeMs += u.mBluetoothScanTimer.getMainTimer().getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009434 elapsedRealtimeMs * 1000) / 1000;
9435 }
9436
9437 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
9438 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
9439
9440 if (DEBUG_ENERGY) {
9441 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
9442 + " TX=" + normalizeScanTxTime);
9443 }
9444
9445 long leftOverRxTimeMs = rxTimeMs;
9446 long leftOverTxTimeMs = txTimeMs;
9447
9448 for (int i = 0; i < uidCount; i++) {
9449 final Uid u = mUidStats.valueAt(i);
9450 if (u.mBluetoothScanTimer == null) {
9451 continue;
9452 }
9453
Bookatz867c0d72017-03-07 18:23:42 -08009454 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getMainTimer().getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009455 elapsedRealtimeMs * 1000) / 1000;
9456 if (scanTimeSinceMarkMs > 0) {
9457 // Set the new mark so that next time we get new data since this point.
9458 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
9459
9460 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
9461 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
9462
9463 if (normalizeScanRxTime) {
9464 // Scan time is longer than the total rx time in the controller,
9465 // so distribute the scan time proportionately. This means regular traffic
9466 // will not blamed, but scans are more expensive anyways.
9467 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
9468 }
9469
9470 if (normalizeScanTxTime) {
9471 // Scan time is longer than the total tx time in the controller,
9472 // so distribute the scan time proportionately. This means regular traffic
9473 // will not blamed, but scans are more expensive anyways.
9474 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
9475 }
9476
9477 final ControllerActivityCounterImpl counter =
9478 u.getOrCreateBluetoothControllerActivityLocked();
9479 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
9480 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
9481
9482 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
9483 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
9484 }
9485 }
9486
9487 if (DEBUG_ENERGY) {
9488 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
9489 + " TX=" + leftOverTxTimeMs);
9490 }
9491
9492 //
9493 // Now distribute blame to apps that did bluetooth traffic.
9494 //
9495
9496 long totalTxBytes = 0;
9497 long totalRxBytes = 0;
9498
9499 final UidTraffic[] uidTraffic = info.getUidTraffic();
9500 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9501 for (int i = 0; i < numUids; i++) {
9502 final UidTraffic traffic = uidTraffic[i];
9503
9504 // Add to the global counters.
9505 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9506 traffic.getRxBytes());
9507 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9508 traffic.getTxBytes());
9509
9510 // Add to the UID counters.
9511 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9512 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9513 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9514
9515 // Calculate the total traffic.
9516 totalTxBytes += traffic.getTxBytes();
9517 totalRxBytes += traffic.getRxBytes();
9518 }
9519
9520 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9521 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009522 for (int i = 0; i < numUids; i++) {
9523 final UidTraffic traffic = uidTraffic[i];
9524
Adam Lesinski50e47602015-12-04 17:04:54 -08009525 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009526 final ControllerActivityCounterImpl counter =
9527 u.getOrCreateBluetoothControllerActivityLocked();
9528
9529 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
9530 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9531
9532 if (DEBUG_ENERGY) {
9533 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
9534 + " rx_time=" + timeRxMs);
9535 }
9536 counter.getRxTimeCounter().addCountLocked(timeRxMs);
9537 leftOverRxTimeMs -= timeRxMs;
9538 }
9539
9540 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
9541 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
9542
9543 if (DEBUG_ENERGY) {
9544 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
9545 + " tx_time=" + timeTxMs);
9546 }
9547
9548 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
9549 leftOverTxTimeMs -= timeTxMs;
9550 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009551 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009552 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009553
9554 mBluetoothActivity.getRxTimeCounter().addCountLocked(
9555 info.getControllerRxTimeMillis());
9556 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
9557 info.getControllerTxTimeMillis());
9558 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
9559 info.getControllerIdleTimeMillis());
9560
9561 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9562 final double opVolt = mPowerProfile.getAveragePower(
9563 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9564 if (opVolt != 0) {
9565 // We store the power drain as mAms.
9566 mBluetoothActivity.getPowerCounter().addCountLocked(
9567 (long) (info.getControllerEnergyUsed() / opVolt));
9568 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009569 }
9570
9571 /**
9572 * Read and distribute kernel wake lock use across apps.
9573 */
9574 public void updateKernelWakelocksLocked() {
9575 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
9576 mTmpWakelockStats);
9577 if (wakelockStats == null) {
9578 // Not crashing might make board bringup easier.
9579 Slog.w(TAG, "Couldn't get kernel wake lock stats");
9580 return;
9581 }
9582
9583 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
9584 String name = ent.getKey();
9585 KernelWakelockStats.Entry kws = ent.getValue();
9586
9587 SamplingTimer kwlt = mKernelWakelockStats.get(name);
9588 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009589 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009590 mKernelWakelockStats.put(name, kwlt);
9591 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07009592
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009593 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009594 kwlt.setUpdateVersion(kws.mVersion);
9595 }
9596
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009597 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07009598 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
9599 // this time.
9600 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9601 SamplingTimer st = ent.getValue();
9602 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
9603 st.endSample();
9604 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009605 }
9606 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009607
Adam Lesinskid84ad302016-05-17 18:31:02 -07009608 // Record whether we've seen a non-zero time (for debugging b/22716723).
9609 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009610 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
9611 }
9612
9613 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
9614 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
9615 wakelockStats.kernelWakelockVersion);
9616 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009617 }
9618
Adam Lesinski72478f02015-06-17 15:39:43 -07009619 // We use an anonymous class to access these variables,
9620 // so they can't live on the stack or they'd have to be
9621 // final MutableLong objects (more allocations).
9622 // Used in updateCpuTimeLocked().
9623 long mTempTotalCpuUserTimeUs;
9624 long mTempTotalCpuSystemTimeUs;
9625
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009626 /**
James Carr3a226052016-07-01 14:49:52 -07009627 * Reads the newest memory stats from the kernel.
9628 */
9629 public void updateKernelMemoryBandwidthLocked() {
9630 mKernelMemoryBandwidthStats.updateStats();
9631 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
9632 final int bandwidthEntryCount = bandwidthEntries.size();
9633 int index;
9634 for (int i = 0; i < bandwidthEntryCount; i++) {
9635 SamplingTimer timer;
9636 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
9637 timer = mKernelMemoryStats.valueAt(index);
9638 } else {
9639 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
9640 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
9641 }
9642 timer.update(bandwidthEntries.valueAt(i), 1);
9643 if (DEBUG_MEMORY) {
9644 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
9645 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
9646 mKernelMemoryStats.get(
9647 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
9648 mKernelMemoryStats.size()));
9649 }
9650 }
9651 }
9652
9653 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07009654 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
9655 * and we are on battery with screen off, we give more of the cpu time to those apps holding
9656 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009657 */
Adam Lesinski72478f02015-06-17 15:39:43 -07009658 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009659 if (mPowerProfile == null) {
9660 return;
9661 }
9662
Adam Lesinski72478f02015-06-17 15:39:43 -07009663 if (DEBUG_ENERGY_CPU) {
9664 Slog.d(TAG, "!Cpu updating!");
9665 }
9666
9667 // Holding a wakelock costs more than just using the cpu.
9668 // Currently, we assign only half the cpu time to an app that is running but
9669 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
9670 // If no app is holding a wakelock, then the distribution is normal.
9671 final int wakelockWeight = 50;
9672
Adam Lesinski6832f392015-09-05 18:05:40 -07009673 // Read the time spent for each cluster at various cpu frequencies.
9674 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
9675 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
9676 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
9677 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009678
9679 int numWakelocks = 0;
9680
9681 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
9682 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
9683 final int numPartialTimers = mPartialTimers.size();
9684 if (mOnBatteryScreenOffTimeBase.isRunning()) {
9685 for (int i = 0; i < numPartialTimers; i++) {
9686 final StopwatchTimer timer = mPartialTimers.get(i);
9687 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9688 // Since the collection and blaming of wakelocks can be scheduled to run after
9689 // some delay, the mPartialTimers list may have new entries. We can't blame
9690 // the newly added timer for past cpu time, so we only consider timers that
9691 // were present for one round of collection. Once a timer has gone through
9692 // a round of collection, its mInList field is set to true.
9693 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009694 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009695 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009696 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009697
9698 final int numWakelocksF = numWakelocks;
9699 mTempTotalCpuUserTimeUs = 0;
9700 mTempTotalCpuSystemTimeUs = 0;
9701
9702 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9703 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9704 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009705 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009706 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9707 new KernelUidCpuTimeReader.Callback() {
9708 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009709 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
9710 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009711 final Uid u = getUidStatsLocked(mapUid(uid));
9712
9713 // Accumulate the total system and user time.
9714 mTempTotalCpuUserTimeUs += userTimeUs;
9715 mTempTotalCpuSystemTimeUs += systemTimeUs;
9716
9717 StringBuilder sb = null;
9718 if (DEBUG_ENERGY_CPU) {
9719 sb = new StringBuilder();
9720 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9721 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9722 sb.append(" s=");
9723 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009724 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009725 }
9726
9727 if (numWakelocksF > 0) {
9728 // We have wakelocks being held, so only give a portion of the
9729 // time to the process. The rest will be distributed among wakelock
9730 // holders.
9731 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9732 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9733 }
9734
9735 if (sb != null) {
9736 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9737 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9738 sb.append(" s=");
9739 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009740 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009741 Slog.d(TAG, sb.toString());
9742 }
9743
9744 u.mUserCpuTime.addCountLocked(userTimeUs);
9745 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009746 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009747
9748 // Add the cpu speeds to this UID. These are used as a ratio
9749 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009750 final int numClusters = mPowerProfile.getNumCpuClusters();
9751 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9752 numClusters) {
9753 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009754 }
9755
9756 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009757 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9758 cluster);
9759 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9760 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009761 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009762 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009763 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009764
9765 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9766 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9767 if (cpuSpeeds[speed] == null) {
9768 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9769 }
9770 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9771 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009772 }
9773 }
9774 });
9775
Amith Yamasanid2450862017-02-07 15:58:24 -08009776 // TODO: STOPSHIP, remove the "true" below after b/34961340 is fixed
9777 if (DEBUG_ENERGY_CPU || true) {
Joe Onoratoabded112016-02-08 16:49:39 -08009778 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009779 " ms");
9780 }
9781
9782 if (mOnBatteryInternal && numWakelocks > 0) {
9783 // Distribute a portion of the total cpu time to wakelock holders.
9784 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9785 mTempTotalCpuSystemTimeUs =
9786 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9787
9788 for (int i = 0; i < numPartialTimers; i++) {
9789 final StopwatchTimer timer = mPartialTimers.get(i);
9790
9791 // The system does not share any blame, as it is usually holding the wakelock
9792 // on behalf of an app.
9793 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9794 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9795 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9796
9797 if (DEBUG_ENERGY_CPU) {
9798 StringBuilder sb = new StringBuilder();
9799 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9800 .append(": u=");
9801 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9802 sb.append(" s=");
9803 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9804 Slog.d(TAG, sb.toString());
9805 }
9806
9807 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9808 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9809
9810 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009811 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009812
9813 mTempTotalCpuUserTimeUs -= userTimeUs;
9814 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9815 numWakelocks--;
9816 }
9817 }
9818
9819 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9820 // Anything left over is given to the system.
9821 if (DEBUG_ENERGY_CPU) {
9822 StringBuilder sb = new StringBuilder();
9823 sb.append(" Distributing lost time to system: u=");
9824 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9825 sb.append(" s=");
9826 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9827 Slog.d(TAG, sb.toString());
9828 }
9829
9830 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9831 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9832 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9833
9834 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009835 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9836 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009837 }
9838 }
9839
9840 // See if there is a difference in wakelocks between this collection and the last
9841 // collection.
9842 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9843 // No difference, so each timer is now considered for the next collection.
9844 for (int i = 0; i < numPartialTimers; i++) {
9845 mPartialTimers.get(i).mInList = true;
9846 }
9847 } else {
9848 // The lists are different, meaning we added (or removed a timer) since the last
9849 // collection.
9850 final int numLastPartialTimers = mLastPartialTimers.size();
9851 for (int i = 0; i < numLastPartialTimers; i++) {
9852 mLastPartialTimers.get(i).mInList = false;
9853 }
9854 mLastPartialTimers.clear();
9855
9856 // Mark the current timers as gone through a collection.
9857 for (int i = 0; i < numPartialTimers; i++) {
9858 final StopwatchTimer timer = mPartialTimers.get(i);
9859 timer.mInList = true;
9860 mLastPartialTimers.add(timer);
9861 }
9862 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009863 }
9864
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009865 boolean setChargingLocked(boolean charging) {
9866 if (mCharging != charging) {
9867 mCharging = charging;
9868 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009869 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009870 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009871 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009872 }
9873 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9874 return true;
9875 }
9876 return false;
9877 }
9878
Dianne Hackborn40c87252014-03-19 16:55:40 -07009879 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009880 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009881 boolean doWrite = false;
9882 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9883 m.arg1 = onBattery ? 1 : 0;
9884 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009885
Dianne Hackborn40c87252014-03-19 16:55:40 -07009886 final long uptime = mSecUptime * 1000;
9887 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009888 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009889 if (onBattery) {
9890 // We will reset our status if we are unplugging after the
9891 // battery was last full, or the level is at 100, or
9892 // we have gone through a significant charge (from a very low
9893 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009894 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009895 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009896 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009897 || (mDischargeCurrentLevel < 20 && level >= 80)
9898 || (getHighDischargeAmountSinceCharge() >= 200
9899 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009900 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009901 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009902 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9903 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009904 // Before we write, collect a snapshot of the final aggregated
9905 // stats to be reported in the next checkin. Only do this if we have
9906 // a sufficient amount of data to make it interesting.
9907 if (getLowDischargeAmountSinceCharge() >= 20) {
9908 final Parcel parcel = Parcel.obtain();
9909 writeSummaryToParcel(parcel, true);
9910 BackgroundThread.getHandler().post(new Runnable() {
9911 @Override public void run() {
9912 synchronized (mCheckinFile) {
9913 FileOutputStream stream = null;
9914 try {
9915 stream = mCheckinFile.startWrite();
9916 stream.write(parcel.marshall());
9917 stream.flush();
9918 FileUtils.sync(stream);
9919 stream.close();
9920 mCheckinFile.finishWrite(stream);
9921 } catch (IOException e) {
9922 Slog.w("BatteryStats",
9923 "Error writing checkin battery statistics", e);
9924 mCheckinFile.failWrite(stream);
9925 } finally {
9926 parcel.recycle();
9927 }
9928 }
9929 }
9930 });
9931 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009932 doWrite = true;
9933 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -08009934 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009935 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -08009936 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009937 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009938 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009939 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009940 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009941 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009942 if (mCharging) {
9943 setChargingLocked(false);
9944 }
9945 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009946 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009947 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009948 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009949 mDischargeStepTracker.clearTime();
9950 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009951 mInitStepMode = mCurStepMode;
9952 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009953 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009954 mHistoryCur.batteryLevel = (byte)level;
9955 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9956 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9957 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009958 if (reset) {
9959 mRecordingHistory = true;
9960 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9961 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009962 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009963 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009964 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009965 mDischargeScreenOnUnplugLevel = level;
9966 mDischargeScreenOffUnplugLevel = 0;
9967 } else {
9968 mDischargeScreenOnUnplugLevel = 0;
9969 mDischargeScreenOffUnplugLevel = level;
9970 }
9971 mDischargeAmountScreenOn = 0;
9972 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009973 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009974 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009975 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009976 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009977 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009978 mHistoryCur.batteryLevel = (byte)level;
9979 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9980 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9981 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009982 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009983 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009984 if (level < mDischargeUnplugLevel) {
9985 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9986 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9987 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009988 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9989 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009990 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009991 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009992 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009993 mInitStepMode = mCurStepMode;
9994 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009995 }
9996 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9997 if (mFile != null) {
9998 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 }
10000 }
10001 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010002
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010003 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
10004 boolean reset) {
10005 mRecordingHistory = true;
10006 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070010007 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
10008 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010009 mHistoryCur);
10010 mHistoryCur.currentTime = 0;
10011 if (reset) {
10012 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
10013 }
10014 }
10015
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010016 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
10017 final long uptimeMs) {
10018 if (mRecordingHistory) {
10019 mHistoryCur.currentTime = currentTime;
10020 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
10021 mHistoryCur);
10022 mHistoryCur.currentTime = 0;
10023 }
10024 }
10025
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010026 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
10027 if (mRecordingHistory) {
10028 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010029 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
10030 mHistoryCur);
10031 mHistoryCur.currentTime = 0;
10032 }
10033 }
10034
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010035 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010036 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010037 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010038 }
10039 }
10040
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010041 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010042 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010043
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010044 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -080010045 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010046 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080010047 final long uptime = mClocks.uptimeMillis();
10048 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010049 if (!mHaveBatteryLevel) {
10050 mHaveBatteryLevel = true;
10051 // We start out assuming that the device is plugged in (not
10052 // on battery). If our first report is now that we are indeed
10053 // plugged in, then twiddle our state to correctly reflect that
10054 // since we won't be going through the full setOnBattery().
10055 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010056 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010057 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010058 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010059 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010060 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010061 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010062 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010063 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010064 mHistoryCur.batteryStatus = (byte)status;
10065 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010066 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010067 mMaxChargeStepLevel = mMinDischargeStepLevel =
10068 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010069 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010070 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
10071 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
10072 }
10073 int oldStatus = mHistoryCur.batteryStatus;
10074 if (onBattery) {
10075 mDischargeCurrentLevel = level;
10076 if (!mRecordingHistory) {
10077 mRecordingHistory = true;
10078 startRecordingHistory(elapsedRealtime, uptime, true);
10079 }
10080 } else if (level < 96) {
10081 if (!mRecordingHistory) {
10082 mRecordingHistory = true;
10083 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010084 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010085 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010086 mCurrentBatteryLevel = level;
10087 if (mDischargePlugLevel < 0) {
10088 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070010089 }
Adam Lesinski926969b2016-04-28 17:31:12 -070010090
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010091 if (onBattery != mOnBattery) {
10092 mHistoryCur.batteryLevel = (byte)level;
10093 mHistoryCur.batteryStatus = (byte)status;
10094 mHistoryCur.batteryHealth = (byte)health;
10095 mHistoryCur.batteryPlugType = (byte)plugType;
10096 mHistoryCur.batteryTemperature = (short)temp;
10097 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010098 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10099 // Only record discharges
10100 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10101 mDischargeCounter.addCountLocked(chargeDiff);
10102 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10103 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010104 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010105 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010106 } else {
10107 boolean changed = false;
10108 if (mHistoryCur.batteryLevel != level) {
10109 mHistoryCur.batteryLevel = (byte)level;
10110 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070010111
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010112 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
10113 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010114 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070010115 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010116 if (mHistoryCur.batteryStatus != status) {
10117 mHistoryCur.batteryStatus = (byte)status;
10118 changed = true;
10119 }
10120 if (mHistoryCur.batteryHealth != health) {
10121 mHistoryCur.batteryHealth = (byte)health;
10122 changed = true;
10123 }
10124 if (mHistoryCur.batteryPlugType != plugType) {
10125 mHistoryCur.batteryPlugType = (byte)plugType;
10126 changed = true;
10127 }
10128 if (temp >= (mHistoryCur.batteryTemperature+10)
10129 || temp <= (mHistoryCur.batteryTemperature-10)) {
10130 mHistoryCur.batteryTemperature = (short)temp;
10131 changed = true;
10132 }
10133 if (volt > (mHistoryCur.batteryVoltage+20)
10134 || volt < (mHistoryCur.batteryVoltage-20)) {
10135 mHistoryCur.batteryVoltage = (char)volt;
10136 changed = true;
10137 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010138 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
10139 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010140 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10141 // Only record discharges
10142 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10143 mDischargeCounter.addCountLocked(chargeDiff);
10144 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10145 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010146 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070010147 changed = true;
10148 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010149 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
10150 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
10151 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
10152 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010153 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010154 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
10155 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10156 modeBits, elapsedRealtime);
10157 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10158 modeBits, elapsedRealtime);
10159 mLastDischargeStepLevel = level;
10160 mMinDischargeStepLevel = level;
10161 mInitStepMode = mCurStepMode;
10162 mModStepMode = 0;
10163 }
10164 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010165 if (level >= 90) {
10166 // If the battery level is at least 90%, always consider the device to be
10167 // charging even if it happens to go down a level.
10168 changed |= setChargingLocked(true);
10169 mLastChargeStepLevel = level;
10170 } if (!mCharging) {
10171 if (mLastChargeStepLevel < level) {
10172 // We have not reporting that we are charging, but the level has now
10173 // gone up, so consider the state to be charging.
10174 changed |= setChargingLocked(true);
10175 mLastChargeStepLevel = level;
10176 }
10177 } else {
10178 if (mLastChargeStepLevel > level) {
10179 // We had reported that the device was charging, but here we are with
10180 // power connected and the level going down. Looks like the current
10181 // power supplied isn't enough, so consider the device to now be
10182 // discharging.
10183 changed |= setChargingLocked(false);
10184 mLastChargeStepLevel = level;
10185 }
10186 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010187 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
10188 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10189 modeBits, elapsedRealtime);
10190 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10191 modeBits, elapsedRealtime);
10192 mLastChargeStepLevel = level;
10193 mMaxChargeStepLevel = level;
10194 mInitStepMode = mCurStepMode;
10195 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070010196 }
10197 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010198 if (changed) {
10199 addHistoryRecordLocked(elapsedRealtime, uptime);
10200 }
Evan Millarc64edde2009-04-18 12:26:32 -070010201 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010202 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10203 // We don't record history while we are plugged in and fully charged.
10204 // The next time we are unplugged, history will be cleared.
10205 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010206 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010207
10208 if (differsByMoreThan(chargeFullUAh, mLastChargeFullUAh, 100)) {
10209 mLastChargeFullUAh = chargeFullUAh;
10210 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ESTIMATED_BATTERY_CAP,
10211 "", chargeFullUAh / 1000);
10212 }
10213 }
10214
10215 private static boolean differsByMoreThan(int left, int right, int diff) {
10216 return Math.abs(left - right) > diff;
Adam Lesinski33dac552015-03-09 15:24:48 -070010217 }
10218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 public long getAwakeTimeBattery() {
10220 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
10221 }
10222
10223 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080010224 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 }
10226
10227 @Override
10228 public long computeUptime(long curTime, int which) {
10229 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010230 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010232 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233 }
10234 return 0;
10235 }
10236
10237 @Override
10238 public long computeRealtime(long curTime, int which) {
10239 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010240 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010242 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 }
10244 return 0;
10245 }
10246
10247 @Override
10248 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010249 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 }
10251
10252 @Override
10253 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010254 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010255 }
10256
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010257 @Override
10258 public long computeBatteryScreenOffUptime(long curTime, int which) {
10259 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
10260 }
10261
10262 @Override
10263 public long computeBatteryScreenOffRealtime(long curTime, int which) {
10264 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 }
10266
Dianne Hackborn260c5022014-04-29 11:23:16 -070010267 private long computeTimePerLevel(long[] steps, int numSteps) {
10268 // For now we'll do a simple average across all steps.
10269 if (numSteps <= 0) {
10270 return -1;
10271 }
10272 long total = 0;
10273 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010274 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010275 }
10276 return total / numSteps;
10277 /*
10278 long[] buckets = new long[numSteps];
10279 int numBuckets = 0;
10280 int numToAverage = 4;
10281 int i = 0;
10282 while (i < numSteps) {
10283 long totalTime = 0;
10284 int num = 0;
10285 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010286 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010287 num++;
10288 }
10289 buckets[numBuckets] = totalTime / num;
10290 numBuckets++;
10291 numToAverage *= 2;
10292 i += num;
10293 }
10294 if (numBuckets < 1) {
10295 return -1;
10296 }
10297 long averageTime = buckets[numBuckets-1];
10298 for (i=numBuckets-2; i>=0; i--) {
10299 averageTime = (averageTime + buckets[i]) / 2;
10300 }
10301 return averageTime;
10302 */
10303 }
10304
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010305 @Override
10306 public long computeBatteryTimeRemaining(long curTime) {
10307 if (!mOnBattery) {
10308 return -1;
10309 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010310 /* Simple implementation just looks at the average discharge per level across the
10311 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010312 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
10313 if (discharge < 2) {
10314 return -1;
10315 }
10316 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
10317 if (duration < 1000*1000) {
10318 return -1;
10319 }
10320 long usPerLevel = duration/discharge;
10321 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010322 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010323 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010324 return -1;
10325 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010326 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010327 if (msPerLevel <= 0) {
10328 return -1;
10329 }
10330 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010331 }
10332
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010333 @Override
10334 public LevelStepTracker getDischargeLevelStepTracker() {
10335 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010336 }
10337
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010338 @Override
10339 public LevelStepTracker getDailyDischargeLevelStepTracker() {
10340 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010341 }
10342
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010343 @Override
10344 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010345 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010346 // Not yet working.
10347 return -1;
10348 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010349 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010350 int curLevel = mCurrentBatteryLevel;
10351 int plugLevel = mDischargePlugLevel;
10352 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
10353 return -1;
10354 }
10355 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
10356 if (duration < 1000*1000) {
10357 return -1;
10358 }
10359 long usPerLevel = duration/(curLevel-plugLevel);
10360 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070010361 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010362 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010363 return -1;
10364 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010365 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010366 if (msPerLevel <= 0) {
10367 return -1;
10368 }
10369 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010370 }
10371
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010372 @Override
10373 public LevelStepTracker getChargeLevelStepTracker() {
10374 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010375 }
10376
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010377 @Override
10378 public LevelStepTracker getDailyChargeLevelStepTracker() {
10379 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010380 }
10381
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010382 @Override
10383 public ArrayList<PackageChange> getDailyPackageChanges() {
10384 return mDailyPackageChanges;
10385 }
10386
Joe Onoratoe1acd632016-02-23 13:25:10 -080010387 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010388 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 }
10390
10391 @Override
10392 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010393 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 }
10395
10396 @Override
10397 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010398 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070010400
The Android Open Source Project10592532009-03-18 17:39:46 -070010401 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010402 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010403 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010404 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010405 }
10406 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010407
Evan Millar633a1742009-04-02 16:36:33 -070010408 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010409 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010410 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010411
The Android Open Source Project10592532009-03-18 17:39:46 -070010412 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010413 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010414 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010415 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010416 }
10417 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010418
Evan Millar633a1742009-04-02 16:36:33 -070010419 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010420 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422
Amith Yamasanie43530a2009-08-21 13:11:37 -070010423 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010424 public int getLowDischargeAmountSinceCharge() {
10425 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010426 int val = mLowDischargeAmountSinceCharge;
10427 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10428 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
10429 }
10430 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010431 }
10432 }
10433
10434 @Override
10435 public int getHighDischargeAmountSinceCharge() {
10436 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010437 int val = mHighDischargeAmountSinceCharge;
10438 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10439 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
10440 }
10441 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010442 }
10443 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010444
10445 @Override
10446 public int getDischargeAmount(int which) {
10447 int dischargeAmount = which == STATS_SINCE_CHARGED
10448 ? getHighDischargeAmountSinceCharge()
10449 : (getDischargeStartLevel() - getDischargeCurrentLevel());
10450 if (dischargeAmount < 0) {
10451 dischargeAmount = 0;
10452 }
10453 return dischargeAmount;
10454 }
10455
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010456 public int getDischargeAmountScreenOn() {
10457 synchronized(this) {
10458 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010459 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010460 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10461 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10462 }
10463 return val;
10464 }
10465 }
10466
10467 public int getDischargeAmountScreenOnSinceCharge() {
10468 synchronized(this) {
10469 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010470 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010471 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10472 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10473 }
10474 return val;
10475 }
10476 }
10477
10478 public int getDischargeAmountScreenOff() {
10479 synchronized(this) {
10480 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010481 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010482 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10483 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10484 }
10485 return val;
10486 }
10487 }
10488
10489 public int getDischargeAmountScreenOffSinceCharge() {
10490 synchronized(this) {
10491 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010492 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010493 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10494 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10495 }
10496 return val;
10497 }
10498 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010500 /**
10501 * Retrieve the statistics object for a particular uid, creating if needed.
10502 */
10503 public Uid getUidStatsLocked(int uid) {
10504 Uid u = mUidStats.get(uid);
10505 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080010506 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 mUidStats.put(uid, u);
10508 }
10509 return u;
10510 }
10511
10512 /**
10513 * Remove the statistics object for a particular uid.
10514 */
10515 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070010516 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 mUidStats.remove(uid);
10518 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070010519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 /**
10521 * Retrieve the statistics object for a particular process, creating
10522 * if needed.
10523 */
10524 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010525 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 Uid u = getUidStatsLocked(uid);
10527 return u.getProcessStatsLocked(name);
10528 }
10529
10530 /**
10531 * Retrieve the statistics object for a particular process, creating
10532 * if needed.
10533 */
10534 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010535 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 Uid u = getUidStatsLocked(uid);
10537 return u.getPackageStatsLocked(pkg);
10538 }
10539
10540 /**
10541 * Retrieve the statistics object for a particular service, creating
10542 * if needed.
10543 */
10544 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010545 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 Uid u = getUidStatsLocked(uid);
10547 return u.getServiceStatsLocked(pkg, name);
10548 }
10549
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010550 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010551 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010552 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010553 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010554 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010555
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010556 Parcel mPendingWrite = null;
10557 final ReentrantLock mWriteLock = new ReentrantLock();
10558
10559 public void writeAsyncLocked() {
10560 writeLocked(false);
10561 }
10562
10563 public void writeSyncLocked() {
10564 writeLocked(true);
10565 }
10566
10567 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010568 if (mFile == null) {
10569 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 return;
10571 }
10572
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010573 if (mShuttingDown) {
10574 return;
10575 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010576
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010577 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010578 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080010579 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010580
10581 if (mPendingWrite != null) {
10582 mPendingWrite.recycle();
10583 }
10584 mPendingWrite = out;
10585
10586 if (sync) {
10587 commitPendingDataToDisk();
10588 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010589 BackgroundThread.getHandler().post(new Runnable() {
10590 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010591 commitPendingDataToDisk();
10592 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010593 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010594 }
10595 }
10596
10597 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010598 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010599 synchronized (this) {
10600 next = mPendingWrite;
10601 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010602 if (next == null) {
10603 return;
10604 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010605 }
10606
Amith Yamasanid2450862017-02-07 15:58:24 -080010607 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010609 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010610 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070010612 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010614 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010616 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010617 mFile.rollback();
10618 } finally {
10619 next.recycle();
10620 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070010621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622 }
10623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010625 if (mDailyFile != null) {
10626 readDailyStatsLocked();
10627 }
10628
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010629 if (mFile == null) {
10630 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631 return;
10632 }
10633
10634 mUidStats.clear();
10635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010637 File file = mFile.chooseForRead();
10638 if (!file.exists()) {
10639 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010641 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010643 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 Parcel in = Parcel.obtain();
10645 in.unmarshall(raw, 0, raw.length);
10646 in.setDataPosition(0);
10647 stream.close();
10648
10649 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080010650 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010651 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010652 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010654
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010655 mEndPlatformVersion = Build.ID;
10656
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010657 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010658 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080010659 final long elapsedRealtime = mClocks.elapsedRealtime();
10660 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010661 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010662 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010663 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010664 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
10665 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010666 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010667
10668 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010669 }
10670
10671 public int describeContents() {
10672 return 0;
10673 }
10674
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010675 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070010676 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010677
10678 mHistoryBuffer.setDataSize(0);
10679 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010680 mHistoryTagPool.clear();
10681 mNextHistoryTagIdx = 0;
10682 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010683
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010684 int numTags = in.readInt();
10685 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080010686 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010687 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010688 if (str == null) {
10689 throw new ParcelFormatException("null history tag string");
10690 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010691 int uid = in.readInt();
10692 HistoryTag tag = new HistoryTag();
10693 tag.string = str;
10694 tag.uid = uid;
10695 tag.poolIdx = idx;
10696 mHistoryTagPool.put(tag, idx);
10697 if (idx >= mNextHistoryTagIdx) {
10698 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010699 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010700 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010701 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010702
10703 int bufSize = in.readInt();
10704 int curPos = in.dataPosition();
10705 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010706 throw new ParcelFormatException("File corrupt: history data buffer too large " +
10707 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010708 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010709 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
10710 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010711 } else {
10712 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
10713 + " bytes at " + curPos);
10714 mHistoryBuffer.appendFrom(in, curPos, bufSize);
10715 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010716 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010717
Dianne Hackbornae384452011-06-28 12:33:48 -070010718 if (andOldHistory) {
10719 readOldHistory(in);
10720 }
10721
10722 if (DEBUG_HISTORY) {
10723 StringBuilder sb = new StringBuilder(128);
10724 sb.append("****************** OLD mHistoryBaseTime: ");
10725 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10726 Slog.i(TAG, sb.toString());
10727 }
10728 mHistoryBaseTime = historyBaseTime;
10729 if (DEBUG_HISTORY) {
10730 StringBuilder sb = new StringBuilder(128);
10731 sb.append("****************** NEW mHistoryBaseTime: ");
10732 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10733 Slog.i(TAG, sb.toString());
10734 }
10735
10736 // We are just arbitrarily going to insert 1 minute from the sample of
10737 // the last run until samples in this run.
10738 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080010739 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010740 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070010741 if (DEBUG_HISTORY) {
10742 StringBuilder sb = new StringBuilder(128);
10743 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
10744 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10745 Slog.i(TAG, sb.toString());
10746 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070010747 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010748 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010749
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010750 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010751 if (!USE_OLD_HISTORY) {
10752 return;
10753 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010754 mHistory = mHistoryEnd = mHistoryCache = null;
10755 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010756 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010757 HistoryItem rec = new HistoryItem(time, in);
10758 addHistoryRecordLocked(rec);
10759 }
10760 }
10761
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010762 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010763 if (DEBUG_HISTORY) {
10764 StringBuilder sb = new StringBuilder(128);
10765 sb.append("****************** WRITING mHistoryBaseTime: ");
10766 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010767 sb.append(" mLastHistoryElapsedRealtime: ");
10768 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010769 Slog.i(TAG, sb.toString());
10770 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010771 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010772 if (!inclData) {
10773 out.writeInt(0);
10774 out.writeInt(0);
10775 return;
10776 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010777 out.writeInt(mHistoryTagPool.size());
10778 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10779 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010780 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010781 out.writeString(tag.string);
10782 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010783 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010784 out.writeInt(mHistoryBuffer.dataSize());
10785 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10786 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10787 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010788
10789 if (andOldHistory) {
10790 writeOldHistory(out);
10791 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010792 }
10793
10794 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010795 if (!USE_OLD_HISTORY) {
10796 return;
10797 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010798 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010799 while (rec != null) {
10800 if (rec.time >= 0) rec.writeToParcel(out, 0);
10801 rec = rec.next;
10802 }
10803 out.writeLong(-1);
10804 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010805
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010806 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010807 final int version = in.readInt();
10808 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010809 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 + ", expected " + VERSION + "; erasing old stats");
10811 return;
10812 }
10813
Dianne Hackbornae384452011-06-28 12:33:48 -070010814 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010816 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010819 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010820 mStartPlatformVersion = in.readString();
10821 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010822 mOnBatteryTimeBase.readSummaryFromParcel(in);
10823 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010824 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010825 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010826 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010827 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010828 mEstimatedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010829 mLowDischargeAmountSinceCharge = in.readInt();
10830 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010831 mDischargeAmountScreenOnSinceCharge = in.readInt();
10832 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010833 mDischargeStepTracker.readFromParcel(in);
10834 mChargeStepTracker.readFromParcel(in);
10835 mDailyDischargeStepTracker.readFromParcel(in);
10836 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010837 mDischargeCounter.readSummaryFromParcelLocked(in);
10838 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010839 int NPKG = in.readInt();
10840 if (NPKG > 0) {
10841 mDailyPackageChanges = new ArrayList<>(NPKG);
10842 while (NPKG > 0) {
10843 NPKG--;
10844 PackageChange pc = new PackageChange();
10845 pc.mPackageName = in.readString();
10846 pc.mUpdate = in.readInt() != 0;
10847 pc.mVersionCode = in.readInt();
10848 mDailyPackageChanges.add(pc);
10849 }
10850 } else {
10851 mDailyPackageChanges = null;
10852 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010853 mDailyStartTime = in.readLong();
10854 mNextMinDailyDeadline = in.readLong();
10855 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010857 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010858
Jeff Browne95c3cd2014-05-02 16:59:26 -070010859 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010861 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10862 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10863 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010864 mInteractive = false;
10865 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010866 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010867 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010868 mLongestLightIdleTime = in.readLong();
10869 mLongestFullIdleTime = in.readLong();
10870 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10871 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10872 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010873 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010874 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010875 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010876 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10877 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010878 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010879 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10880 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10881 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010882 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010883 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10884 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010885 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010886 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010887 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010888 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010889 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010890 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10891 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010892 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010893 mWifiOn = false;
10894 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010895 mGlobalWifiRunning = false;
10896 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010897 for (int i=0; i<NUM_WIFI_STATES; i++) {
10898 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10899 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010900 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10901 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10902 }
10903 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10904 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10905 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010906 mWifiActivity.readSummaryFromParcel(in);
10907 mBluetoothActivity.readSummaryFromParcel(in);
10908 mModemActivity.readSummaryFromParcel(in);
10909 mHasWifiReporting = in.readInt() != 0;
10910 mHasBluetoothReporting = in.readInt() != 0;
10911 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010912
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010913 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010914 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010915 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010916 mCameraOnNesting = 0;
10917 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010918 mBluetoothScanNesting = 0;
10919 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920
Evan Millarc64edde2009-04-18 12:26:32 -070010921 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010922 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010923 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010924 }
Evan Millarc64edde2009-04-18 12:26:32 -070010925 for (int ikw = 0; ikw < NKW; ikw++) {
10926 if (in.readInt() != 0) {
10927 String kwltName = in.readString();
10928 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10929 }
10930 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010931
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010932 int NWR = in.readInt();
10933 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010934 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010935 }
10936 for (int iwr = 0; iwr < NWR; iwr++) {
10937 if (in.readInt() != 0) {
10938 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010939 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010940 }
10941 }
10942
James Carr3a226052016-07-01 14:49:52 -070010943 int NMS = in.readInt();
10944 for (int ims = 0; ims < NMS; ims++) {
10945 if (in.readInt() != 0) {
10946 long kmstName = in.readLong();
10947 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
10948 }
10949 }
10950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010951 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010952 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010953 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 for (int iu = 0; iu < NU; iu++) {
10956 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010957 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 mUidStats.put(uid, u);
10959
Bookatz867c0d72017-03-07 18:23:42 -080010960 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
10961
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010962 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010963 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010964 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010965 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010966 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010967 if (in.readInt() != 0) {
10968 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10969 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010970 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010971 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010972 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010973 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010974 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10975 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10976 if (in.readInt() != 0) {
10977 u.makeWifiBatchedScanBin(i, null);
10978 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10979 }
10980 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010981 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010982 if (in.readInt() != 0) {
10983 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10984 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010985 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010986 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010987 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010988 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010989 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10990 }
10991 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010992 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10993 }
10994 if (in.readInt() != 0) {
10995 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10996 }
10997 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010998 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10999 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011000 if (in.readInt() != 0) {
11001 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
11002 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080011003 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070011004 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11005 if (in.readInt() != 0) {
11006 u.makeProcessState(i, null);
11007 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
11008 }
11009 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011010 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011011 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011012 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011013
Dianne Hackborn617f8772009-03-31 15:04:46 -070011014 if (in.readInt() != 0) {
11015 if (u.mUserActivityCounters == null) {
11016 u.initUserActivityLocked();
11017 }
11018 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11019 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
11020 }
11021 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011022
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011023 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011024 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011025 u.initNetworkActivityLocked();
11026 }
11027 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011028 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11029 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011030 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011031 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
11032 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011033 }
11034
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011035 u.mUserCpuTime.readSummaryFromParcelLocked(in);
11036 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070011037 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011038
Adam Lesinski6832f392015-09-05 18:05:40 -070011039 if (in.readInt() != 0) {
11040 final int numClusters = in.readInt();
11041 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
11042 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011043 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011044
11045 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
11046 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070011047 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070011048 final int NSB = in.readInt();
11049 if (mPowerProfile != null &&
11050 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
11051 throw new ParcelFormatException("File corrupt: too many speed bins " +
11052 NSB);
11053 }
11054
Adam Lesinski6832f392015-09-05 18:05:40 -070011055 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
11056 for (int speed = 0; speed < NSB; speed++) {
11057 if (in.readInt() != 0) {
11058 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
11059 mOnBatteryTimeBase);
11060 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
11061 }
11062 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070011063 } else {
11064 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070011065 }
11066 }
11067 } else {
11068 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011069 }
11070
Adam Lesinski5f056f62016-07-14 16:56:08 -070011071 if (in.readInt() != 0) {
11072 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11073 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
11074 } else {
11075 u.mMobileRadioApWakeupCount = null;
11076 }
11077
11078 if (in.readInt() != 0) {
11079 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11080 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
11081 } else {
11082 u.mWifiRadioApWakeupCount = null;
11083 }
11084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011085 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011086 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011087 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089 for (int iw = 0; iw < NW; iw++) {
11090 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011091 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011092 }
11093
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011094 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011095 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011096 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011097 }
11098 for (int is = 0; is < NS; is++) {
11099 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011100 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011101 }
11102
11103 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011104 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011105 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011106 }
11107 for (int ij = 0; ij < NJ; ij++) {
11108 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011109 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011110 }
11111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011113 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011114 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 for (int is = 0; is < NP; is++) {
11117 int seNumber = in.readInt();
11118 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080011119 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 }
11121 }
11122
11123 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011124 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011125 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 for (int ip = 0; ip < NP; ip++) {
11128 String procName = in.readString();
11129 Uid.Proc p = u.getProcessStatsLocked(procName);
11130 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011132 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011133 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011134 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
11135 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011136 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011137 }
11138
11139 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011140 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011141 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 for (int ip = 0; ip < NP; ip++) {
11144 String pkgName = in.readString();
11145 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011146 final int NWA = in.readInt();
11147 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011148 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011149 }
11150 p.mWakeupAlarms.clear();
11151 for (int iwa=0; iwa<NWA; iwa++) {
11152 String tag = in.readString();
11153 Counter c = new Counter(mOnBatteryTimeBase);
11154 c.readSummaryFromParcelLocked(in);
11155 p.mWakeupAlarms.put(tag, c);
11156 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011157 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011158 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011159 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 for (int is = 0; is < NS; is++) {
11162 String servName = in.readString();
11163 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
11164 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011165 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011166 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011167 }
11168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169 }
11170 }
11171
11172 /**
11173 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
11174 * disk. This format does not allow a lossless round-trip.
11175 *
11176 * @param out the Parcel to be written to.
11177 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011178 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011179 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011180
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011181 // Pull the clock time. This may update the time and make a new history entry
11182 // if we had originally pulled a time before the RTC was set.
11183 long startClockTime = getStartClockTime();
11184
Joe Onoratoabded112016-02-08 16:49:39 -080011185 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
11186 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187
11188 out.writeInt(VERSION);
11189
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011190 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011192 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011193 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011194 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011195 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011196 out.writeString(mStartPlatformVersion);
11197 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011198 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11199 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011200 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011201 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011202 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011203 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011204 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011205 out.writeInt(getLowDischargeAmountSinceCharge());
11206 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011207 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11208 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011209 mDischargeStepTracker.writeToParcel(out);
11210 mChargeStepTracker.writeToParcel(out);
11211 mDailyDischargeStepTracker.writeToParcel(out);
11212 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011213 mDischargeCounter.writeSummaryFromParcelLocked(out);
11214 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011215 if (mDailyPackageChanges != null) {
11216 final int NPKG = mDailyPackageChanges.size();
11217 out.writeInt(NPKG);
11218 for (int i=0; i<NPKG; i++) {
11219 PackageChange pc = mDailyPackageChanges.get(i);
11220 out.writeString(pc.mPackageName);
11221 out.writeInt(pc.mUpdate ? 1 : 0);
11222 out.writeInt(pc.mVersionCode);
11223 }
11224 } else {
11225 out.writeInt(0);
11226 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011227 out.writeLong(mDailyStartTime);
11228 out.writeLong(mNextMinDailyDeadline);
11229 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011230
11231 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011232 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011233 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011234 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011235 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011236 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011237 out.writeLong(mLongestLightIdleTime);
11238 out.writeLong(mLongestFullIdleTime);
11239 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11240 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11241 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011242 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011243 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011244 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011245 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011246 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011247 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011248 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011249 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011250 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011251 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011252 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11253 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011254 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011255 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11256 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011257 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011258 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11259 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011260 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11261 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011262 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011263 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011264 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011265 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11266 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11267 }
11268 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11269 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11270 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011271 mWifiActivity.writeSummaryToParcel(out);
11272 mBluetoothActivity.writeSummaryToParcel(out);
11273 mModemActivity.writeSummaryToParcel(out);
11274 out.writeInt(mHasWifiReporting ? 1 : 0);
11275 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11276 out.writeInt(mHasModemReporting ? 1 : 0);
11277
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011278 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011279 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011280 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011281 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011282
Evan Millarc64edde2009-04-18 12:26:32 -070011283 out.writeInt(mKernelWakelockStats.size());
11284 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11285 Timer kwlt = ent.getValue();
11286 if (kwlt != null) {
11287 out.writeInt(1);
11288 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011289 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11290 } else {
11291 out.writeInt(0);
11292 }
11293 }
11294
11295 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011296 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11297 SamplingTimer timer = ent.getValue();
11298 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011299 out.writeInt(1);
11300 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011301 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070011302 } else {
11303 out.writeInt(0);
11304 }
11305 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011306
James Carr3a226052016-07-01 14:49:52 -070011307 out.writeInt(mKernelMemoryStats.size());
11308 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11309 Timer kmt = mKernelMemoryStats.valueAt(i);
11310 if (kmt != null) {
11311 out.writeInt(1);
11312 out.writeLong(mKernelMemoryStats.keyAt(i));
11313 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11314 } else {
11315 out.writeInt(0);
11316 }
11317 }
11318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011319 final int NU = mUidStats.size();
11320 out.writeInt(NU);
11321 for (int iu = 0; iu < NU; iu++) {
11322 out.writeInt(mUidStats.keyAt(iu));
11323 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011324
Bookatz867c0d72017-03-07 18:23:42 -080011325 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11326
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011327 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011328 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011329 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011330 } else {
11331 out.writeInt(0);
11332 }
11333 if (u.mFullWifiLockTimer != null) {
11334 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011335 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011336 } else {
11337 out.writeInt(0);
11338 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011339 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011340 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011341 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011342 } else {
11343 out.writeInt(0);
11344 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011345 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11346 if (u.mWifiBatchedScanTimer[i] != null) {
11347 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011348 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070011349 } else {
11350 out.writeInt(0);
11351 }
11352 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011353 if (u.mWifiMulticastTimer != null) {
11354 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011355 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011356 } else {
11357 out.writeInt(0);
11358 }
11359 if (u.mAudioTurnedOnTimer != null) {
11360 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011361 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011362 } else {
11363 out.writeInt(0);
11364 }
11365 if (u.mVideoTurnedOnTimer != null) {
11366 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011367 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011368 } else {
11369 out.writeInt(0);
11370 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011371 if (u.mFlashlightTurnedOnTimer != null) {
11372 out.writeInt(1);
11373 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11374 } else {
11375 out.writeInt(0);
11376 }
11377 if (u.mCameraTurnedOnTimer != null) {
11378 out.writeInt(1);
11379 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11380 } else {
11381 out.writeInt(0);
11382 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011383 if (u.mForegroundActivityTimer != null) {
11384 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011385 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011386 } else {
11387 out.writeInt(0);
11388 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011389 if (u.mBluetoothScanTimer != null) {
11390 out.writeInt(1);
11391 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11392 } else {
11393 out.writeInt(0);
11394 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070011395 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11396 if (u.mProcessStateTimer[i] != null) {
11397 out.writeInt(1);
11398 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11399 } else {
11400 out.writeInt(0);
11401 }
11402 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011403 if (u.mVibratorOnTimer != null) {
11404 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011405 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011406 } else {
11407 out.writeInt(0);
11408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011409
Dianne Hackborn617f8772009-03-31 15:04:46 -070011410 if (u.mUserActivityCounters == null) {
11411 out.writeInt(0);
11412 } else {
11413 out.writeInt(1);
11414 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11415 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
11416 }
11417 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011418
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011419 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011420 out.writeInt(0);
11421 } else {
11422 out.writeInt(1);
11423 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011424 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11425 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011426 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011427 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
11428 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011429 }
11430
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011431 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
11432 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070011433 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011434
Adam Lesinski6832f392015-09-05 18:05:40 -070011435 if (u.mCpuClusterSpeed != null) {
11436 out.writeInt(1);
11437 out.writeInt(u.mCpuClusterSpeed.length);
11438 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
11439 if (cpuSpeeds != null) {
11440 out.writeInt(1);
11441 out.writeInt(cpuSpeeds.length);
11442 for (LongSamplingCounter c : cpuSpeeds) {
11443 if (c != null) {
11444 out.writeInt(1);
11445 c.writeSummaryFromParcelLocked(out);
11446 } else {
11447 out.writeInt(0);
11448 }
11449 }
11450 } else {
11451 out.writeInt(0);
11452 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011453 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011454 } else {
11455 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011456 }
11457
Adam Lesinski5f056f62016-07-14 16:56:08 -070011458 if (u.mMobileRadioApWakeupCount != null) {
11459 out.writeInt(1);
11460 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11461 } else {
11462 out.writeInt(0);
11463 }
11464
11465 if (u.mWifiRadioApWakeupCount != null) {
11466 out.writeInt(1);
11467 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11468 } else {
11469 out.writeInt(0);
11470 }
11471
Dianne Hackbornd953c532014-08-16 18:17:38 -070011472 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
11473 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011474 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011475 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011476 out.writeString(wakeStats.keyAt(iw));
11477 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011478 if (wl.mTimerFull != null) {
11479 out.writeInt(1);
11480 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11481 } else {
11482 out.writeInt(0);
11483 }
11484 if (wl.mTimerPartial != null) {
11485 out.writeInt(1);
11486 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11487 } else {
11488 out.writeInt(0);
11489 }
11490 if (wl.mTimerWindow != null) {
11491 out.writeInt(1);
11492 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11493 } else {
11494 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011495 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011496 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070011497 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011498 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070011499 } else {
11500 out.writeInt(0);
11501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011502 }
11503
Dianne Hackbornd953c532014-08-16 18:17:38 -070011504 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
11505 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011506 out.writeInt(NS);
11507 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011508 out.writeString(syncStats.keyAt(is));
11509 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011510 }
11511
Dianne Hackbornd953c532014-08-16 18:17:38 -070011512 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
11513 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011514 out.writeInt(NJ);
11515 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011516 out.writeString(jobStats.keyAt(ij));
11517 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011518 }
11519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011520 int NSE = u.mSensorStats.size();
11521 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011522 for (int ise=0; ise<NSE; ise++) {
11523 out.writeInt(u.mSensorStats.keyAt(ise));
11524 Uid.Sensor se = u.mSensorStats.valueAt(ise);
11525 if (se.mTimer != null) {
11526 out.writeInt(1);
11527 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11528 } else {
11529 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011530 }
11531 }
11532
11533 int NP = u.mProcessStats.size();
11534 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011535 for (int ip=0; ip<NP; ip++) {
11536 out.writeString(u.mProcessStats.keyAt(ip));
11537 Uid.Proc ps = u.mProcessStats.valueAt(ip);
11538 out.writeLong(ps.mUserTime);
11539 out.writeLong(ps.mSystemTime);
11540 out.writeLong(ps.mForegroundTime);
11541 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011542 out.writeInt(ps.mNumCrashes);
11543 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011544 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011545 }
11546
11547 NP = u.mPackageStats.size();
11548 out.writeInt(NP);
11549 if (NP > 0) {
11550 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
11551 : u.mPackageStats.entrySet()) {
11552 out.writeString(ent.getKey());
11553 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011554 final int NWA = ps.mWakeupAlarms.size();
11555 out.writeInt(NWA);
11556 for (int iwa=0; iwa<NWA; iwa++) {
11557 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
11558 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
11559 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011560 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011562 for (int is=0; is<NS; is++) {
11563 out.writeString(ps.mServiceStats.keyAt(is));
11564 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
11565 long time = ss.getStartTimeToNowLocked(
11566 mOnBatteryTimeBase.getUptime(NOW_SYS));
11567 out.writeLong(time);
11568 out.writeInt(ss.mStarts);
11569 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011570 }
11571 }
11572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011573 }
11574 }
11575
11576 public void readFromParcel(Parcel in) {
11577 readFromParcelLocked(in);
11578 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 void readFromParcelLocked(Parcel in) {
11581 int magic = in.readInt();
11582 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011583 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011584 }
11585
Dianne Hackbornae384452011-06-28 12:33:48 -070011586 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011588 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011589 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011590 mStartPlatformVersion = in.readString();
11591 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 mUptime = in.readLong();
11593 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011594 mRealtime = in.readLong();
11595 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011596 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011597 mEstimatedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011598 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011599 mOnBatteryTimeBase.readFromParcel(in);
11600 mOnBatteryScreenOffTimeBase.readFromParcel(in);
11601
Jeff Browne95c3cd2014-05-02 16:59:26 -070011602 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080011603 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011604 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011605 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
11606 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011607 }
Dianne Hackborn29325132014-05-21 15:01:03 -070011608 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011609 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011610 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011611 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
11612 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011613 mLongestLightIdleTime = in.readLong();
11614 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080011615 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
11616 mOnBatteryTimeBase, in);
11617 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
11618 mOnBatteryTimeBase, in);
11619 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
11620 mOnBatteryTimeBase, in);
11621 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
11622 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011623 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011624 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011625 null, mOnBatteryTimeBase, in);
11626 }
Joe Onoratoabded112016-02-08 16:49:39 -080011627 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
11628 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011629 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011630 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011631 null, mOnBatteryTimeBase, in);
11632 }
11633 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11634 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11635 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11636 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011637 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080011638 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
11639 mOnBatteryTimeBase, in);
11640 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
11641 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011642 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011643 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
11644 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011645 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011646 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011647 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011648 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011649 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
11650 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011651 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011652 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011653 null, mOnBatteryTimeBase, in);
11654 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011655 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011656 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011657 null, mOnBatteryTimeBase, in);
11658 }
11659 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011660 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011661 null, mOnBatteryTimeBase, in);
11662 }
Adam Lesinski33dac552015-03-09 15:24:48 -070011663
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011664 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11665 NUM_WIFI_TX_LEVELS, in);
11666 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11667 NUM_BT_TX_LEVELS, in);
11668 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11669 ModemActivityInfo.TX_POWER_LEVELS, in);
11670 mHasWifiReporting = in.readInt() != 0;
11671 mHasBluetoothReporting = in.readInt() != 0;
11672 mHasModemReporting = in.readInt() != 0;
11673
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011674 mNumConnectivityChange = in.readInt();
11675 mLoadedNumConnectivityChange = in.readInt();
11676 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011677 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011678 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011679 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011680 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011681 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011682 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011683 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011684 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011685 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011686 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011687 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011688 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011689 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011690 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011691 mLowDischargeAmountSinceCharge = in.readInt();
11692 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011693 mDischargeAmountScreenOn = in.readInt();
11694 mDischargeAmountScreenOnSinceCharge = in.readInt();
11695 mDischargeAmountScreenOff = in.readInt();
11696 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011697 mDischargeStepTracker.readFromParcel(in);
11698 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011699 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
11700 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 mLastWriteTime = in.readLong();
11702
Evan Millarc64edde2009-04-18 12:26:32 -070011703 mKernelWakelockStats.clear();
11704 int NKW = in.readInt();
11705 for (int ikw = 0; ikw < NKW; ikw++) {
11706 if (in.readInt() != 0) {
11707 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011708 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070011709 mKernelWakelockStats.put(wakelockName, kwlt);
11710 }
11711 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011712
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011713 mWakeupReasonStats.clear();
11714 int NWR = in.readInt();
11715 for (int iwr = 0; iwr < NWR; iwr++) {
11716 if (in.readInt() != 0) {
11717 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011718 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011719 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011720 }
11721 }
11722
James Carr3a226052016-07-01 14:49:52 -070011723 mKernelMemoryStats.clear();
11724 int nmt = in.readInt();
11725 for (int imt = 0; imt < nmt; imt++) {
11726 if (in.readInt() != 0) {
11727 Long bucket = in.readLong();
11728 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
11729 mKernelMemoryStats.put(bucket, kmt);
11730 }
11731 }
11732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011733 mPartialTimers.clear();
11734 mFullTimers.clear();
11735 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011736 mWifiRunningTimers.clear();
11737 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070011738 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070011739 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011740 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011741 mAudioTurnedOnTimers.clear();
11742 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011743 mFlashlightTurnedOnTimers.clear();
11744 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745
11746 int numUids = in.readInt();
11747 mUidStats.clear();
11748 for (int i = 0; i < numUids; i++) {
11749 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011750 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011751 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011752 mUidStats.append(uid, u);
11753 }
11754 }
11755
11756 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011757 writeToParcelLocked(out, true, flags);
11758 }
11759
11760 public void writeToParcelWithoutUids(Parcel out, int flags) {
11761 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011763
11764 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011765 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011766 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011767 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011768
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011769 // Pull the clock time. This may update the time and make a new history entry
11770 // if we had originally pulled a time before the RTC was set.
11771 long startClockTime = getStartClockTime();
11772
Joe Onoratoabded112016-02-08 16:49:39 -080011773 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11774 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011775 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11776 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011779
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011780 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011782 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011783 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011784 out.writeString(mStartPlatformVersion);
11785 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011786 out.writeLong(mUptime);
11787 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011788 out.writeLong(mRealtime);
11789 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011790 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011791 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011792 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11793 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11794
11795 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11796 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11797 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11798 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011799 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011800 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011801 out.writeLong(mLongestLightIdleTime);
11802 out.writeLong(mLongestFullIdleTime);
11803 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11804 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11805 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011806 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011807 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11808 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11809 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11810 }
11811 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11812 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11813 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11814 }
11815 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11816 mNetworkByteActivityCounters[i].writeToParcel(out);
11817 mNetworkPacketActivityCounters[i].writeToParcel(out);
11818 }
11819 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11820 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011821 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011822 mMobileRadioActiveUnknownTime.writeToParcel(out);
11823 mMobileRadioActiveUnknownCount.writeToParcel(out);
11824 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11825 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11826 for (int i=0; i<NUM_WIFI_STATES; i++) {
11827 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11828 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011829 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11830 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11831 }
11832 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11833 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11834 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011835 mWifiActivity.writeToParcel(out, 0);
11836 mBluetoothActivity.writeToParcel(out, 0);
11837 mModemActivity.writeToParcel(out, 0);
11838 out.writeInt(mHasWifiReporting ? 1 : 0);
11839 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11840 out.writeInt(mHasModemReporting ? 1 : 0);
11841
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011842 out.writeInt(mNumConnectivityChange);
11843 out.writeInt(mLoadedNumConnectivityChange);
11844 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011845 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011846 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011847 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011848 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011849 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011850 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011851 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011852 out.writeInt(mLowDischargeAmountSinceCharge);
11853 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011854 out.writeInt(mDischargeAmountScreenOn);
11855 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11856 out.writeInt(mDischargeAmountScreenOff);
11857 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011858 mDischargeStepTracker.writeToParcel(out);
11859 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011860 mDischargeCounter.writeToParcel(out);
11861 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011862 out.writeLong(mLastWriteTime);
11863
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011864 if (inclUids) {
11865 out.writeInt(mKernelWakelockStats.size());
11866 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11867 SamplingTimer kwlt = ent.getValue();
11868 if (kwlt != null) {
11869 out.writeInt(1);
11870 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011871 kwlt.writeToParcel(out, uSecRealtime);
11872 } else {
11873 out.writeInt(0);
11874 }
11875 }
11876 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011877 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11878 SamplingTimer timer = ent.getValue();
11879 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011880 out.writeInt(1);
11881 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011882 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011883 } else {
11884 out.writeInt(0);
11885 }
Evan Millarc64edde2009-04-18 12:26:32 -070011886 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011887 } else {
11888 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011889 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011890
James Carr3a226052016-07-01 14:49:52 -070011891 out.writeInt(mKernelMemoryStats.size());
11892 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11893 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
11894 if (kmt != null) {
11895 out.writeInt(1);
11896 out.writeLong(mKernelMemoryStats.keyAt(i));
11897 kmt.writeToParcel(out, uSecRealtime);
11898 } else {
11899 out.writeInt(0);
11900 }
11901 }
11902
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011903 if (inclUids) {
11904 int size = mUidStats.size();
11905 out.writeInt(size);
11906 for (int i = 0; i < size; i++) {
11907 out.writeInt(mUidStats.keyAt(i));
11908 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011909
Bookatz867c0d72017-03-07 18:23:42 -080011910 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011911 }
11912 } else {
11913 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011914 }
11915 }
11916
11917 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11918 new Parcelable.Creator<BatteryStatsImpl>() {
11919 public BatteryStatsImpl createFromParcel(Parcel in) {
11920 return new BatteryStatsImpl(in);
11921 }
11922
11923 public BatteryStatsImpl[] newArray(int size) {
11924 return new BatteryStatsImpl[size];
11925 }
11926 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011927
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011928 public void prepareForDumpLocked() {
11929 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011930 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011931
11932 // Pull the clock time. This may update the time and make a new history entry
11933 // if we had originally pulled a time before the RTC was set.
11934 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011935 }
11936
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011937 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011938 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011939 pw.println("mOnBatteryTimeBase:");
11940 mOnBatteryTimeBase.dump(pw, " ");
11941 pw.println("mOnBatteryScreenOffTimeBase:");
11942 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011943 Printer pr = new PrintWriterPrinter(pw);
11944 pr.println("*** Screen timer:");
11945 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011946 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011947 pr.println("*** Screen brightness #" + i + ":");
11948 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011949 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011950 pr.println("*** Interactive timer:");
11951 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011952 pr.println("*** Power save mode timer:");
11953 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011954 pr.println("*** Device idle mode light timer:");
11955 mDeviceIdleModeLightTimer.logState(pr, " ");
11956 pr.println("*** Device idle mode full timer:");
11957 mDeviceIdleModeFullTimer.logState(pr, " ");
11958 pr.println("*** Device light idling timer:");
11959 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011960 pr.println("*** Device idling timer:");
11961 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011962 pr.println("*** Phone timer:");
11963 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011964 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011965 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011966 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011967 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011968 pr.println("*** Signal scanning :");
11969 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011970 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011971 pr.println("*** Data connection type #" + i + ":");
11972 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011973 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011974 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011975 pr.println("*** Mobile network active timer:");
11976 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011977 pr.println("*** Mobile network active adjusted timer:");
11978 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011979 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011980 pr.println("*** Wifi timer:");
11981 mWifiOnTimer.logState(pr, " ");
11982 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011983 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011984 for (int i=0; i<NUM_WIFI_STATES; i++) {
11985 pr.println("*** Wifi state #" + i + ":");
11986 mWifiStateTimer[i].logState(pr, " ");
11987 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011988 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11989 pr.println("*** Wifi suppl state #" + i + ":");
11990 mWifiSupplStateTimer[i].logState(pr, " ");
11991 }
11992 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11993 pr.println("*** Wifi signal strength #" + i + ":");
11994 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11995 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011996 pr.println("*** Flashlight timer:");
11997 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011998 pr.println("*** Camera timer:");
11999 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012000 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012001 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012003}