blob: 3bb20b4ad63726991cce6c16a3a190ae3cb134c9 [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
Sudheer Shanka9b735c52017-05-09 18:26:18 -070019import android.annotation.NonNull;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070020import android.annotation.Nullable;
Dianne Hackborn61659e52014-07-09 16:13:01 -070021import android.app.ActivityManager;
Adam Lesinski33dac552015-03-09 15:24:48 -070022import android.bluetooth.BluetoothActivityEnergyInfo;
Adam Lesinski50e47602015-12-04 17:04:54 -080023import android.bluetooth.UidTraffic;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080024import android.content.Context;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070025import android.content.Intent;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070026import android.net.ConnectivityManager;
27import android.net.NetworkStats;
Adam Lesinski33dac552015-03-09 15:24:48 -070028import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn3251b902014-06-20 14:40:53 -070029import android.net.wifi.WifiManager;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070030import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.BatteryStats;
Dianne Hackborncd0e3352014-08-07 17:08:09 -070032import android.os.Build;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070033import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070034import android.os.Handler;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080035import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6f357d32014-01-15 20:40:55 -080036import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070037import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.Parcel;
39import android.os.ParcelFormatException;
40import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070041import android.os.Process;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.SystemClock;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080045import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070046import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070047import android.telephony.DataConnectionRealTimeInfo;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080048import android.telephony.ModemActivityInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070049import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070050import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070051import android.telephony.TelephonyManager;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070052import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070053import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070055import android.util.LogWriter;
James Carr3a226052016-07-01 14:49:52 -070056import android.util.LongSparseArray;
57import android.util.LongSparseLongArray;
Dianne Hackbornd953c532014-08-16 18:17:38 -070058import android.util.MutableInt;
Adam Lesinski14ae39a2017-05-26 11:50:40 -070059import android.util.Pools;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070060import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070062import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080064import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070065import android.util.SparseLongArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070066import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080067import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070068import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Adam Lesinski14ae39a2017-05-26 11:50:40 -070070import com.android.internal.annotations.GuardedBy;
Adam Lesinski98f0d462016-04-19 16:46:20 -070071import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070072import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080073import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070074import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080075import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070076import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080077import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070078import com.android.server.NetworkManagementSocketTagger;
79import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080080import org.xmlpull.v1.XmlPullParser;
81import org.xmlpull.v1.XmlPullParserException;
82import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070083
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080084import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.io.File;
86import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080087import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.io.FileOutputStream;
89import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070090import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010091import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.util.ArrayList;
Sudheer Shanka9b735c52017-05-09 18:26:18 -070093import java.util.Arrays;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080094import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070096import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070098import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070099import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101/**
102 * All information we are collecting about things that can happen that impact
103 * battery life. All times are represented in microseconds except where indicated
104 * otherwise.
105 */
Joe Onoratoabded112016-02-08 16:49:39 -0800106public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800108 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700109 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800110 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
James Carr3a226052016-07-01 14:49:52 -0700111 private static final boolean DEBUG_MEMORY = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700112 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700113 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700114
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700115 // TODO: remove "tcp" from network methods, since we measure total stats.
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700118 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 // Current on-disk Parcel version
Bookatz5b5ec322017-05-26 09:40:38 -0700121 private static final int VERSION = 159 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700122
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700123 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700124 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700125
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700126 // No, really, THIS is the maximum number of items we will record in the history.
127 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
128
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800129 // The maximum number of names wakelocks we will keep track of
130 // per uid; once the limit is reached, we batch the remaining wakelocks
131 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700132 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700133
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800134 // Number of transmit power states the Wifi controller can be in.
135 private static final int NUM_WIFI_TX_LEVELS = 1;
136
137 // Number of transmit power states the Bluetooth controller can be in.
138 private static final int NUM_BT_TX_LEVELS = 1;
139
Joe Onoratoabded112016-02-08 16:49:39 -0800140 protected Clocks mClocks;
141
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700142 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700143 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800144 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700146 static final int MSG_UPDATE_WAKELOCKS = 1;
147 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700148 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700149 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700150
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700151 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
152 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
153
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700154 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700155 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700156 private final KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader =
157 new KernelUidCpuFreqTimeReader();
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700158
James Carr3a226052016-07-01 14:49:52 -0700159 private final KernelMemoryBandwidthStats mKernelMemoryBandwidthStats
160 = new KernelMemoryBandwidthStats();
161 private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
162 public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
163 return mKernelMemoryStats;
164 }
165
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700166 public interface BatteryCallback {
167 public void batteryNeedsCpuUpdate();
168 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700169 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700170 }
171
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700172 public interface PlatformIdleStateCallback {
173 public String getPlatformLowPowerStats();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000174 public String getSubsystemLowPowerStats();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700175 }
176
177 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
178
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700179 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800180 public MyHandler(Looper looper) {
181 super(looper, null, true);
182 }
183
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700184 @Override
185 public void handleMessage(Message msg) {
186 BatteryCallback cb = mCallback;
187 switch (msg.what) {
188 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700189 synchronized (BatteryStatsImpl.this) {
Sudheer Shanka671985f2017-05-19 11:33:42 -0700190 updateCpuTimeLocked(false /* updateCpuFreqData */);
Adam Lesinski72478f02015-06-17 15:39:43 -0700191 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700192 if (cb != null) {
193 cb.batteryNeedsCpuUpdate();
194 }
195 break;
196 case MSG_REPORT_POWER_CHANGE:
197 if (cb != null) {
198 cb.batteryPowerChanged(msg.arg1 != 0);
199 }
200 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700201 case MSG_REPORT_CHARGING:
202 if (cb != null) {
203 final String action;
204 synchronized (BatteryStatsImpl.this) {
205 action = mCharging ? BatteryManager.ACTION_CHARGING
206 : BatteryManager.ACTION_DISCHARGING;
207 }
208 Intent intent = new Intent(action);
209 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
210 cb.batterySendBroadcast(intent);
211 }
212 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700213 }
214 }
215 }
216
Joe Onoratoabded112016-02-08 16:49:39 -0800217 public interface Clocks {
218 public long elapsedRealtime();
219 public long uptimeMillis();
220 }
221
222 public static class SystemClocks implements Clocks {
223 public long elapsedRealtime() {
224 return SystemClock.elapsedRealtime();
225 }
226
227 public long uptimeMillis() {
228 return SystemClock.uptimeMillis();
229 }
230 }
231
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700232 public interface ExternalStatsSync {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700233 int UPDATE_CPU = 0x01;
234 int UPDATE_WIFI = 0x02;
235 int UPDATE_RADIO = 0x04;
236 int UPDATE_BT = 0x08;
237 int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800238
239 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700240 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700241 }
242
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700243 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700244 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700245
246 private BatteryCallback mCallback;
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800249 * Mapping isolated uids to the actual owning app uid.
250 */
251 final SparseIntArray mIsolatedUids = new SparseIntArray();
252
253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * The statistics we have collected organized by uids.
255 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800256 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
258 // A set of pools of currently active timers. When a timer is queried, we will divide the
259 // elapsed time by the number of active timers to arrive at that timer's share of the time.
260 // In order to do this, we must refresh each timer whenever the number of active timers
261 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700262 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
263 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
264 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700265 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700266 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
267 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
268 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
269 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
270 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
271 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
272 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
273 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700274 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
275 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800276 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700278 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700279 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 // These are the objects that will want to do something when the device
282 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800283 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800284
285 // These are the objects that will want to do something when the device
286 // is unplugged from power *and* the screen is off.
287 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
288
289 // Set to true when we want to distribute CPU across wakelocks for the next
290 // CPU update, even if we aren't currently running wake locks.
291 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700292
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700293 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700294
Dianne Hackborn37de0982014-05-09 09:32:18 -0700295 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800296
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700297 long mHistoryBaseTime;
298 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700299 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700300 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700301
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700302 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
303 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700304 final Parcel mHistoryBuffer = Parcel.obtain();
305 final HistoryItem mHistoryLastWritten = new HistoryItem();
306 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700307 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700308 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700309 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800310 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800311 int[] mReadHistoryUids;
312 int mReadHistoryChars;
313 int mNextHistoryTagIdx = 0;
314 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700315 int mHistoryBufferLastPos = -1;
316 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700317 int mActiveHistoryStates = 0xffffffff;
318 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700319 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700320 long mTrackRunningHistoryElapsedRealtime = 0;
321 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700322
323 final HistoryItem mHistoryCur = new HistoryItem();
324
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700325 HistoryItem mHistory;
326 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700327 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700328 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700329
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800330 // Used by computeHistoryStepDetails
331 HistoryStepDetails mLastHistoryStepDetails = null;
332 byte mLastHistoryStepLevel = 0;
333 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
334 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
335 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700336
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800337 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700338 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800339 */
340 long mLastStepCpuUserTime;
341 long mCurStepCpuUserTime;
342 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700343 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800344 */
345 long mLastStepCpuSystemTime;
346 long mCurStepCpuSystemTime;
347 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700348 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800349 */
350 long mLastStepStatUserTime;
351 long mLastStepStatSystemTime;
352 long mLastStepStatIOWaitTime;
353 long mLastStepStatIrqTime;
354 long mLastStepStatSoftIrqTime;
355 long mLastStepStatIdleTime;
356 long mCurStepStatUserTime;
357 long mCurStepStatSystemTime;
358 long mCurStepStatIOWaitTime;
359 long mCurStepStatIrqTime;
360 long mCurStepStatSoftIrqTime;
361 long mCurStepStatIdleTime;
362
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700363 private HistoryItem mHistoryIterator;
364 private boolean mReadOverflow;
365 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 int mStartCount;
368
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800369 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700370 String mStartPlatformVersion;
371 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 long mUptime;
374 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 long mRealtime;
376 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700377
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800378 int mWakeLockNesting;
379 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700380 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700381 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800382
Jeff Browne95c3cd2014-05-02 16:59:26 -0700383 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700384 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700385
Dianne Hackborn617f8772009-03-31 15:04:46 -0700386 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700387 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700388
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800389 boolean mPretendScreenOff;
390
Jeff Browne95c3cd2014-05-02 16:59:26 -0700391 boolean mInteractive;
392 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700393
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700394 boolean mPowerSaveModeEnabled;
395 StopwatchTimer mPowerSaveModeEnabledTimer;
396
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700397 boolean mDeviceIdling;
398 StopwatchTimer mDeviceIdlingTimer;
399
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700400 boolean mDeviceLightIdling;
401 StopwatchTimer mDeviceLightIdlingTimer;
402
403 int mDeviceIdleMode;
404 long mLastIdleTimeStart;
405 long mLongestLightIdleTime;
406 long mLongestFullIdleTime;
407 StopwatchTimer mDeviceIdleModeLightTimer;
408 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700411 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700412
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700413 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700414 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700415
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700416 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700417 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700418
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700419 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700420 StopwatchTimer mFlashlightOnTimer;
421
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700422 int mCameraOnNesting;
423 StopwatchTimer mCameraOnTimer;
424
Dianne Hackborn627bba72009-03-24 22:32:56 -0700425 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800426 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700427 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800428 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700429
430 StopwatchTimer mPhoneSignalScanningTimer;
431
Dianne Hackborn627bba72009-03-24 22:32:56 -0700432 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700433 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700434 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700435
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800436 final LongSamplingCounter[] mNetworkByteActivityCounters =
437 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
438 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700439 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
440
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800441 /**
442 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
443 */
444 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700445
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800446 /**
447 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
448 */
449 ControllerActivityCounterImpl mBluetoothActivity;
450
451 /**
452 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
453 */
454 ControllerActivityCounterImpl mModemActivity;
455
456 /**
457 * Whether the device supports WiFi controller energy reporting. This is set to true on
458 * the first WiFi energy report. See {@link #mWifiActivity}.
459 */
460 boolean mHasWifiReporting = false;
461
462 /**
463 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
464 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
465 */
466 boolean mHasBluetoothReporting = false;
467
468 /**
469 * Whether the device supports Modem controller energy reporting. This is set to true on
470 * the first Modem energy report. See {@link #mModemActivity}.
471 */
472 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700473
The Android Open Source Project10592532009-03-18 17:39:46 -0700474 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700475 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700476
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700477 boolean mGlobalWifiRunning;
478 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700479
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800480 int mWifiState = -1;
481 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
482
Dianne Hackborn3251b902014-06-20 14:40:53 -0700483 int mWifiSupplState = -1;
484 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
485
486 int mWifiSignalStrengthBin = -1;
487 final StopwatchTimer[] mWifiSignalStrengthsTimer =
488 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
489
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800490 int mBluetoothScanNesting;
Bookatz867c0d72017-03-07 18:23:42 -0800491 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
492 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800493
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700494 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700495 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800496 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800497 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700498 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800499 LongSamplingCounter mMobileRadioActiveUnknownTime;
500 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800501
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700502 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 /**
505 * These provide time bases that discount the time the device is plugged
506 * in to power.
507 */
508 boolean mOnBattery;
509 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700510
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700511 /**
512 * External reporting of whether the device is actually charging.
513 */
514 boolean mCharging = true;
515 int mLastChargingStateLevel;
516
The Android Open Source Project10592532009-03-18 17:39:46 -0700517 /*
518 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
519 */
Evan Millar633a1742009-04-02 16:36:33 -0700520 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700521 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700522 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700523 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700524 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700525 int mLowDischargeAmountSinceCharge;
526 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800527 int mDischargeScreenOnUnplugLevel;
528 int mDischargeScreenOffUnplugLevel;
529 int mDischargeAmountScreenOn;
530 int mDischargeAmountScreenOnSinceCharge;
531 int mDischargeAmountScreenOff;
532 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700533
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700534 private LongSamplingCounter mDischargeScreenOffCounter;
535 private LongSamplingCounter mDischargeCounter;
536
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700537 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700538
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700539 int mInitStepMode = 0;
540 int mCurStepMode = 0;
541 int mModStepMode = 0;
542
Dianne Hackborn260c5022014-04-29 11:23:16 -0700543 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700544 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800545 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
546 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700547 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700548
549 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700550 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800551 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
552 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
553
554 static final int MAX_DAILY_ITEMS = 10;
555
556 long mDailyStartTime = 0;
557 long mNextMinDailyDeadline = 0;
558 long mNextMaxDailyDeadline = 0;
559
560 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700563
Amith Yamasanif37447b2009-10-08 18:28:01 -0700564 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800565 private int mPhoneServiceStateRaw = -1;
566 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700567
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800568 private int mNumConnectivityChange;
569 private int mLoadedNumConnectivityChange;
570 private int mUnpluggedNumConnectivityChange;
571
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700572 private int mEstimatedBatteryCapacity = -1;
573
Jocelyn Dangc627d102017-04-14 13:15:14 -0700574 private int mMinLearnedBatteryCapacity = -1;
575 private int mMaxLearnedBatteryCapacity = -1;
Adam Lesinski041d9172016-12-12 12:03:56 -0800576
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700577 private long[] mCpuFreqs;
578
Adam Lesinskie08af192015-03-25 16:42:59 -0700579 private PowerProfile mPowerProfile;
580
Evan Millarc64edde2009-04-18 12:26:32 -0700581 /*
582 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
583 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700584 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700585
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700586 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700587 return mKernelWakelockStats;
588 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700589
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700590 String mLastWakeupReason = null;
591 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700592 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700593
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700594 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700595 return mWakeupReasonStats;
596 }
597
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700598 @Override
599 public LongCounter getDischargeScreenOffCoulombCounter() {
600 return mDischargeScreenOffCounter;
601 }
602
603 @Override
604 public LongCounter getDischargeCoulombCounter() {
605 return mDischargeCounter;
606 }
607
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700608 @Override
609 public int getEstimatedBatteryCapacity() {
610 return mEstimatedBatteryCapacity;
611 }
612
Jocelyn Dangc627d102017-04-14 13:15:14 -0700613 @Override
614 public int getMinLearnedBatteryCapacity() {
615 return mMinLearnedBatteryCapacity;
616 }
617
618 @Override
619 public int getMaxLearnedBatteryCapacity() {
620 return mMaxLearnedBatteryCapacity;
621 }
622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800624 this(new SystemClocks());
625 }
626
627 public BatteryStatsImpl(Clocks clocks) {
628 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700629 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700630 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800631 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700632 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700633 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700634 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700635 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637
Joe Onoratoabded112016-02-08 16:49:39 -0800638 private void init(Clocks clocks) {
639 mClocks = clocks;
Joe Onoratoabded112016-02-08 16:49:39 -0800640 }
641
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700642 public interface TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800643 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
644 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
645 }
646
Joe Onoratoabded112016-02-08 16:49:39 -0800647 // methods are protected not private to be VisibleForTesting
648 public static class TimeBase {
649 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800650
Joe Onoratoabded112016-02-08 16:49:39 -0800651 protected long mUptime;
652 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800653
Joe Onoratoabded112016-02-08 16:49:39 -0800654 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800655
Joe Onoratoabded112016-02-08 16:49:39 -0800656 protected long mPastUptime;
657 protected long mUptimeStart;
658 protected long mPastRealtime;
659 protected long mRealtimeStart;
660 protected long mUnpluggedUptime;
661 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800662
663 public void dump(PrintWriter pw, String prefix) {
664 StringBuilder sb = new StringBuilder(128);
665 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
666 sb.setLength(0);
667 sb.append(prefix);
668 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700669 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800670 pw.println(sb.toString());
671 sb.setLength(0);
672 sb.append(prefix);
673 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700674 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800675 pw.println(sb.toString());
676 sb.setLength(0);
677 sb.append(prefix);
678 sb.append("mPastUptime=");
679 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
680 formatTimeMs(sb, mUptimeStart / 1000);
681 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
682 pw.println(sb.toString());
683 sb.setLength(0);
684 sb.append(prefix);
685 sb.append("mPastRealtime=");
686 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
687 formatTimeMs(sb, mRealtimeStart / 1000);
688 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
689 pw.println(sb.toString());
690 }
691
692 public void add(TimeBaseObs observer) {
693 mObservers.add(observer);
694 }
695
696 public void remove(TimeBaseObs observer) {
697 if (!mObservers.remove(observer)) {
698 Slog.wtf(TAG, "Removed unknown observer: " + observer);
699 }
700 }
701
Joe Onoratoabded112016-02-08 16:49:39 -0800702 public boolean hasObserver(TimeBaseObs observer) {
703 return mObservers.contains(observer);
704 }
705
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800706 public void init(long uptime, long realtime) {
707 mRealtime = 0;
708 mUptime = 0;
709 mPastUptime = 0;
710 mPastRealtime = 0;
711 mUptimeStart = uptime;
712 mRealtimeStart = realtime;
713 mUnpluggedUptime = getUptime(mUptimeStart);
714 mUnpluggedRealtime = getRealtime(mRealtimeStart);
715 }
716
717 public void reset(long uptime, long realtime) {
718 if (!mRunning) {
719 mPastUptime = 0;
720 mPastRealtime = 0;
721 } else {
722 mUptimeStart = uptime;
723 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800724 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
725 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800726 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800727 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800728 mUnpluggedRealtime = getRealtime(realtime);
729 }
730 }
731
732 public long computeUptime(long curTime, int which) {
733 switch (which) {
734 case STATS_SINCE_CHARGED:
735 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800736 case STATS_CURRENT:
737 return getUptime(curTime);
738 case STATS_SINCE_UNPLUGGED:
739 return getUptime(curTime) - mUnpluggedUptime;
740 }
741 return 0;
742 }
743
744 public long computeRealtime(long curTime, int which) {
745 switch (which) {
746 case STATS_SINCE_CHARGED:
747 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800748 case STATS_CURRENT:
749 return getRealtime(curTime);
750 case STATS_SINCE_UNPLUGGED:
751 return getRealtime(curTime) - mUnpluggedRealtime;
752 }
753 return 0;
754 }
755
756 public long getUptime(long curTime) {
757 long time = mPastUptime;
758 if (mRunning) {
759 time += curTime - mUptimeStart;
760 }
761 return time;
762 }
763
764 public long getRealtime(long curTime) {
765 long time = mPastRealtime;
766 if (mRunning) {
767 time += curTime - mRealtimeStart;
768 }
769 return time;
770 }
771
772 public long getUptimeStart() {
773 return mUptimeStart;
774 }
775
776 public long getRealtimeStart() {
777 return mRealtimeStart;
778 }
779
780 public boolean isRunning() {
781 return mRunning;
782 }
783
784 public boolean setRunning(boolean running, long uptime, long realtime) {
785 if (mRunning != running) {
786 mRunning = running;
787 if (running) {
788 mUptimeStart = uptime;
789 mRealtimeStart = realtime;
790 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
791 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
792
793 for (int i = mObservers.size() - 1; i >= 0; i--) {
794 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
795 }
796 } else {
797 mPastUptime += uptime - mUptimeStart;
798 mPastRealtime += realtime - mRealtimeStart;
799
800 long batteryUptime = getUptime(uptime);
801 long batteryRealtime = getRealtime(realtime);
802
803 for (int i = mObservers.size() - 1; i >= 0; i--) {
804 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
805 }
806 }
807 return true;
808 }
809 return false;
810 }
811
812 public void readSummaryFromParcel(Parcel in) {
813 mUptime = in.readLong();
814 mRealtime = in.readLong();
815 }
816
817 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
818 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
819 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
820 }
821
822 public void readFromParcel(Parcel in) {
823 mRunning = false;
824 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800825 mPastUptime = in.readLong();
826 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700827 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800828 mPastRealtime = in.readLong();
829 mRealtimeStart = in.readLong();
830 mUnpluggedUptime = in.readLong();
831 mUnpluggedRealtime = in.readLong();
832 }
833
834 public void writeToParcel(Parcel out, long uptime, long realtime) {
835 final long runningUptime = getUptime(uptime);
836 final long runningRealtime = getRealtime(realtime);
837 out.writeLong(mUptime);
838 out.writeLong(runningUptime);
839 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700840 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800841 out.writeLong(runningRealtime);
842 out.writeLong(mRealtimeStart);
843 out.writeLong(mUnpluggedUptime);
844 out.writeLong(mUnpluggedRealtime);
845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700849 * State for keeping track of counting information.
850 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800851 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700852 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800853 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700854 int mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700855 int mUnpluggedCount;
856 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700857
Bookatz8c6f3c52017-05-24 12:00:17 -0700858 public Counter(TimeBase timeBase, Parcel in) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800859 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700860 mPluggedCount = in.readInt();
861 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700862 mLoadedCount = in.readInt();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700863 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800864 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700865 }
866
Bookatz8c6f3c52017-05-24 12:00:17 -0700867 public Counter(TimeBase timeBase) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800868 mTimeBase = timeBase;
869 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700870 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700871
Dianne Hackborn617f8772009-03-31 15:04:46 -0700872 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700873 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700874 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700875 out.writeInt(mUnpluggedCount);
876 }
877
Bookatz8c6f3c52017-05-24 12:00:17 -0700878 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800879 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700880 mUnpluggedCount = mPluggedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700881 }
882
Bookatz8c6f3c52017-05-24 12:00:17 -0700883 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800884 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700885 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700886 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700887
Dianne Hackborn617f8772009-03-31 15:04:46 -0700888 /**
889 * Writes a possibly null Counter to a Parcel.
890 *
891 * @param out the Parcel to be written to.
892 * @param counter a Counter, or null.
893 */
894 public static void writeCounterToParcel(Parcel out, Counter counter) {
895 if (counter == null) {
896 out.writeInt(0); // indicates null
897 return;
898 }
899 out.writeInt(1); // indicates non-null
900
901 counter.writeToParcel(out);
902 }
903
904 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700905 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700906 int val = mCount.get();
907 if (which == STATS_SINCE_UNPLUGGED) {
908 val -= mUnpluggedCount;
909 } else if (which != STATS_SINCE_CHARGED) {
910 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700911 }
912
913 return val;
914 }
915
916 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700917 pw.println(prefix + "mCount=" + mCount.get()
Bookatz8c6f3c52017-05-24 12:00:17 -0700918 + " mLoadedCount=" + mLoadedCount
Dianne Hackborn617f8772009-03-31 15:04:46 -0700919 + " mUnpluggedCount=" + mUnpluggedCount
920 + " mPluggedCount=" + mPluggedCount);
921 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700922
Bookatz8c6f3c52017-05-24 12:00:17 -0700923 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
924 public void stepAtomic() {
925 if (mTimeBase.isRunning()) {
926 mCount.incrementAndGet();
927 }
Dianne Hackborn617f8772009-03-31 15:04:46 -0700928 }
929
Bookatz4ebc0642017-05-11 12:21:19 -0700930 void addAtomic(int delta) {
Bookatz8c6f3c52017-05-24 12:00:17 -0700931 if (mTimeBase.isRunning()) {
932 mCount.addAndGet(delta);
933 }
Bookatz4ebc0642017-05-11 12:21:19 -0700934 }
935
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700936 /**
937 * Clear state of this counter.
938 */
939 void reset(boolean detachIfReset) {
940 mCount.set(0);
Bookatz8c6f3c52017-05-24 12:00:17 -0700941 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700942 if (detachIfReset) {
943 detach();
944 }
945 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700946
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700947 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800948 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700949 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700950
Bookatz8c6f3c52017-05-24 12:00:17 -0700951 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
952 public void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700953 int count = mCount.get();
954 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700955 }
956
Bookatz8c6f3c52017-05-24 12:00:17 -0700957 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
958 public void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700959 mLoadedCount = in.readInt();
960 mCount.set(mLoadedCount);
Christopher Tate4cee7252010-03-19 14:50:40 -0700961 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700962 }
963 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700964
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700965 @VisibleForTesting
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700966 public static class LongSamplingCounterArray extends LongCounterArray implements TimeBaseObs {
967 final TimeBase mTimeBase;
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700968 public long[] mCounts;
969 public long[] mLoadedCounts;
970 public long[] mUnpluggedCounts;
971 public long[] mPluggedCounts;
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700972
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700973 private LongSamplingCounterArray(TimeBase timeBase, Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700974 mTimeBase = timeBase;
975 mPluggedCounts = in.createLongArray();
976 mCounts = copyArray(mPluggedCounts, mCounts);
977 mLoadedCounts = in.createLongArray();
978 mUnpluggedCounts = in.createLongArray();
979 timeBase.add(this);
980 }
981
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700982 public LongSamplingCounterArray(TimeBase timeBase) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700983 mTimeBase = timeBase;
984 timeBase.add(this);
985 }
986
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700987 private void writeToParcel(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700988 out.writeLongArray(mCounts);
989 out.writeLongArray(mLoadedCounts);
990 out.writeLongArray(mUnpluggedCounts);
991 }
992
993 @Override
994 public void onTimeStarted(long elapsedRealTime, long baseUptime, long baseRealtime) {
995 mUnpluggedCounts = copyArray(mPluggedCounts, mUnpluggedCounts);
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700996 }
997
998 @Override
999 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1000 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1001 }
1002
1003 @Override
1004 public long[] getCountsLocked(int which) {
1005 long[] val = copyArray(mTimeBase.isRunning() ? mCounts : mPluggedCounts, null);
1006 if (which == STATS_SINCE_UNPLUGGED) {
1007 subtract(val, mUnpluggedCounts);
1008 } else if (which != STATS_SINCE_CHARGED) {
1009 subtract(val, mLoadedCounts);
1010 }
1011 return val;
1012 }
1013
1014 @Override
1015 public void logState(Printer pw, String prefix) {
1016 pw.println(prefix + "mCounts=" + Arrays.toString(mCounts)
1017 + " mLoadedCounts=" + Arrays.toString(mLoadedCounts)
1018 + " mUnpluggedCounts=" + Arrays.toString(mUnpluggedCounts)
1019 + " mPluggedCounts=" + Arrays.toString(mPluggedCounts));
1020 }
1021
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001022 public void addCountLocked(long[] counts) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001023 if (counts == null) {
1024 return;
1025 }
Bookatz8c6f3c52017-05-24 12:00:17 -07001026 if (mTimeBase.isRunning()) {
1027 if (mCounts == null) {
1028 mCounts = new long[counts.length];
1029 }
1030 for (int i = 0; i < counts.length; ++i) {
1031 mCounts[i] += counts[i];
1032 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001033 }
1034 }
1035
1036 /**
1037 * Clear state of this counter.
1038 */
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001039 public void reset(boolean detachIfReset) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001040 fillArray(mCounts, 0);
1041 fillArray(mLoadedCounts, 0);
1042 fillArray(mPluggedCounts, 0);
1043 fillArray(mUnpluggedCounts, 0);
1044 if (detachIfReset) {
1045 detach();
1046 }
1047 }
1048
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001049 public void detach() {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001050 mTimeBase.remove(this);
1051 }
1052
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001053 private void writeSummaryToParcelLocked(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001054 out.writeLongArray(mCounts);
1055 }
1056
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001057 private void readSummaryFromParcelLocked(Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001058 mCounts = in.createLongArray();
1059 mLoadedCounts = copyArray(mCounts, mLoadedCounts);
1060 mUnpluggedCounts = copyArray(mCounts, mUnpluggedCounts);
1061 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1062 }
1063
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001064 public static void writeToParcel(Parcel out, LongSamplingCounterArray counterArray) {
1065 if (counterArray != null) {
1066 out.writeInt(1);
1067 counterArray.writeToParcel(out);
1068 } else {
1069 out.writeInt(0);
1070 }
1071 }
1072
1073 public static LongSamplingCounterArray readFromParcel(Parcel in, TimeBase timeBase) {
1074 if (in.readInt() != 0) {
1075 return new LongSamplingCounterArray(timeBase, in);
1076 } else {
1077 return null;
1078 }
1079 }
1080
1081 public static void writeSummaryToParcelLocked(Parcel out,
1082 LongSamplingCounterArray counterArray) {
1083 if (counterArray != null) {
1084 out.writeInt(1);
1085 counterArray.writeSummaryToParcelLocked(out);
1086 } else {
1087 out.writeInt(0);
1088 }
1089 }
1090
1091 public static LongSamplingCounterArray readSummaryFromParcelLocked(Parcel in,
1092 TimeBase timeBase) {
1093 if (in.readInt() != 0) {
1094 final LongSamplingCounterArray counterArray
1095 = new LongSamplingCounterArray(timeBase);
1096 counterArray.readSummaryFromParcelLocked(in);
1097 return counterArray;
1098 } else {
1099 return null;
1100 }
1101 }
1102
Bookatz8c6f3c52017-05-24 12:00:17 -07001103 private static void fillArray(long[] a, long val) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001104 if (a != null) {
1105 Arrays.fill(a, val);
1106 }
1107 }
1108
Bookatz8c6f3c52017-05-24 12:00:17 -07001109 private static void subtract(@NonNull long[] val, long[] toSubtract) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001110 if (toSubtract == null) {
1111 return;
1112 }
1113 for (int i = 0; i < val.length; i++) {
1114 val[i] -= toSubtract[i];
1115 }
1116 }
1117
Bookatz8c6f3c52017-05-24 12:00:17 -07001118 private static long[] copyArray(long[] src, long[] dest) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001119 if (src == null) {
1120 return null;
1121 } else {
1122 if (dest == null) {
1123 dest = new long[src.length];
1124 }
1125 System.arraycopy(src, 0, dest, 0, src.length);
1126 return dest;
1127 }
1128 }
1129 }
1130
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001131 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001132 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001133 long mCount;
1134 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001135 long mUnpluggedCount;
1136 long mPluggedCount;
1137
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001138 LongSamplingCounter(TimeBase timeBase, Parcel in) {
1139 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001140 mPluggedCount = in.readLong();
1141 mCount = mPluggedCount;
1142 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001143 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001144 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001145 }
1146
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001147 LongSamplingCounter(TimeBase timeBase) {
1148 mTimeBase = timeBase;
1149 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001150 }
1151
1152 public void writeToParcel(Parcel out) {
1153 out.writeLong(mCount);
1154 out.writeLong(mLoadedCount);
1155 out.writeLong(mUnpluggedCount);
1156 }
1157
1158 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001159 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001160 mUnpluggedCount = mPluggedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001161 }
1162
1163 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001164 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001165 mPluggedCount = mCount;
1166 }
1167
1168 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001169 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -07001170 if (which == STATS_SINCE_UNPLUGGED) {
1171 val -= mUnpluggedCount;
1172 } else if (which != STATS_SINCE_CHARGED) {
1173 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001174 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001175 return val;
1176 }
1177
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001178 @Override
1179 public void logState(Printer pw, String prefix) {
1180 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001181 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001182 + " mUnpluggedCount=" + mUnpluggedCount
1183 + " mPluggedCount=" + mPluggedCount);
1184 }
1185
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001186 void addCountLocked(long count) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001187 if (mTimeBase.isRunning()) {
1188 mCount += count;
1189 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001190 }
1191
1192 /**
1193 * Clear state of this counter.
1194 */
1195 void reset(boolean detachIfReset) {
1196 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001197 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001198 if (detachIfReset) {
1199 detach();
1200 }
1201 }
1202
1203 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001204 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001205 }
1206
1207 void writeSummaryFromParcelLocked(Parcel out) {
1208 out.writeLong(mCount);
1209 }
1210
1211 void readSummaryFromParcelLocked(Parcel in) {
1212 mLoadedCount = in.readLong();
1213 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001214 mUnpluggedCount = mPluggedCount = mLoadedCount;
1215 }
1216 }
1217
Dianne Hackborn617f8772009-03-31 15:04:46 -07001218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 * State for keeping track of timing information.
1220 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001221 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001222 protected final Clocks mClocks;
1223 protected final int mType;
1224 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001225
Joe Onoratoabded112016-02-08 16:49:39 -08001226 protected int mCount;
1227 protected int mLoadedCount;
1228 protected int mLastCount;
1229 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 // Times are in microseconds for better accuracy when dividing by the
1232 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * The total time we have accumulated since the start of the original
1236 * boot, to the last time something interesting happened in the
1237 * current run.
1238 */
Joe Onoratoabded112016-02-08 16:49:39 -08001239 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 /**
1242 * The total time we loaded for the previous runs. Subtract this from
1243 * mTotalTime to find the time for the current run of the system.
1244 */
Joe Onoratoabded112016-02-08 16:49:39 -08001245 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 /**
1248 * The run time of the last run of the system, as loaded from the
1249 * saved data.
1250 */
Joe Onoratoabded112016-02-08 16:49:39 -08001251 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * The value of mTotalTime when unplug() was last called. Subtract
1255 * this from mTotalTime to find the time since the last unplug from
1256 * power.
1257 */
Joe Onoratoabded112016-02-08 16:49:39 -08001258 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001259
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001260 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001261 * The total time this timer has been running until the latest mark has been set.
1262 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1263 */
Joe Onoratoabded112016-02-08 16:49:39 -08001264 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001265
1266 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001267 * Constructs from a parcel.
1268 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001269 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001270 * @param in
1271 */
Joe Onoratoabded112016-02-08 16:49:39 -08001272 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1273 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001275 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 mCount = in.readInt();
1278 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001279 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 mUnpluggedCount = in.readInt();
1281 mTotalTime = in.readLong();
1282 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001283 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001285 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001286 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001287 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289
Joe Onoratoabded112016-02-08 16:49:39 -08001290 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1291 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001293 mTimeBase = timeBase;
1294 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 }
Evan Millarc64edde2009-04-18 12:26:32 -07001296
1297 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001298
Evan Millarc64edde2009-04-18 12:26:32 -07001299 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001300
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001301 /**
1302 * Clear state of this timer. Returns true if the timer is inactive
1303 * so can be completely dropped.
1304 */
Joe Onoratoabded112016-02-08 16:49:39 -08001305 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001306 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001307 mCount = mLoadedCount = mLastCount = 0;
1308 if (detachIfReset) {
1309 detach();
1310 }
1311 return true;
1312 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001313
Joe Onoratoabded112016-02-08 16:49:39 -08001314 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001315 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001316 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001317
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001318 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001319 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1320 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001321 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001324 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001327 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
1329
Adam Lesinskie08af192015-03-25 16:42:59 -07001330 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001331 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001333 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 + " old mUnpluggedTime=" + mUnpluggedTime
1335 + " old mUnpluggedCount=" + mUnpluggedCount);
1336 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001337 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001338 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 if (DEBUG && mType < 0) {
1340 Log.v(TAG, "unplug #" + mType
1341 + ": new mUnpluggedTime=" + mUnpluggedTime
1342 + " new mUnpluggedCount=" + mUnpluggedCount);
1343 }
1344 }
1345
Adam Lesinskie08af192015-03-25 16:42:59 -07001346 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001347 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001348 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001349 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001350 + " old mTotalTime=" + mTotalTime);
1351 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001352 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001353 mCount = computeCurrentCountLocked();
1354 if (DEBUG && mType < 0) {
1355 Log.v(TAG, "plug #" + mType
1356 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 }
1358 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Writes a possibly null Timer to a Parcel.
1362 *
1363 * @param out the Parcel to be written to.
1364 * @param timer a Timer, or null.
1365 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001366 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 if (timer == null) {
1368 out.writeInt(0); // indicates null
1369 return;
1370 }
1371 out.writeInt(1); // indicates non-null
1372
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001373 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
1375
1376 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001377 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001378 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1379 if (which == STATS_SINCE_UNPLUGGED) {
1380 val -= mUnpluggedTime;
1381 } else if (which != STATS_SINCE_CHARGED) {
1382 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
1384
1385 return val;
1386 }
1387
1388 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001389 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001390 int val = computeCurrentCountLocked();
1391 if (which == STATS_SINCE_UNPLUGGED) {
1392 val -= mUnpluggedCount;
1393 } else if (which != STATS_SINCE_CHARGED) {
1394 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396
1397 return val;
1398 }
1399
Adam Lesinskie08af192015-03-25 16:42:59 -07001400 @Override
1401 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1402 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1403 return val - mTimeBeforeMark;
1404 }
1405
1406 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001407 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001408 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1410 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001411 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001413 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001415 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001416
1417
Joe Onoratoabded112016-02-08 16:49:39 -08001418 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001419 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1420 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001421 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001422 }
1423
Joe Onoratoabded112016-02-08 16:49:39 -08001424 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001425 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001426 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001427 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001428 mUnpluggedTime = mTotalTime;
1429 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001430 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001431 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001432
1433 // When reading the summary, we set the mark to be the latest information.
1434 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001435 }
1436 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001437
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001438 /**
1439 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1440 * method. The state of the timer according to its {@link TimeBase} will determine how much
1441 * of the value is recorded.
1442 *
1443 * If the value being recorded resets, {@link #endSample()} can be called in order to
1444 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1445 * between calls, the {@link #endSample()} is automatically called and the new value is
1446 * expected to increase monotonically from that point on.
1447 */
Joe Onoratoabded112016-02-08 16:49:39 -08001448 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001449
Evan Millarc64edde2009-04-18 12:26:32 -07001450 /**
1451 * The most recent reported count from /proc/wakelocks.
1452 */
1453 int mCurrentReportedCount;
1454
1455 /**
1456 * The reported count from /proc/wakelocks when unplug() was last
1457 * called.
1458 */
1459 int mUnpluggedReportedCount;
1460
1461 /**
1462 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001463 */
Evan Millarc64edde2009-04-18 12:26:32 -07001464 long mCurrentReportedTotalTime;
1465
1466
1467 /**
1468 * The reported total_time from /proc/wakelocks when unplug() was last
1469 * called.
1470 */
1471 long mUnpluggedReportedTotalTime;
1472
1473 /**
1474 * Whether we are currently in a discharge cycle.
1475 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001476 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001477
1478 /**
1479 * Whether we are currently recording reported values.
1480 */
1481 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001482
Evan Millarc64edde2009-04-18 12:26:32 -07001483 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001484 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001485 */
1486 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001487
Adam Lesinski98f0d462016-04-19 16:46:20 -07001488 @VisibleForTesting
1489 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001490 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001491 mCurrentReportedCount = in.readInt();
1492 mUnpluggedReportedCount = in.readInt();
1493 mCurrentReportedTotalTime = in.readLong();
1494 mUnpluggedReportedTotalTime = in.readLong();
1495 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001496 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001497 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001498
Adam Lesinski98f0d462016-04-19 16:46:20 -07001499 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001500 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001501 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001502 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001503 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001504 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001505
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001506 /**
1507 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1508 * be less than the values used for a previous invocation.
1509 */
1510 public void endSample() {
1511 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1512 mCount = computeCurrentCountLocked();
1513 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1514 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001515 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001516
Evan Millarc64edde2009-04-18 12:26:32 -07001517 public void setUpdateVersion(int version) {
1518 mUpdateVersion = version;
1519 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001520
Evan Millarc64edde2009-04-18 12:26:32 -07001521 public int getUpdateVersion() {
1522 return mUpdateVersion;
1523 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001524
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001525 /**
1526 * Updates the current recorded values. These are meant to be monotonically increasing
1527 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1528 *
1529 * If the values being recorded have been reset, the monotonically increasing requirement
1530 * will be broken. In this case, {@link #endSample()} is automatically called and
1531 * the total value of totalTime and count are recorded, starting a new monotonically
1532 * increasing sample.
1533 *
1534 * @param totalTime total time of sample in microseconds.
1535 * @param count total number of times the event being sampled occurred.
1536 */
1537 public void update(long totalTime, int count) {
1538 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001539 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001540 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001541 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001542 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001543
1544 mTrackingReportedValues = true;
1545
1546 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1547 endSample();
1548 }
1549
1550 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001551 mCurrentReportedCount = count;
1552 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001553
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001554 /**
1555 * Adds deltaTime and deltaCount to the current sample.
1556 *
1557 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1558 * @param deltaCount additional number of times the event being sampled occurred.
1559 */
1560 public void add(long deltaTime, int deltaCount) {
1561 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001562 }
1563
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001564 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001565 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1566 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001567 if (mTrackingReportedValues) {
1568 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1569 mUnpluggedReportedCount = mCurrentReportedCount;
1570 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001571 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001572 }
1573
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001574 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001575 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1576 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1577 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001578 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001579
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001580 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001581 public void logState(Printer pw, String prefix) {
1582 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001583 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001584 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1585 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1586 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1587 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001588
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001589 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001590 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001591 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001592 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1593 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001594
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001595 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001596 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001597 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001598 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1599 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001600
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001601 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001602 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1603 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001604 out.writeInt(mCurrentReportedCount);
1605 out.writeInt(mUnpluggedReportedCount);
1606 out.writeLong(mCurrentReportedTotalTime);
1607 out.writeLong(mUnpluggedReportedTotalTime);
1608 out.writeInt(mTrackingReportedValues ? 1 : 0);
1609 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001610
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001611 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001612 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001613 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001614 mTrackingReportedValues = false;
1615 mUnpluggedReportedTotalTime = 0;
1616 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001617 return true;
1618 }
Evan Millarc64edde2009-04-18 12:26:32 -07001619 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001620
Evan Millarc64edde2009-04-18 12:26:32 -07001621 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001622 * A timer that increments in batches. It does not run for durations, but just jumps
1623 * for a pre-determined amount.
1624 */
Joe Onoratoabded112016-02-08 16:49:39 -08001625 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001626 final Uid mUid;
1627
1628 /**
1629 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1630 */
1631 long mLastAddedTime;
1632
1633 /**
1634 * The last duration that we added to the timer. This is in microseconds.
1635 */
1636 long mLastAddedDuration;
1637
1638 /**
1639 * Whether we are currently in a discharge cycle.
1640 */
1641 boolean mInDischarge;
1642
Joe Onoratoabded112016-02-08 16:49:39 -08001643 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1644 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001645 mUid = uid;
1646 mLastAddedTime = in.readLong();
1647 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001648 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001649 }
1650
Joe Onoratoabded112016-02-08 16:49:39 -08001651 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1652 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001653 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001654 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001655 }
1656
1657 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001658 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1659 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001660 out.writeLong(mLastAddedTime);
1661 out.writeLong(mLastAddedDuration);
1662 }
1663
1664 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001665 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001666 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001667 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001668 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001669 }
1670
1671 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001672 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001673 recomputeLastDuration(elapsedRealtime, false);
1674 mInDischarge = true;
1675 // If we are still within the last added duration, then re-added whatever remains.
1676 if (mLastAddedTime == elapsedRealtime) {
1677 mTotalTime += mLastAddedDuration;
1678 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001679 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001680 }
1681
1682 @Override
1683 public void logState(Printer pw, String prefix) {
1684 super.logState(pw, prefix);
1685 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1686 + " mLastAddedDuration=" + mLastAddedDuration);
1687 }
1688
1689 private long computeOverage(long curTime) {
1690 if (mLastAddedTime > 0) {
1691 return mLastTime + mLastAddedDuration - curTime;
1692 }
1693 return 0;
1694 }
1695
1696 private void recomputeLastDuration(long curTime, boolean abort) {
1697 final long overage = computeOverage(curTime);
1698 if (overage > 0) {
1699 // Aborting before the duration ran out -- roll back the remaining
1700 // duration. Only do this if currently discharging; otherwise we didn't
1701 // actually add the time.
1702 if (mInDischarge) {
1703 mTotalTime -= overage;
1704 }
1705 if (abort) {
1706 mLastAddedTime = 0;
1707 } else {
1708 mLastAddedTime = curTime;
1709 mLastAddedDuration -= overage;
1710 }
1711 }
1712 }
1713
1714 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001715 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001716 recomputeLastDuration(now, true);
1717 mLastAddedTime = now;
1718 mLastAddedDuration = durationMillis * 1000;
1719 if (mInDischarge) {
1720 mTotalTime += mLastAddedDuration;
1721 mCount++;
1722 }
1723 }
1724
1725 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001726 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001727 recomputeLastDuration(now, true);
1728 }
1729
1730 @Override
1731 protected int computeCurrentCountLocked() {
1732 return mCount;
1733 }
1734
1735 @Override
1736 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001737 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001738 if (overage > 0) {
1739 return mTotalTime = overage;
1740 }
1741 return mTotalTime;
1742 }
1743
1744 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001745 public boolean reset(boolean detachIfReset) {
1746 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001747 recomputeLastDuration(now, true);
1748 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001749 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001750 return !stillActive;
1751 }
1752 }
1753
Joe Onorato92fd23f2016-07-25 11:18:42 -07001754
1755 /**
1756 * A StopwatchTimer that also tracks the total and max individual
1757 * time spent active according to the given timebase. Whereas
1758 * StopwatchTimer apportions the time amongst all in the pool,
1759 * the total and max durations are not apportioned.
1760 */
1761 public static class DurationTimer extends StopwatchTimer {
1762 /**
1763 * The time (in ms) that the timer was last acquired or the time base
1764 * last (re-)started. Increasing the nesting depth does not reset this time.
1765 *
1766 * -1 if the timer is currently not running or the time base is not running.
1767 *
1768 * If written to a parcel, the start time is reset, as is mNesting in the base class
1769 * StopwatchTimer.
1770 */
1771 long mStartTimeMs = -1;
1772
1773 /**
Bookatz867c0d72017-03-07 18:23:42 -08001774 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001775 */
1776 long mMaxDurationMs;
1777
1778 /**
Bookatz867c0d72017-03-07 18:23:42 -08001779 * The time (in ms) that that the timer has been active since most recent
1780 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001781 */
1782 long mCurrentDurationMs;
1783
Bookatz867c0d72017-03-07 18:23:42 -08001784 /**
1785 * The total time (in ms) that that the timer has been active since most recent reset()
1786 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1787 * (but not including the present currentDuration) since reset. Not pooled.
1788 */
1789 long mTotalDurationMs;
1790
Joe Onorato92fd23f2016-07-25 11:18:42 -07001791 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1792 TimeBase timeBase, Parcel in) {
1793 super(clocks, uid, type, timerPool, timeBase, in);
1794 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001795 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001796 }
1797
1798 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1799 TimeBase timeBase) {
1800 super(clocks, uid, type, timerPool, timeBase);
1801 }
1802
1803 @Override
1804 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1805 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001806 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001807 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001808 }
1809
1810 /**
1811 * Write the summary to the parcel.
1812 *
1813 * Since the time base is probably meaningless after we come back, reading
1814 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001815 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001816 */
1817 @Override
1818 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1819 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001820 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001821 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001822 }
1823
1824 /**
1825 * Read the summary parcel.
1826 *
1827 * Has the side effect of stopping the timer.
1828 */
1829 @Override
1830 public void readSummaryFromParcelLocked(Parcel in) {
1831 super.readSummaryFromParcelLocked(in);
1832 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001833 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001834 mStartTimeMs = -1;
1835 mCurrentDurationMs = 0;
1836 }
1837
1838 /**
1839 * The TimeBase time started (again).
1840 *
1841 * If the timer is also running, store the start time.
1842 */
1843 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1844 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1845 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001846 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001847 }
1848 }
1849
1850 /**
1851 * The TimeBase stopped running.
1852 *
1853 * If the timer is running, add the duration into mCurrentDurationMs.
1854 */
1855 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001856 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1857 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001858 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001859 // baseRealtimeUs has already been converted to the timebase's realtime.
1860 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001861 }
1862 mStartTimeMs = -1;
1863 }
1864
1865 @Override
1866 public void logState(Printer pw, String prefix) {
1867 super.logState(pw, prefix);
1868 }
1869
1870 @Override
1871 public void startRunningLocked(long elapsedRealtimeMs) {
1872 super.startRunningLocked(elapsedRealtimeMs);
1873 if (mNesting == 1 && mTimeBase.isRunning()) {
1874 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001875 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001876 }
1877 }
1878
1879 /**
1880 * Decrements the mNesting ref-count on this timer.
1881 *
1882 * If it actually stopped (mNesting went to 0), then possibly update
1883 * mMaxDuration if the current duration was the longest ever.
1884 */
1885 @Override
1886 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001887 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001888 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001889 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001890 if (durationMs > mMaxDurationMs) {
1891 mMaxDurationMs = durationMs;
1892 }
1893 mStartTimeMs = -1;
1894 mCurrentDurationMs = 0;
1895 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001896 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1897 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1898 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001899 }
1900
1901 @Override
1902 public boolean reset(boolean detachIfReset) {
1903 boolean result = super.reset(detachIfReset);
1904 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08001905 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001906 mCurrentDurationMs = 0;
1907 if (mNesting > 0) {
1908 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
1909 } else {
1910 mStartTimeMs = -1;
1911 }
1912 return result;
1913 }
1914
1915 /**
1916 * Returns the max duration that this timer has ever seen.
1917 *
1918 * Note that this time is NOT split between the timers in the timer group that
1919 * this timer is attached to. It is the TOTAL time.
1920 */
1921 @Override
1922 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
1923 if (mNesting > 0) {
1924 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1925 if (durationMs > mMaxDurationMs) {
1926 return durationMs;
1927 }
1928 }
1929 return mMaxDurationMs;
1930 }
1931
1932 /**
1933 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08001934 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001935 *
1936 * Note that this time is NOT split between the timers in the timer group that
1937 * this timer is attached to. It is the TOTAL time.
1938 */
1939 @Override
1940 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
1941 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08001942 if (mNesting > 0 && mTimeBase.isRunning()) {
1943 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
1944 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001945 }
1946 return durationMs;
1947 }
Bookatz867c0d72017-03-07 18:23:42 -08001948
1949 /**
1950 * Returns the total cumulative duration that this timer has been on since reset().
1951 * If mTimerPool == null, this should be the same
1952 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
1953 *
1954 * Note that this time is NOT split between the timers in the timer group that
1955 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
1956 * the result will not be equivalent to getTotalTimeLocked.
1957 */
1958 @Override
1959 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
1960 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
1961 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07001962 }
1963
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001964 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001965 * State for keeping track of timing information.
1966 */
Joe Onoratoabded112016-02-08 16:49:39 -08001967 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001968 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001969 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001970
Evan Millarc64edde2009-04-18 12:26:32 -07001971 int mNesting;
1972
Evan Millarc64edde2009-04-18 12:26:32 -07001973 /**
1974 * The last time at which we updated the timer. If mNesting is > 0,
1975 * subtract this from the current battery time to find the amount of
1976 * time we have been running since we last computed an update.
1977 */
1978 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001979
Evan Millarc64edde2009-04-18 12:26:32 -07001980 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001981 * The total time at which the timer was acquired, to determine if it
Bookatzceebafe2017-04-06 11:59:13 -07001982 * was actually held for an interesting duration. If time base was not running when timer
1983 * was acquired, will be -1.
Evan Millarc64edde2009-04-18 12:26:32 -07001984 */
Bookatzceebafe2017-04-06 11:59:13 -07001985 long mAcquireTime = -1;
Evan Millarc64edde2009-04-18 12:26:32 -07001986
Amith Yamasanif37447b2009-10-08 18:28:01 -07001987 long mTimeout;
1988
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001989 /**
1990 * For partial wake locks, keep track of whether we are in the list
1991 * to consume CPU cycles.
1992 */
1993 boolean mInList;
1994
Joe Onoratoabded112016-02-08 16:49:39 -08001995 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001996 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001997 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001998 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001999 mTimerPool = timerPool;
2000 mUpdateTime = in.readLong();
2001 }
2002
Joe Onoratoabded112016-02-08 16:49:39 -08002003 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002004 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08002005 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002006 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002007 mTimerPool = timerPool;
2008 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002009
Joe Onoratoabded112016-02-08 16:49:39 -08002010 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002011 mTimeout = timeout;
2012 }
2013
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002014 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2015 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07002016 out.writeLong(mUpdateTime);
2017 }
2018
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002019 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07002020 if (mNesting > 0) {
2021 if (DEBUG && mType < 0) {
2022 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
2023 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002024 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
2025 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07002026 if (DEBUG && mType < 0) {
2027 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
2028 }
2029 }
2030 }
2031
2032 public void logState(Printer pw, String prefix) {
2033 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002034 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 + " mAcquireTime=" + mAcquireTime);
2036 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002037
Joe Onoratoabded112016-02-08 16:49:39 -08002038 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002040 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002041 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 if (mTimerPool != null) {
2043 // Accumulate time to all currently active timers before adding
2044 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002045 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 // Add this timer to the active pool
2047 mTimerPool.add(this);
2048 }
Bookatzceebafe2017-04-06 11:59:13 -07002049 if (mTimeBase.isRunning()) {
2050 // Increment the count
2051 mCount++;
2052 mAcquireTime = mTotalTime;
2053 } else {
2054 mAcquireTime = -1;
2055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 if (DEBUG && mType < 0) {
2057 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
2058 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2059 + " mAcquireTime=" + mAcquireTime);
2060 }
2061 }
2062 }
2063
Joe Onoratoabded112016-02-08 16:49:39 -08002064 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07002065 return mNesting > 0;
2066 }
2067
Joe Onoratoabded112016-02-08 16:49:39 -08002068 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 // Ignore attempt to stop a timer that isn't running
2070 if (mNesting == 0) {
2071 return;
2072 }
2073 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002074 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 if (mTimerPool != null) {
2076 // Accumulate time to all active counters, scaled by the total
2077 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002078 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 // Remove this timer from the active pool
2080 mTimerPool.remove(this);
2081 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 mNesting = 1;
2083 mTotalTime = computeRunTimeLocked(batteryRealtime);
2084 mNesting = 0;
2085 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 if (DEBUG && mType < 0) {
2088 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
2089 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2090 + " mAcquireTime=" + mAcquireTime);
2091 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002092
Bookatzceebafe2017-04-06 11:59:13 -07002093 if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 // If there was no change in the time, then discard this
2095 // count. A somewhat cheezy strategy, but hey.
2096 mCount--;
2097 }
2098 }
2099 }
2100
Joe Onoratoabded112016-02-08 16:49:39 -08002101 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07002102 if (mNesting > 0) {
2103 mNesting = 1;
2104 stopRunningLocked(elapsedRealtimeMs);
2105 }
2106 }
2107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 // Update the total time for all other running Timers with the same type as this Timer
2109 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002110 private static long refreshTimersLocked(long batteryRealtime,
2111 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002112 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 final int N = pool.size();
2114 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07002115 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 long heldTime = batteryRealtime - t.mUpdateTime;
2117 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002118 final long myTime = heldTime / N;
2119 if (t == self) {
2120 selfTime = myTime;
2121 }
2122 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
2124 t.mUpdateTime = batteryRealtime;
2125 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002126 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
2128
Evan Millarc64edde2009-04-18 12:26:32 -07002129 @Override
2130 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002131 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
2132 curBatteryRealtime = mUpdateTime + mTimeout;
2133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 return mTotalTime + (mNesting > 0
2135 ? (curBatteryRealtime - mUpdateTime)
2136 / (mTimerPool != null ? mTimerPool.size() : 1)
2137 : 0);
2138 }
2139
Evan Millarc64edde2009-04-18 12:26:32 -07002140 @Override
2141 protected int computeCurrentCountLocked() {
2142 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
2144
Adam Lesinskie08af192015-03-25 16:42:59 -07002145 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002146 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002147 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002148 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002149 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08002150 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002151 }
Bookatzceebafe2017-04-06 11:59:13 -07002152 mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002153 return canDetach;
2154 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002155
Adam Lesinskie08af192015-03-25 16:42:59 -07002156 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002157 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002158 super.detach();
2159 if (mTimerPool != null) {
2160 mTimerPool.remove(this);
2161 }
2162 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002163
Adam Lesinskie08af192015-03-25 16:42:59 -07002164 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002165 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07002166 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 mNesting = 0;
2168 }
Adam Lesinskie08af192015-03-25 16:42:59 -07002169
2170 /**
2171 * Set the mark so that we can query later for the total time the timer has
2172 * accumulated since this point. The timer can be running or not.
2173 *
2174 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
2175 */
2176 public void setMark(long elapsedRealtimeMs) {
2177 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
2178 if (mNesting > 0) {
2179 // We are running.
2180 if (mTimerPool != null) {
2181 refreshTimersLocked(batteryRealtime, mTimerPool, this);
2182 } else {
2183 mTotalTime += batteryRealtime - mUpdateTime;
2184 mUpdateTime = batteryRealtime;
2185 }
2186 }
2187 mTimeBeforeMark = mTotalTime;
2188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002190
Bookatz867c0d72017-03-07 18:23:42 -08002191 /**
2192 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2193 * TimeBase is effectively a subset of the other.
2194 */
Bookatzaa4594a2017-03-24 12:39:56 -07002195 public static class DualTimer extends DurationTimer {
2196 // This class both is a DurationTimer and also holds a second DurationTimer.
2197 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2198 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2199 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002200 // mSubTimer typically tracks only part of the total time, such as background time, as
2201 // determined by a subTimeBase. It is NOT pooled.
2202 private final DurationTimer mSubTimer;
2203
2204 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002205 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2206 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002207 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002208 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002209 */
2210 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2211 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002212 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002213 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2214 }
2215
2216 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002217 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2218 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002219 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002220 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002221 */
2222 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2223 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002224 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002225 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2226 }
2227
Bookatz867c0d72017-03-07 18:23:42 -08002228 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002229 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002230 public DurationTimer getSubTimer() {
2231 return mSubTimer;
2232 }
2233
Bookatzaa4594a2017-03-24 12:39:56 -07002234 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002235 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002236 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002237 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2238 }
2239
Bookatzaa4594a2017-03-24 12:39:56 -07002240 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002241 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002242 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002243 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2244 }
2245
Bookatzaa4594a2017-03-24 12:39:56 -07002246 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002247 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002248 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002249 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2250 }
2251
Bookatzaa4594a2017-03-24 12:39:56 -07002252 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002253 public boolean reset(boolean detachIfReset) {
2254 boolean active = false;
Bookatz4a3eda92017-04-10 13:10:46 -07002255 // Do not detach the subTimer explicitly since that'll be done by DualTimer.detach().
2256 active |= !mSubTimer.reset(false);
Bookatzaa4594a2017-03-24 12:39:56 -07002257 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002258 return !active;
2259 }
2260
Bookatzaa4594a2017-03-24 12:39:56 -07002261 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002262 public void detach() {
Bookatz867c0d72017-03-07 18:23:42 -08002263 mSubTimer.detach();
Bookatz4a3eda92017-04-10 13:10:46 -07002264 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002265 }
2266
Bookatzaa4594a2017-03-24 12:39:56 -07002267 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002268 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002269 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002270 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2271 }
2272
Bookatzaa4594a2017-03-24 12:39:56 -07002273 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002274 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002275 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002276 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2277 }
2278
Bookatzaa4594a2017-03-24 12:39:56 -07002279 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002280 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002281 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002282 mSubTimer.readSummaryFromParcelLocked(in);
2283 }
2284 }
2285
2286
Dianne Hackbornd953c532014-08-16 18:17:38 -07002287 public abstract class OverflowArrayMap<T> {
2288 private static final String OVERFLOW_NAME = "*overflow*";
2289
Dianne Hackborn657153b2016-07-29 14:54:14 -07002290 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002291 final ArrayMap<String, T> mMap = new ArrayMap<>();
2292 T mCurOverflow;
2293 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002294 long mLastOverflowTime;
2295 long mLastOverflowFinishTime;
2296 long mLastClearTime;
2297 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002298
Dianne Hackborn657153b2016-07-29 14:54:14 -07002299 public OverflowArrayMap(int uid) {
2300 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002301 }
2302
2303 public ArrayMap<String, T> getMap() {
2304 return mMap;
2305 }
2306
2307 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002308 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002309 mMap.clear();
2310 mCurOverflow = null;
2311 mActiveOverflow = null;
2312 }
2313
2314 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002315 if (name == null) {
2316 name = "";
2317 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002318 mMap.put(name, obj);
2319 if (OVERFLOW_NAME.equals(name)) {
2320 mCurOverflow = obj;
2321 }
2322 }
2323
2324 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002325 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002326 if (mActiveOverflow != null) {
2327 if (mActiveOverflow.size() == 0) {
2328 mActiveOverflow = null;
2329 }
2330 }
2331 if (mActiveOverflow == null) {
2332 // There is no currently active overflow, so we should no longer have
2333 // an overflow entry.
2334 if (mMap.containsKey(OVERFLOW_NAME)) {
2335 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2336 + mMap.get(OVERFLOW_NAME));
2337 mMap.remove(OVERFLOW_NAME);
2338 }
2339 mCurOverflow = null;
2340 } else {
2341 // There is currently active overflow, so we should still have an overflow entry.
2342 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2343 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2344 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2345 }
2346 }
2347 }
2348
2349 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002350 if (name == null) {
2351 name = "";
2352 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002353 T obj = mMap.get(name);
2354 if (obj != null) {
2355 return obj;
2356 }
2357
2358 // No object exists for the given name, but do we currently have it
2359 // running as part of the overflow?
2360 if (mActiveOverflow != null) {
2361 MutableInt over = mActiveOverflow.get(name);
2362 if (over != null) {
2363 // We are already actively counting this name in the overflow object.
2364 obj = mCurOverflow;
2365 if (obj == null) {
2366 // Shouldn't be here, but we'll try to recover.
2367 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2368 obj = mCurOverflow = instantiateObject();
2369 mMap.put(OVERFLOW_NAME, obj);
2370 }
2371 over.value++;
2372 return obj;
2373 }
2374 }
2375
2376 // No object exists for given name nor in the overflow; we need to make
2377 // a new one.
2378 final int N = mMap.size();
2379 if (N >= MAX_WAKELOCKS_PER_UID) {
2380 // Went over the limit on number of objects to track; this one goes
2381 // in to the overflow.
2382 obj = mCurOverflow;
2383 if (obj == null) {
2384 // Need to start overflow now...
2385 obj = mCurOverflow = instantiateObject();
2386 mMap.put(OVERFLOW_NAME, obj);
2387 }
2388 if (mActiveOverflow == null) {
2389 mActiveOverflow = new ArrayMap<>();
2390 }
2391 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002392 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002393 return obj;
2394 }
2395
2396 // Normal case where we just need to make a new object.
2397 obj = instantiateObject();
2398 mMap.put(name, obj);
2399 return obj;
2400 }
2401
2402 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002403 if (name == null) {
2404 name = "";
2405 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002406 T obj = mMap.get(name);
2407 if (obj != null) {
2408 return obj;
2409 }
2410
2411 // No object exists for the given name, but do we currently have it
2412 // running as part of the overflow?
2413 if (mActiveOverflow != null) {
2414 MutableInt over = mActiveOverflow.get(name);
2415 if (over != null) {
2416 // We are already actively counting this name in the overflow object.
2417 obj = mCurOverflow;
2418 if (obj != null) {
2419 over.value--;
2420 if (over.value <= 0) {
2421 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002422 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002423 }
2424 return obj;
2425 }
2426 }
2427 }
2428
2429 // Huh, they are stopping an active operation but we can't find one!
2430 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002431 StringBuilder sb = new StringBuilder();
2432 sb.append("Unable to find object for ");
2433 sb.append(name);
2434 sb.append(" in uid ");
2435 sb.append(mUid);
2436 sb.append(" mapsize=");
2437 sb.append(mMap.size());
2438 sb.append(" activeoverflow=");
2439 sb.append(mActiveOverflow);
2440 sb.append(" curoverflow=");
2441 sb.append(mCurOverflow);
2442 long now = SystemClock.elapsedRealtime();
2443 if (mLastOverflowTime != 0) {
2444 sb.append(" lastOverflowTime=");
2445 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2446 }
2447 if (mLastOverflowFinishTime != 0) {
2448 sb.append(" lastOverflowFinishTime=");
2449 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2450 }
2451 if (mLastClearTime != 0) {
2452 sb.append(" lastClearTime=");
2453 TimeUtils.formatDuration(mLastClearTime-now, sb);
2454 }
2455 if (mLastCleanupTime != 0) {
2456 sb.append(" lastCleanupTime=");
2457 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2458 }
2459 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002460 return null;
2461 }
2462
2463 public abstract T instantiateObject();
2464 }
2465
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002466 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2467 implements Parcelable {
2468 private final LongSamplingCounter mIdleTimeMillis;
2469 private final LongSamplingCounter mRxTimeMillis;
2470 private final LongSamplingCounter[] mTxTimeMillis;
2471 private final LongSamplingCounter mPowerDrainMaMs;
2472
2473 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2474 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2475 mRxTimeMillis = new LongSamplingCounter(timeBase);
2476 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2477 for (int i = 0; i < numTxStates; i++) {
2478 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2479 }
2480 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2481 }
2482
2483 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2484 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2485 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2486 final int recordedTxStates = in.readInt();
2487 if (recordedTxStates != numTxStates) {
2488 throw new ParcelFormatException("inconsistent tx state lengths");
2489 }
2490
2491 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2492 for (int i = 0; i < numTxStates; i++) {
2493 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2494 }
2495 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2496 }
2497
2498 public void readSummaryFromParcel(Parcel in) {
2499 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2500 mRxTimeMillis.readSummaryFromParcelLocked(in);
2501 final int recordedTxStates = in.readInt();
2502 if (recordedTxStates != mTxTimeMillis.length) {
2503 throw new ParcelFormatException("inconsistent tx state lengths");
2504 }
2505 for (LongSamplingCounter counter : mTxTimeMillis) {
2506 counter.readSummaryFromParcelLocked(in);
2507 }
2508 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2509 }
2510
2511 @Override
2512 public int describeContents() {
2513 return 0;
2514 }
2515
2516 public void writeSummaryToParcel(Parcel dest) {
2517 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2518 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2519 dest.writeInt(mTxTimeMillis.length);
2520 for (LongSamplingCounter counter : mTxTimeMillis) {
2521 counter.writeSummaryFromParcelLocked(dest);
2522 }
2523 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2524 }
2525
2526 @Override
2527 public void writeToParcel(Parcel dest, int flags) {
2528 mIdleTimeMillis.writeToParcel(dest);
2529 mRxTimeMillis.writeToParcel(dest);
2530 dest.writeInt(mTxTimeMillis.length);
2531 for (LongSamplingCounter counter : mTxTimeMillis) {
2532 counter.writeToParcel(dest);
2533 }
2534 mPowerDrainMaMs.writeToParcel(dest);
2535 }
2536
2537 public void reset(boolean detachIfReset) {
2538 mIdleTimeMillis.reset(detachIfReset);
2539 mRxTimeMillis.reset(detachIfReset);
2540 for (LongSamplingCounter counter : mTxTimeMillis) {
2541 counter.reset(detachIfReset);
2542 }
2543 mPowerDrainMaMs.reset(detachIfReset);
2544 }
2545
2546 public void detach() {
2547 mIdleTimeMillis.detach();
2548 mRxTimeMillis.detach();
2549 for (LongSamplingCounter counter : mTxTimeMillis) {
2550 counter.detach();
2551 }
2552 mPowerDrainMaMs.detach();
2553 }
2554
2555 /**
2556 * @return a LongSamplingCounter, measuring time spent in the idle state in
2557 * milliseconds.
2558 */
2559 @Override
2560 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002561 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002562 }
2563
2564 /**
2565 * @return a LongSamplingCounter, measuring time spent in the receive state in
2566 * milliseconds.
2567 */
2568 @Override
2569 public LongSamplingCounter getRxTimeCounter() {
2570 return mRxTimeMillis;
2571 }
2572
2573 /**
2574 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2575 * milliseconds.
2576 */
2577 @Override
2578 public LongSamplingCounter[] getTxTimeCounters() {
2579 return mTxTimeMillis;
2580 }
2581
2582 /**
2583 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2584 */
2585 @Override
2586 public LongSamplingCounter getPowerCounter() {
2587 return mPowerDrainMaMs;
2588 }
2589 }
2590
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002591 /*
2592 * Get the wakeup reason counter, and create a new one if one
2593 * doesn't already exist.
2594 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002595 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2596 SamplingTimer timer = mWakeupReasonStats.get(name);
2597 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002598 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002599 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002600 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002601 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002602 }
2603
Evan Millarc64edde2009-04-18 12:26:32 -07002604 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002605 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002606 * doesn't already exist.
2607 */
2608 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2609 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2610 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002611 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002612 mKernelWakelockStats.put(name, kwlt);
2613 }
2614 return kwlt;
2615 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002616
James Carr3a226052016-07-01 14:49:52 -07002617 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2618 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2619 if (kmt == null) {
2620 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2621 mKernelMemoryStats.put(bucket, kmt);
2622 }
2623 return kmt;
2624 }
2625
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002626 private int writeHistoryTag(HistoryTag tag) {
2627 Integer idxObj = mHistoryTagPool.get(tag);
2628 int idx;
2629 if (idxObj != null) {
2630 idx = idxObj;
2631 } else {
2632 idx = mNextHistoryTagIdx;
2633 HistoryTag key = new HistoryTag();
2634 key.setTo(tag);
2635 tag.poolIdx = idx;
2636 mHistoryTagPool.put(key, idx);
2637 mNextHistoryTagIdx++;
2638 mNumHistoryTagChars += key.string.length() + 1;
2639 }
2640 return idx;
2641 }
2642
2643 private void readHistoryTag(int index, HistoryTag tag) {
2644 tag.string = mReadHistoryStrings[index];
2645 tag.uid = mReadHistoryUids[index];
2646 tag.poolIdx = index;
2647 }
2648
Adam Lesinski926969b2016-04-28 17:31:12 -07002649 /*
2650 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2651
2652 There is always the first token, which may contain the delta time, or an indicator of
2653 the length of the time (int or long) following this token.
2654
2655 First token: always present,
2656 31 23 15 7 0
2657 â–ˆ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â–ˆ
2658
2659 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2660 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2661 delta time.
2662 A: battery level changed and an int follows with battery data.
2663 B: state changed and an int follows with state change data.
2664 C: state2 has changed and an int follows with state2 change data.
2665 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2666 E: event data has changed and an event struct follows.
2667 F: battery charge in coulombs has changed and an int with the charge follows.
2668 G: state flag denoting that the mobile radio was active.
2669 H: state flag denoting that the wifi radio was active.
2670 I: state flag denoting that a wifi scan occurred.
2671 J: state flag denoting that a wifi full lock was held.
2672 K: state flag denoting that the gps was on.
2673 L: state flag denoting that a wakelock was held.
2674 M: state flag denoting that the cpu was running.
2675
2676 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2677 with the time delta.
2678
2679 Battery level int: if A in the first token is set,
2680 31 23 15 7 0
2681 â–ˆ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â–ˆ
2682
2683 D: indicates that extra history details follow.
2684 V: the battery voltage.
2685 T: the battery temperature.
2686 L: the battery level (out of 100).
2687
2688 State change int: if B in the first token is set,
2689 31 23 15 7 0
2690 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2691
2692 A: wifi multicast was on.
2693 B: battery was plugged in.
2694 C: screen was on.
2695 D: phone was scanning for signal.
2696 E: audio was on.
2697 F: a sensor was active.
2698
2699 State2 change int: if C in the first token is set,
2700 31 23 15 7 0
2701 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2702
2703 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2704 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2705 C: a bluetooth scan was active.
2706 D: the camera was active.
2707 E: bluetooth was on.
2708 F: a phone call was active.
2709 G: the device was charging.
2710 H: 2 bits indicating the device-idle (doze) state: off, light, full
2711 I: the flashlight was on.
2712 J: wifi was on.
2713 K: wifi was running.
2714 L: video was playing.
2715 M: power save mode was on.
2716
2717 Wakelock/wakereason struct: if D in the first token is set,
2718 TODO(adamlesinski): describe wakelock/wakereason struct.
2719
2720 Event struct: if E in the first token is set,
2721 TODO(adamlesinski): describe the event struct.
2722
2723 History step details struct: if D in the battery level int is set,
2724 TODO(adamlesinski): describe the history step details struct.
2725
2726 Battery charge int: if F in the first token is set, an int representing the battery charge
2727 in coulombs follows.
2728 */
2729
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002730 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002731 static final int DELTA_TIME_MASK = 0x7ffff;
2732 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2733 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2734 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002735 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002736 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002737 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002738 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002739 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002740 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002741 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002742 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002743 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002744 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002745 // Flag in delta int: contains the battery charge count in uAh.
2746 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002747 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002748 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002749
2750 // These are the pieces of battery state that are packed in to the upper bits of
2751 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002752 // in STATE_BATTERY_MASK.
2753 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002754 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2755 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2756 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2757 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2758 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2759 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002760
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002761 // We use the low bit of the battery state int to indicate that we have full details
2762 // from a battery level change.
2763 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2764
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002765 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002766 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002767 dest.writeInt(DELTA_TIME_ABS);
2768 cur.writeToParcel(dest, 0);
2769 return;
2770 }
2771
2772 final long deltaTime = cur.time - last.time;
2773 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2774 final int lastStateInt = buildStateInt(last);
2775
2776 int deltaTimeToken;
2777 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2778 deltaTimeToken = DELTA_TIME_LONG;
2779 } else if (deltaTime >= DELTA_TIME_ABS) {
2780 deltaTimeToken = DELTA_TIME_INT;
2781 } else {
2782 deltaTimeToken = (int)deltaTime;
2783 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002784 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002785 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2786 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2787 final boolean computeStepDetails = includeStepDetails != 0
2788 || mLastHistoryStepDetails == null;
2789 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002790 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2791 if (batteryLevelIntChanged) {
2792 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2793 }
2794 final int stateInt = buildStateInt(cur);
2795 final boolean stateIntChanged = stateInt != lastStateInt;
2796 if (stateIntChanged) {
2797 firstToken |= DELTA_STATE_FLAG;
2798 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002799 final boolean state2IntChanged = cur.states2 != last.states2;
2800 if (state2IntChanged) {
2801 firstToken |= DELTA_STATE2_FLAG;
2802 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002803 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002804 firstToken |= DELTA_WAKELOCK_FLAG;
2805 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002806 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2807 firstToken |= DELTA_EVENT_FLAG;
2808 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002809
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002810 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2811 if (batteryChargeChanged) {
2812 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002813 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002814 dest.writeInt(firstToken);
2815 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2816 + " deltaTime=" + deltaTime);
2817
2818 if (deltaTimeToken >= DELTA_TIME_INT) {
2819 if (deltaTimeToken == DELTA_TIME_INT) {
2820 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2821 dest.writeInt((int)deltaTime);
2822 } else {
2823 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2824 dest.writeLong(deltaTime);
2825 }
2826 }
2827 if (batteryLevelIntChanged) {
2828 dest.writeInt(batteryLevelInt);
2829 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2830 + Integer.toHexString(batteryLevelInt)
2831 + " batteryLevel=" + cur.batteryLevel
2832 + " batteryTemp=" + cur.batteryTemperature
2833 + " batteryVolt=" + (int)cur.batteryVoltage);
2834 }
2835 if (stateIntChanged) {
2836 dest.writeInt(stateInt);
2837 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2838 + Integer.toHexString(stateInt)
2839 + " batteryStatus=" + cur.batteryStatus
2840 + " batteryHealth=" + cur.batteryHealth
2841 + " batteryPlugType=" + cur.batteryPlugType
2842 + " states=0x" + Integer.toHexString(cur.states));
2843 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002844 if (state2IntChanged) {
2845 dest.writeInt(cur.states2);
2846 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2847 + Integer.toHexString(cur.states2));
2848 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002849 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2850 int wakeLockIndex;
2851 int wakeReasonIndex;
2852 if (cur.wakelockTag != null) {
2853 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2854 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2855 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2856 } else {
2857 wakeLockIndex = 0xffff;
2858 }
2859 if (cur.wakeReasonTag != null) {
2860 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2861 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2862 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2863 } else {
2864 wakeReasonIndex = 0xffff;
2865 }
2866 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002867 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002868 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002869 int index = writeHistoryTag(cur.eventTag);
2870 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002871 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002872 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2873 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2874 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002875 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002876 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002877 if (mPlatformIdleStateCallback != null) {
2878 mCurHistoryStepDetails.statPlatformIdleState =
2879 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2880 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2881 mCurHistoryStepDetails.statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00002882
2883 mCurHistoryStepDetails.statSubsystemPowerState =
2884 mPlatformIdleStateCallback.getSubsystemLowPowerStats();
2885 if (DEBUG) Slog.i(TAG, "WRITE SubsystemPowerState:" +
2886 mCurHistoryStepDetails.statSubsystemPowerState);
2887
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002888 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002889 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2890 if (includeStepDetails != 0) {
2891 mCurHistoryStepDetails.writeToParcel(dest);
2892 }
2893 cur.stepDetails = mCurHistoryStepDetails;
2894 mLastHistoryStepDetails = mCurHistoryStepDetails;
2895 } else {
2896 cur.stepDetails = null;
2897 }
2898 if (mLastHistoryStepLevel < cur.batteryLevel) {
2899 mLastHistoryStepDetails = null;
2900 }
2901 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002902
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002903 if (batteryChargeChanged) {
2904 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2905 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002906 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002907 }
2908
2909 private int buildBatteryLevelInt(HistoryItem h) {
2910 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002911 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2912 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2913 }
2914
2915 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2916 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2917 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2918 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002919 }
2920
2921 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002922 int plugType = 0;
2923 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2924 plugType = 1;
2925 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2926 plugType = 2;
2927 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2928 plugType = 3;
2929 }
2930 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2931 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2932 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002933 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002934 }
2935
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002936 private void computeHistoryStepDetails(final HistoryStepDetails out,
2937 final HistoryStepDetails last) {
2938 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2939
2940 // Perform a CPU update right after we do this collection, so we have started
2941 // collecting good data for the next step.
2942 requestImmediateCpuUpdate();
2943
2944 if (last == null) {
2945 // We are not generating a delta, so all we need to do is reset the stats
2946 // we will later be doing a delta from.
2947 final int NU = mUidStats.size();
2948 for (int i=0; i<NU; i++) {
2949 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2950 uid.mLastStepUserTime = uid.mCurStepUserTime;
2951 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2952 }
2953 mLastStepCpuUserTime = mCurStepCpuUserTime;
2954 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2955 mLastStepStatUserTime = mCurStepStatUserTime;
2956 mLastStepStatSystemTime = mCurStepStatSystemTime;
2957 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2958 mLastStepStatIrqTime = mCurStepStatIrqTime;
2959 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2960 mLastStepStatIdleTime = mCurStepStatIdleTime;
2961 tmp.clear();
2962 return;
2963 }
2964 if (DEBUG) {
2965 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2966 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2967 + " irq=" + mLastStepStatIrqTime + " sirq="
2968 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2969 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2970 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2971 + " irq=" + mCurStepStatIrqTime + " sirq="
2972 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2973 }
2974 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2975 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2976 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2977 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2978 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2979 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2980 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2981 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2982 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2983 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2984 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2985 final int NU = mUidStats.size();
2986 for (int i=0; i<NU; i++) {
2987 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2988 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2989 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2990 final int totalTime = totalUTime + totalSTime;
2991 uid.mLastStepUserTime = uid.mCurStepUserTime;
2992 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2993 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2994 continue;
2995 }
2996 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2997 out.appCpuUid3 = uid.mUid;
2998 out.appCpuUTime3 = totalUTime;
2999 out.appCpuSTime3 = totalSTime;
3000 } else {
3001 out.appCpuUid3 = out.appCpuUid2;
3002 out.appCpuUTime3 = out.appCpuUTime2;
3003 out.appCpuSTime3 = out.appCpuSTime2;
3004 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
3005 out.appCpuUid2 = uid.mUid;
3006 out.appCpuUTime2 = totalUTime;
3007 out.appCpuSTime2 = totalSTime;
3008 } else {
3009 out.appCpuUid2 = out.appCpuUid1;
3010 out.appCpuUTime2 = out.appCpuUTime1;
3011 out.appCpuSTime2 = out.appCpuSTime1;
3012 out.appCpuUid1 = uid.mUid;
3013 out.appCpuUTime1 = totalUTime;
3014 out.appCpuSTime1 = totalSTime;
3015 }
3016 }
3017 }
3018 mLastStepCpuUserTime = mCurStepCpuUserTime;
3019 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3020 mLastStepStatUserTime = mCurStepStatUserTime;
3021 mLastStepStatSystemTime = mCurStepStatSystemTime;
3022 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3023 mLastStepStatIrqTime = mCurStepStatIrqTime;
3024 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3025 mLastStepStatIdleTime = mCurStepStatIdleTime;
3026 }
3027
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003028 public void readHistoryDelta(Parcel src, HistoryItem cur) {
3029 int firstToken = src.readInt();
3030 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003031 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003032 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003033 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
3034 + " deltaTimeToken=" + deltaTimeToken);
3035
3036 if (deltaTimeToken < DELTA_TIME_ABS) {
3037 cur.time += deltaTimeToken;
3038 } else if (deltaTimeToken == DELTA_TIME_ABS) {
3039 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003040 cur.numReadInts += 2;
3041 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003042 cur.readFromParcel(src);
3043 return;
3044 } else if (deltaTimeToken == DELTA_TIME_INT) {
3045 int delta = src.readInt();
3046 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003047 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003048 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3049 } else {
3050 long delta = src.readLong();
3051 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3052 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003053 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003054 }
3055
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003056 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003057 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003058 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08003059 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003060 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003061 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
3062 + Integer.toHexString(batteryLevelInt)
3063 + " batteryLevel=" + cur.batteryLevel
3064 + " batteryTemp=" + cur.batteryTemperature
3065 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003066 } else {
3067 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003068 }
3069
3070 if ((firstToken&DELTA_STATE_FLAG) != 0) {
3071 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003072 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003073 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
3074 & STATE_BATTERY_STATUS_MASK);
3075 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
3076 & STATE_BATTERY_HEALTH_MASK);
3077 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
3078 & STATE_BATTERY_PLUG_MASK);
3079 switch (cur.batteryPlugType) {
3080 case 1:
3081 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
3082 break;
3083 case 2:
3084 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
3085 break;
3086 case 3:
3087 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
3088 break;
3089 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003090 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003091 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
3092 + Integer.toHexString(stateInt)
3093 + " batteryStatus=" + cur.batteryStatus
3094 + " batteryHealth=" + cur.batteryHealth
3095 + " batteryPlugType=" + cur.batteryPlugType
3096 + " states=0x" + Integer.toHexString(cur.states));
3097 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07003098 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003099 }
3100
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003101 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
3102 cur.states2 = src.readInt();
3103 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
3104 + Integer.toHexString(cur.states2));
3105 }
3106
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003107 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003108 int indexes = src.readInt();
3109 int wakeLockIndex = indexes&0xffff;
3110 int wakeReasonIndex = (indexes>>16)&0xffff;
3111 if (wakeLockIndex != 0xffff) {
3112 cur.wakelockTag = cur.localWakelockTag;
3113 readHistoryTag(wakeLockIndex, cur.wakelockTag);
3114 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
3115 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
3116 } else {
3117 cur.wakelockTag = null;
3118 }
3119 if (wakeReasonIndex != 0xffff) {
3120 cur.wakeReasonTag = cur.localWakeReasonTag;
3121 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
3122 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
3123 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
3124 } else {
3125 cur.wakeReasonTag = null;
3126 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003127 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003128 } else {
3129 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003130 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003131 }
3132
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003133 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003134 cur.eventTag = cur.localEventTag;
3135 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003136 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003137 final int index = ((codeAndIndex>>16)&0xffff);
3138 readHistoryTag(index, cur.eventTag);
3139 cur.numReadInts += 1;
3140 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
3141 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3142 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003143 } else {
3144 cur.eventCode = HistoryItem.EVENT_NONE;
3145 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003146
3147 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
3148 cur.stepDetails = mReadHistoryStepDetails;
3149 cur.stepDetails.readFromParcel(src);
3150 } else {
3151 cur.stepDetails = null;
3152 }
Adam Lesinski926969b2016-04-28 17:31:12 -07003153
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003154 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
3155 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003156 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003157 }
3158
Dianne Hackbornfc064132014-06-02 12:42:12 -07003159 @Override
3160 public void commitCurrentHistoryBatchLocked() {
3161 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
3162 }
3163
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003164 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003165 if (!mHaveBatteryLevel || !mRecordingHistory) {
3166 return;
3167 }
3168
Dianne Hackborn40c87252014-03-19 16:55:40 -07003169 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003170 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
3171 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003172 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003173 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003174 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
3175 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003176 + Integer.toHexString(lastDiffStates) + " diff2="
3177 + Integer.toHexString(diffStates2) + " lastDiff2="
3178 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003179 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003180 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003181 && (diffStates2&lastDiffStates2) == 0
3182 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3183 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003184 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003185 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003186 || cur.eventCode == HistoryItem.EVENT_NONE)
3187 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3188 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3189 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3190 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3191 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3192 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003193 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003194 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003195 // as long as no bit has changed both between now and the last entry, as
3196 // well as the last entry and the one before it (so we capture any toggles).
3197 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003198 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3199 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3200 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003201 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003202 // If the last written history had a wakelock tag, we need to retain it.
3203 // Note that the condition above made sure that we aren't in a case where
3204 // both it and the current history item have a wakelock tag.
3205 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003206 cur.wakelockTag = cur.localWakelockTag;
3207 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003208 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003209 // If the last written history had a wake reason tag, we need to retain it.
3210 // Note that the condition above made sure that we aren't in a case where
3211 // both it and the current history item have a wakelock tag.
3212 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003213 cur.wakeReasonTag = cur.localWakeReasonTag;
3214 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003215 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003216 // If the last written history had an event, we need to retain it.
3217 // Note that the condition above made sure that we aren't in a case where
3218 // both it and the current history item have an event.
3219 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003220 cur.eventCode = mHistoryLastWritten.eventCode;
3221 cur.eventTag = cur.localEventTag;
3222 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003223 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003224 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003225 }
3226
Adam Lesinski45489782016-12-15 23:45:17 -08003227 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003228 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003229 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3230 // Clients can't deal with history buffers this large. This only
3231 // really happens when the device is on charger and interacted with
3232 // for long periods of time, like in retail mode. Since the device is
3233 // most likely charged, when unplugged, stats would have reset anyways.
3234 // Reset the stats and mark that we overflowed.
3235 // b/32540341
3236 resetAllStatsLocked();
3237
3238 // Mark that we want to set *OVERFLOW* event and the RESET:START
3239 // events.
3240 recordResetDueToOverflow = true;
3241
3242 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003243 if (!mHistoryOverflow) {
3244 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003245 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3246 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003247 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003248 }
3249
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003250 // After overflow, we allow various bit-wise states to settle to 0.
3251 boolean writeAnyway = false;
3252 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3253 & mActiveHistoryStates;
3254 if (mHistoryLastWritten.states != curStates) {
3255 // mActiveHistoryStates keeps track of which bits in .states are now being
3256 // forced to 0.
3257 int old = mActiveHistoryStates;
3258 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3259 writeAnyway |= old != mActiveHistoryStates;
3260 }
3261 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3262 & mActiveHistoryStates2;
3263 if (mHistoryLastWritten.states2 != curStates2) {
3264 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3265 // forced to 0.
3266 int old = mActiveHistoryStates2;
3267 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3268 writeAnyway |= old != mActiveHistoryStates2;
3269 }
3270
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003271 // Once we've reached the maximum number of items, we only
3272 // record changes to the battery level and the most interesting states.
3273 // Once we've reached the maximum maximum number of items, we only
3274 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003275 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003276 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003277 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003278 & HistoryItem.MOST_INTERESTING_STATES) == 0
3279 || ((mHistoryLastWritten.states2^cur.states2)
3280 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003281 return;
3282 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003283
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003284 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003285 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003286 }
3287
Adam Lesinski45489782016-12-15 23:45:17 -08003288 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003289 // The history is currently empty; we need it to start with a time stamp.
3290 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003291 if (recordResetDueToOverflow) {
3292 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3293 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003294 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3295 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003296 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003297 }
3298
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003299 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3300 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003301 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003302 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003303 }
3304 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3305 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003306 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003307 mHistoryLastWritten.states &= mActiveHistoryStates;
3308 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003309 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003310 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003311 cur.wakelockTag = null;
3312 cur.wakeReasonTag = null;
3313 cur.eventCode = HistoryItem.EVENT_NONE;
3314 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003315 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3316 + " now " + mHistoryBuffer.dataPosition()
3317 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003318 }
3319
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003320 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003321 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003322
Dianne Hackborn40c87252014-03-19 16:55:40 -07003323 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003324 if (mTrackRunningHistoryElapsedRealtime != 0) {
3325 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3326 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3327 if (diffUptime < (diffElapsed-20)) {
3328 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3329 mHistoryAddTmp.setTo(mHistoryLastWritten);
3330 mHistoryAddTmp.wakelockTag = null;
3331 mHistoryAddTmp.wakeReasonTag = null;
3332 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3333 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3334 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3335 }
3336 }
3337 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3338 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3339 mTrackRunningHistoryUptime = uptimeMs;
3340 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3341 }
3342
3343 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3344 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003345
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003346 if (!USE_OLD_HISTORY) {
3347 return;
3348 }
3349
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003350 if (!mHaveBatteryLevel || !mRecordingHistory) {
3351 return;
3352 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003353
3354 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003355 // and no states have since the last recorded entry changed and
3356 // are now resetting back to their original value, then just collapse
3357 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003358 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003359 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003360 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3361 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003362 // If the current is the same as the one before, then we no
3363 // longer need the entry.
3364 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003365 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003366 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003367 mHistoryLastEnd.next = null;
3368 mHistoryEnd.next = mHistoryCache;
3369 mHistoryCache = mHistoryEnd;
3370 mHistoryEnd = mHistoryLastEnd;
3371 mHistoryLastEnd = null;
3372 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003373 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3374 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003375 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003376 }
3377 return;
3378 }
3379
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003380 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003381 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003382
3383 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3384 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003385 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003386 }
3387
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003388 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3389 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003390 // record changes to the battery level and the most interesting states.
3391 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003392 // record changes to the battery level.
3393 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003394 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003395 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003396 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003397 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003398 return;
3399 }
3400 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003401
Dianne Hackborn40c87252014-03-19 16:55:40 -07003402 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003403 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003404
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003405 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003406 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003407 mHistoryCur.eventCode = code;
3408 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3409 mHistoryCur.eventTag.string = name;
3410 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003411 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003412 }
3413
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003414 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003415 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003416 if (rec != null) {
3417 mHistoryCache = rec.next;
3418 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003419 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003420 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003421 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003422
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003423 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003424 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003425
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003426 void addHistoryRecordLocked(HistoryItem rec) {
3427 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003428 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003429 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003430 if (mHistoryEnd != null) {
3431 mHistoryEnd.next = rec;
3432 mHistoryEnd = rec;
3433 } else {
3434 mHistory = mHistoryEnd = rec;
3435 }
3436 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003437
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003438 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003439 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003440 if (USE_OLD_HISTORY) {
3441 if (mHistory != null) {
3442 mHistoryEnd.next = mHistoryCache;
3443 mHistoryCache = mHistory;
3444 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3445 }
3446 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003447 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003448
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003449 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003450 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003451 mTrackRunningHistoryElapsedRealtime = 0;
3452 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003453
3454 mHistoryBuffer.setDataSize(0);
3455 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003456 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003457 mHistoryLastLastWritten.clear();
3458 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003459 mHistoryTagPool.clear();
3460 mNextHistoryTagIdx = 0;
3461 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003462 mHistoryBufferLastPos = -1;
3463 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003464 mActiveHistoryStates = 0xffffffff;
3465 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003466 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003467
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003468 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
3469 long realtime) {
Bookatz867c0d72017-03-07 18:23:42 -08003470 boolean batteryStatusChanged = mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
3471
3472 if (batteryStatusChanged) {
Bookatzc8c44962017-05-11 12:12:54 -07003473 for (int i = 0; i < mUidStats.size(); i++) {
3474 mUidStats.valueAt(i).updateOnBatteryBgTimeBase(uptime, realtime);
Bookatz867c0d72017-03-07 18:23:42 -08003475 }
3476 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003477
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003478 boolean unpluggedScreenOff = unplugged && screenOff;
3479 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
3480 updateKernelWakelocksLocked();
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003481 updateBatteryPropertiesLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07003482 if (DEBUG_ENERGY_CPU) {
3483 Slog.d(TAG, "Updating cpu time because screen is now " +
3484 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003485 }
Sudheer Shanka671985f2017-05-19 11:33:42 -07003486 updateCpuTimeLocked(true /* updateCpuFreqData */);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003487 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
Bookatzc8c44962017-05-11 12:12:54 -07003488 for (int i = 0; i < mUidStats.size(); i++) {
3489 mUidStats.valueAt(i).updateOnBatteryScreenOffBgTimeBase(uptime, realtime);
3490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 }
3492 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003493
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003494 private void updateBatteryPropertiesLocked() {
3495 try {
3496 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3497 ServiceManager.getService("batteryproperties"));
3498 registrar.scheduleUpdate();
3499 } catch (RemoteException e) {
3500 // Ignore.
3501 }
3502 }
3503
Dianne Hackborn099bc622014-01-22 13:39:16 -08003504 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3505 mIsolatedUids.put(isolatedUid, appUid);
3506 }
3507
Adam Lesinski61db88f2015-07-01 15:05:07 -07003508 /**
3509 * Schedules a read of the latest cpu times before removing the isolated UID.
3510 * @see #removeIsolatedUidLocked(int)
3511 */
3512 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003513 int curUid = mIsolatedUids.get(isolatedUid, -1);
3514 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003515 if (mExternalSync != null) {
3516 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3517 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003518 }
3519 }
3520
Adam Lesinski61db88f2015-07-01 15:05:07 -07003521 /**
3522 * This should only be called after the cpu times have been read.
3523 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3524 */
3525 public void removeIsolatedUidLocked(int isolatedUid) {
3526 mIsolatedUids.delete(isolatedUid);
3527 mKernelUidCpuTimeReader.removeUid(isolatedUid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -07003528 mKernelUidCpuFreqTimeReader.removeUid(isolatedUid);
Adam Lesinski61db88f2015-07-01 15:05:07 -07003529 }
3530
Dianne Hackborn099bc622014-01-22 13:39:16 -08003531 public int mapUid(int uid) {
3532 int isolated = mIsolatedUids.get(uid, -1);
3533 return isolated > 0 ? isolated : uid;
3534 }
3535
3536 public void noteEventLocked(int code, String name, int uid) {
3537 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003538 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3539 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003540 }
Joe Onoratoabded112016-02-08 16:49:39 -08003541 final long elapsedRealtime = mClocks.elapsedRealtime();
3542 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003543 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003544 }
3545
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003546 boolean ensureStartClockTime(final long currentTime) {
3547 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3548 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3549 // If the start clock time has changed by more than a year, then presumably
3550 // the previous time was completely bogus. So we are going to figure out a
3551 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003552 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003553 return true;
3554 }
3555 return false;
3556 }
3557
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003558 public void noteCurrentTimeChangedLocked() {
3559 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003560 final long elapsedRealtime = mClocks.elapsedRealtime();
3561 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003562 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003563 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003564 }
3565
Dianne Hackborn61659e52014-07-09 16:13:01 -07003566 public void noteProcessStartLocked(String name, int uid) {
3567 uid = mapUid(uid);
3568 if (isOnBattery()) {
3569 Uid u = getUidStatsLocked(uid);
3570 u.getProcessStatsLocked(name).incStartsLocked();
3571 }
3572 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3573 return;
3574 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003575 if (!mRecordAllHistory) {
3576 return;
3577 }
Joe Onoratoabded112016-02-08 16:49:39 -08003578 final long elapsedRealtime = mClocks.elapsedRealtime();
3579 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003580 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3581 }
3582
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003583 public void noteProcessCrashLocked(String name, int uid) {
3584 uid = mapUid(uid);
3585 if (isOnBattery()) {
3586 Uid u = getUidStatsLocked(uid);
3587 u.getProcessStatsLocked(name).incNumCrashesLocked();
3588 }
3589 }
3590
3591 public void noteProcessAnrLocked(String name, int uid) {
3592 uid = mapUid(uid);
3593 if (isOnBattery()) {
3594 Uid u = getUidStatsLocked(uid);
3595 u.getProcessStatsLocked(name).incNumAnrsLocked();
3596 }
3597 }
3598
Dianne Hackborna8d10942015-11-19 17:55:19 -08003599 public void noteUidProcessStateLocked(int uid, int state) {
Amith Yamasanifd3caf62017-07-26 11:48:35 -07003600 int parentUid = mapUid(uid);
3601 if (uid != parentUid) {
3602 // Isolated UIDs process state is already rolled up into parent, so no need to track
3603 // Otherwise the parent's process state will get downgraded incorrectly
3604 return;
3605 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08003606 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003607 }
3608
3609 public void noteProcessFinishLocked(String name, int uid) {
3610 uid = mapUid(uid);
3611 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3612 return;
3613 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003614 if (!mRecordAllHistory) {
3615 return;
3616 }
Joe Onoratoabded112016-02-08 16:49:39 -08003617 final long elapsedRealtime = mClocks.elapsedRealtime();
3618 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003619 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003620 }
3621
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003622 public void noteSyncStartLocked(String name, int uid) {
3623 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003624 final long elapsedRealtime = mClocks.elapsedRealtime();
3625 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003626 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3627 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3628 return;
3629 }
3630 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3631 }
3632
3633 public void noteSyncFinishLocked(String name, int uid) {
3634 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003635 final long elapsedRealtime = mClocks.elapsedRealtime();
3636 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003637 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3638 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3639 return;
3640 }
3641 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3642 }
3643
3644 public void noteJobStartLocked(String name, int uid) {
3645 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003646 final long elapsedRealtime = mClocks.elapsedRealtime();
3647 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003648 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3649 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3650 return;
3651 }
3652 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3653 }
3654
3655 public void noteJobFinishLocked(String name, int uid) {
3656 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003657 final long elapsedRealtime = mClocks.elapsedRealtime();
3658 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003659 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3660 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3661 return;
3662 }
3663 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3664 }
3665
Dianne Hackborn1e383822015-04-10 14:02:33 -07003666 public void noteAlarmStartLocked(String name, int uid) {
3667 if (!mRecordAllHistory) {
3668 return;
3669 }
3670 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003671 final long elapsedRealtime = mClocks.elapsedRealtime();
3672 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003673 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3674 return;
3675 }
3676 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3677 }
3678
3679 public void noteAlarmFinishLocked(String name, int uid) {
3680 if (!mRecordAllHistory) {
3681 return;
3682 }
3683 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003684 final long elapsedRealtime = mClocks.elapsedRealtime();
3685 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003686 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3687 return;
3688 }
3689 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3690 }
3691
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003692 private void requestWakelockCpuUpdate() {
3693 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3694 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3695 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3696 }
3697 }
3698
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003699 private void requestImmediateCpuUpdate() {
3700 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3701 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3702 }
3703
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003704 public void setRecordAllHistoryLocked(boolean enabled) {
3705 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003706 if (!enabled) {
3707 // Clear out any existing state.
3708 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003709 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003710 // Record the currently running processes as stopping, now that we are no
3711 // longer tracking them.
3712 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3713 HistoryItem.EVENT_PROC);
3714 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003715 long mSecRealtime = mClocks.elapsedRealtime();
3716 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003717 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3718 SparseIntArray uids = ent.getValue();
3719 for (int j=0; j<uids.size(); j++) {
3720 addHistoryEventLocked(mSecRealtime, mSecUptime,
3721 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3722 }
3723 }
3724 }
3725 } else {
3726 // Record the currently running processes as starting, now that we are tracking them.
3727 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3728 HistoryItem.EVENT_PROC);
3729 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003730 long mSecRealtime = mClocks.elapsedRealtime();
3731 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003732 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3733 SparseIntArray uids = ent.getValue();
3734 for (int j=0; j<uids.size(); j++) {
3735 addHistoryEventLocked(mSecRealtime, mSecUptime,
3736 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3737 }
3738 }
3739 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003740 }
3741 }
3742
Dianne Hackborn9a755432014-05-15 17:05:22 -07003743 public void setNoAutoReset(boolean enabled) {
3744 mNoAutoReset = enabled;
3745 }
3746
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003747 public void setPretendScreenOff(boolean pretendScreenOff) {
3748 mPretendScreenOff = pretendScreenOff;
3749 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3750 }
3751
Dianne Hackborn9a755432014-05-15 17:05:22 -07003752 private String mInitialAcquireWakeName;
3753 private int mInitialAcquireWakeUid = -1;
3754
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003755 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003756 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003757 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003758 if (type == WAKE_TYPE_PARTIAL) {
3759 // Only care about partial wake locks, since full wake locks
3760 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003761 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003762 if (historyName == null) {
3763 historyName = name;
3764 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003765 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003766 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3767 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003768 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003769 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003770 }
3771 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003772 if (mWakeLockNesting == 0) {
3773 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3774 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3775 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003776 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003777 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003778 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003779 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003780 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003781 } else if (!mWakeLockImportant && !unimportantForLogging
3782 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003783 if (mHistoryLastWritten.wakelockTag != null) {
3784 // We'll try to update the last tag.
3785 mHistoryLastWritten.wakelockTag = null;
3786 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003787 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003788 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003789 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003790 }
3791 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003792 }
3793 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003794 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003795 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003796 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3797 // We only update the cpu time when a wake lock is acquired if the screen is off.
3798 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3799 if (DEBUG_ENERGY_CPU) {
3800 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3801 }
3802 requestWakelockCpuUpdate();
3803 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003804 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003805 }
3806 }
3807
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003808 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3809 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003810 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003811 if (type == WAKE_TYPE_PARTIAL) {
3812 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003813 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003814 if (historyName == null) {
3815 historyName = name;
3816 }
3817 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3818 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003819 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003820 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003821 }
3822 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003823 if (mWakeLockNesting == 0) {
3824 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3825 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3826 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003827 mInitialAcquireWakeName = null;
3828 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003829 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003830 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003831 }
3832 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003833 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3834 if (DEBUG_ENERGY_CPU) {
3835 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3836 }
3837 requestWakelockCpuUpdate();
3838 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003839 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003840 }
3841 }
3842
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003843 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3844 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003845 final long elapsedRealtime = mClocks.elapsedRealtime();
3846 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003847 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003848 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003849 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003850 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003851 }
3852 }
3853
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003854 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3855 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003856 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003857 final long elapsedRealtime = mClocks.elapsedRealtime();
3858 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003859 // For correct semantics, we start the need worksources first, so that we won't
3860 // make inappropriate history items as if all wake locks went away and new ones
3861 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003862 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003863 for (int i=0; i<NN; i++) {
3864 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003865 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003866 }
3867 final int NO = ws.size();
3868 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003869 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003870 }
3871 }
3872
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003873 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3874 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003875 final long elapsedRealtime = mClocks.elapsedRealtime();
3876 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003877 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003878 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003879 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003880 }
3881 }
3882
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003883 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3884 uid = mapUid(uid);
3885 final long elapsedRealtime = mClocks.elapsedRealtime();
3886 final long uptime = mClocks.uptimeMillis();
3887 if (historyName == null) {
3888 historyName = name;
3889 }
3890 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3891 0)) {
3892 return;
3893 }
3894 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3895 historyName, uid);
3896 }
3897
3898 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3899 uid = mapUid(uid);
3900 final long elapsedRealtime = mClocks.elapsedRealtime();
3901 final long uptime = mClocks.uptimeMillis();
3902 if (historyName == null) {
3903 historyName = name;
3904 }
3905 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3906 0)) {
3907 return;
3908 }
3909 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3910 historyName, uid);
3911 }
3912
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003913 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3914 if (mLastWakeupReason != null) {
3915 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003916 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003917 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003918 mLastWakeupReason = null;
3919 }
3920 }
3921
3922 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003923 final long elapsedRealtime = mClocks.elapsedRealtime();
3924 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003925 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003926 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003927 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003928 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3929 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003930 mHistoryCur.wakeReasonTag.uid = 0;
3931 mLastWakeupReason = reason;
3932 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003933 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003934 }
3935
Adam Lesinski72478f02015-06-17 15:39:43 -07003936 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003937 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003938 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003939 }
3940
Adam Lesinski72478f02015-06-17 15:39:43 -07003941 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3942 int statSystemTime, int statIOWaitTime, int statIrqTime,
3943 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003944 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3945 + " user=" + statUserTime + " sys=" + statSystemTime
3946 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3947 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3948 mCurStepCpuUserTime += totalUTime;
3949 mCurStepCpuSystemTime += totalSTime;
3950 mCurStepStatUserTime += statUserTime;
3951 mCurStepStatSystemTime += statSystemTime;
3952 mCurStepStatIOWaitTime += statIOWaitTime;
3953 mCurStepStatIrqTime += statIrqTime;
3954 mCurStepStatSoftIrqTime += statSoftIrqTime;
3955 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003956 }
3957
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003958 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003959 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003960 Uid u = mUidStats.get(uid);
3961 if (u != null) {
3962 u.mPids.remove(pid);
3963 }
3964 }
3965
3966 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003967 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003968 Uid u = mUidStats.get(uid);
3969 if (u != null) {
3970 Uid.Pid p = u.mPids.get(pid);
3971 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003972 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003973 }
3974 }
3975 return 0;
3976 }
3977
3978 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003979 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003980 Uid u = mUidStats.get(uid);
3981 if (u != null) {
3982 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3983 }
3984 }
3985
Dianne Hackborn287952c2010-09-22 22:34:31 -07003986 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003987 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003988 Uid u = mUidStats.get(uid);
3989 if (u != null) {
3990 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3991 }
3992 }
3993
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003994 int mSensorNesting;
3995
3996 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003997 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003998 final long elapsedRealtime = mClocks.elapsedRealtime();
3999 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004000 if (mSensorNesting == 0) {
4001 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
4002 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
4003 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004004 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004005 }
4006 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004007 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004008 }
4009
4010 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004011 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004012 final long elapsedRealtime = mClocks.elapsedRealtime();
4013 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004014 mSensorNesting--;
4015 if (mSensorNesting == 0) {
4016 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
4017 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
4018 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004019 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004020 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004021 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004022 }
4023
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004024 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004025
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004026 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004027 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004028 final long elapsedRealtime = mClocks.elapsedRealtime();
4029 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004030 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004031 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004032 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
4033 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004034 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004035 }
4036 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004037 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004039
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004040 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004041 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004042 final long elapsedRealtime = mClocks.elapsedRealtime();
4043 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004044 mGpsNesting--;
4045 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004046 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004047 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
4048 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004049 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004050 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004051 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004053
Jeff Browne95c3cd2014-05-02 16:59:26 -07004054 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004055 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08004056
4057 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
4058 // original 4 are mapped to one of the originals.
4059 if (state > MAX_TRACKED_SCREEN_STATE) {
4060 switch (state) {
4061 case Display.STATE_VR:
4062 state = Display.STATE_ON;
4063 break;
4064 default:
4065 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
4066 break;
4067 }
4068 }
4069
Jeff Browne95c3cd2014-05-02 16:59:26 -07004070 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004071 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004072 final int oldState = mScreenState;
4073 mScreenState = state;
4074 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
4075 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004076
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004077 if (state != Display.STATE_UNKNOWN) {
4078 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08004079 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
4080 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
4081 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004082 } else {
4083 Slog.wtf(TAG, "Unexpected screen state: " + state);
4084 }
4085 }
4086
Jeff Browne95c3cd2014-05-02 16:59:26 -07004087 if (state == Display.STATE_ON) {
4088 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08004089 final long elapsedRealtime = mClocks.elapsedRealtime();
4090 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004091 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
4092 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
4093 + Integer.toHexString(mHistoryCur.states));
4094 addHistoryRecordLocked(elapsedRealtime, uptime);
4095 mScreenOnTimer.startRunningLocked(elapsedRealtime);
4096 if (mScreenBrightnessBin >= 0) {
4097 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
4098 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004099
Jeff Browne95c3cd2014-05-02 16:59:26 -07004100 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08004101 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004102
Jeff Browne95c3cd2014-05-02 16:59:26 -07004103 // Fake a wake lock, so we consider the device waked as long
4104 // as the screen is on.
4105 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
4106 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004107
Jeff Browne95c3cd2014-05-02 16:59:26 -07004108 // Update discharge amounts.
4109 if (mOnBatteryInternal) {
4110 updateDischargeScreenLevelsLocked(false, true);
4111 }
4112 } else if (oldState == Display.STATE_ON) {
4113 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08004114 final long elapsedRealtime = mClocks.elapsedRealtime();
4115 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004116 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
4117 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
4118 + Integer.toHexString(mHistoryCur.states));
4119 addHistoryRecordLocked(elapsedRealtime, uptime);
4120 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
4121 if (mScreenBrightnessBin >= 0) {
4122 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
4123 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004124
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004125 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07004126 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004127
Jeff Browne95c3cd2014-05-02 16:59:26 -07004128 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08004129 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004130
Jeff Browne95c3cd2014-05-02 16:59:26 -07004131 // Update discharge amounts.
4132 if (mOnBatteryInternal) {
4133 updateDischargeScreenLevelsLocked(true, false);
4134 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004135 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004136 }
4137 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004138
Dianne Hackborn617f8772009-03-31 15:04:46 -07004139 public void noteScreenBrightnessLocked(int brightness) {
4140 // Bin the brightness.
4141 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
4142 if (bin < 0) bin = 0;
4143 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
4144 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004145 final long elapsedRealtime = mClocks.elapsedRealtime();
4146 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004147 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
4148 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004149 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
4150 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004151 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004152 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004153 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004154 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004155 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004156 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004157 }
4158 mScreenBrightnessBin = bin;
4159 }
4160 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004161
Dianne Hackborn617f8772009-03-31 15:04:46 -07004162 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004163 if (mOnBatteryInternal) {
4164 uid = mapUid(uid);
4165 getUidStatsLocked(uid).noteUserActivityLocked(event);
4166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004168
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004169 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004170 final long elapsedRealtime = mClocks.elapsedRealtime();
4171 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004172 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
4173 reason, reasonUid);
4174 }
4175
Jeff Browne95c3cd2014-05-02 16:59:26 -07004176 public void noteInteractiveLocked(boolean interactive) {
4177 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08004178 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004179 mInteractive = interactive;
4180 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
4181 if (interactive) {
4182 mInteractiveTimer.startRunningLocked(elapsedRealtime);
4183 } else {
4184 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
4185 }
4186 }
4187 }
4188
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004189 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08004190 final long elapsedRealtime = mClocks.elapsedRealtime();
4191 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004192 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
4193 extra, type);
4194 mNumConnectivityChange++;
4195 }
4196
Adam Lesinski5f056f62016-07-14 16:56:08 -07004197 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
4198 final long uptimeMillis, int uid) {
4199 uid = mapUid(uid);
4200 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4201 uid);
4202 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4203 }
4204
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004205 /**
4206 * Updates the radio power state and returns true if an external stats collection should occur.
4207 */
4208 public boolean noteMobileRadioPowerStateLocked(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004209 final long elapsedRealtime = mClocks.elapsedRealtime();
4210 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004211 if (mMobileRadioPowerState != powerState) {
4212 long realElapsedRealtimeMs;
4213 final boolean active =
4214 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4215 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4216 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004217 if (uid > 0) {
4218 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4219 }
4220
Adam Lesinski9acfd812016-04-19 18:29:50 -07004221 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004222 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4223 } else {
4224 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004225 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004226 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4227 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4228 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004229 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004230 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4231 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4232 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004233 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004234 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4235 }
4236 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4237 + Integer.toHexString(mHistoryCur.states));
4238 addHistoryRecordLocked(elapsedRealtime, uptime);
4239 mMobileRadioPowerState = powerState;
4240 if (active) {
4241 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4242 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4243 } else {
4244 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004245 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004246 // Tell the caller to collect radio network/power stats.
4247 return true;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004248 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004249 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004250 return false;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004251 }
4252
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004253 public void notePowerSaveModeLocked(boolean enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004254 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004255 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4256 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4257 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004258 final long elapsedRealtime = mClocks.elapsedRealtime();
4259 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004260 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004261 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004262 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4263 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004264 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004265 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004266 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004267 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4268 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004269 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004270 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004271 }
4272 addHistoryRecordLocked(elapsedRealtime, uptime);
4273 }
4274 }
4275
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004276 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004277 final long elapsedRealtime = mClocks.elapsedRealtime();
4278 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004279 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004280 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004281 // We don't go out of general idling mode until explicitly taken out of
4282 // device idle through going active or significant motion.
4283 nowIdling = true;
4284 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004285 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4286 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4287 // We don't go out of general light idling mode until explicitly taken out of
4288 // device idle through going active or significant motion.
4289 nowLightIdling = true;
4290 }
4291 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4292 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4293 activeReason, activeUid);
4294 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004295 if (mDeviceIdling != nowIdling) {
4296 mDeviceIdling = nowIdling;
4297 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4298 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4299 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004300 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004301 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4302 } else {
4303 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4304 }
4305 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004306 if (mDeviceLightIdling != nowLightIdling) {
4307 mDeviceLightIdling = nowLightIdling;
4308 if (nowLightIdling) {
4309 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004310 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004311 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004312 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004313 }
4314 if (mDeviceIdleMode != mode) {
4315 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4316 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4317 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4318 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004319 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004320 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4321 mLastIdleTimeStart = elapsedRealtime;
4322 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4323 if (lastDuration > mLongestLightIdleTime) {
4324 mLongestLightIdleTime = lastDuration;
4325 }
4326 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004327 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004328 if (lastDuration > mLongestFullIdleTime) {
4329 mLongestFullIdleTime = lastDuration;
4330 }
4331 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4332 }
4333 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4334 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004335 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004336 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4337 }
4338 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004339 }
4340 }
4341
4342 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004343 final long elapsedRealtime = mClocks.elapsedRealtime();
4344 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004345 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4346 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004347 PackageChange pc = new PackageChange();
4348 pc.mPackageName = pkgName;
4349 pc.mUpdate = true;
4350 pc.mVersionCode = versionCode;
4351 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004352 }
4353
4354 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004355 final long elapsedRealtime = mClocks.elapsedRealtime();
4356 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004357 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4358 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004359 PackageChange pc = new PackageChange();
4360 pc.mPackageName = pkgName;
4361 pc.mUpdate = true;
4362 addPackageChange(pc);
4363 }
4364
4365 private void addPackageChange(PackageChange pc) {
4366 if (mDailyPackageChanges == null) {
4367 mDailyPackageChanges = new ArrayList<>();
4368 }
4369 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004370 }
4371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 public void notePhoneOnLocked() {
4373 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004374 final long elapsedRealtime = mClocks.elapsedRealtime();
4375 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004376 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004377 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4378 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004379 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004381 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 }
4383 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 public void notePhoneOffLocked() {
4386 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004387 final long elapsedRealtime = mClocks.elapsedRealtime();
4388 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004389 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004390 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4391 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004392 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004394 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 }
4396 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004397
Dianne Hackborn3251b902014-06-20 14:40:53 -07004398 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004399 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004400 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004401 if (i == except) {
4402 continue;
4403 }
4404 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004405 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004406 }
4407 }
4408 }
4409
Dianne Hackborne4a59512010-12-07 11:08:07 -08004410 private int fixPhoneServiceState(int state, int signalBin) {
4411 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4412 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4413 // to infer that we are scanning from other data.
4414 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004415 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004416 state = ServiceState.STATE_IN_SERVICE;
4417 }
4418 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004419
Dianne Hackborne4a59512010-12-07 11:08:07 -08004420 return state;
4421 }
4422
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004423 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004424 boolean scanning = false;
4425 boolean newHistory = false;
4426
4427 mPhoneServiceStateRaw = state;
4428 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004429 mPhoneSignalStrengthBinRaw = strengthBin;
4430
Joe Onoratoabded112016-02-08 16:49:39 -08004431 final long elapsedRealtime = mClocks.elapsedRealtime();
4432 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004433
4434 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4435 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4436 // to infer that we are scanning from other data.
4437 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004438 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004439 state = ServiceState.STATE_IN_SERVICE;
4440 }
4441 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004442
4443 // If the phone is powered off, stop all timers.
4444 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004445 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004446
Dianne Hackborne4a59512010-12-07 11:08:07 -08004447 // If we are in service, make sure the correct signal string timer is running.
4448 } else if (state == ServiceState.STATE_IN_SERVICE) {
4449 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004450
4451 // If we're out of service, we are in the lowest signal strength
4452 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004453 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004454 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004455 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004456 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004457 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004458 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004459 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4460 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004461 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004462 }
4463 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004464
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004465 if (!scanning) {
4466 // If we are no longer scanning, then stop the scanning timer.
4467 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4468 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4469 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4470 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004471 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004472 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004473 }
4474 }
4475
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004476 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004477 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4478 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004479 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004480 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004481 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004482 mPhoneServiceState = state;
4483 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004484
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004485 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004486 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004487 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004488 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004489 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004490 if (strengthBin >= 0) {
4491 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004492 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004493 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004494 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4495 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004496 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004497 + Integer.toHexString(mHistoryCur.states));
4498 newHistory = true;
4499 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004500 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004501 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004502 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004503 }
4504
4505 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004506 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004507 }
4508 }
4509
4510 /**
4511 * Telephony stack updates the phone state.
4512 * @param state phone state from ServiceState.getState()
4513 */
4514 public void notePhoneStateLocked(int state, int simState) {
4515 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004516 }
4517
Wink Savillee9b06d72009-05-18 21:47:50 -07004518 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004519 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004520 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004521 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004522 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004523
Dianne Hackborn627bba72009-03-24 22:32:56 -07004524 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4525 int bin = DATA_CONNECTION_NONE;
4526 if (hasData) {
4527 switch (dataType) {
4528 case TelephonyManager.NETWORK_TYPE_EDGE:
4529 bin = DATA_CONNECTION_EDGE;
4530 break;
4531 case TelephonyManager.NETWORK_TYPE_GPRS:
4532 bin = DATA_CONNECTION_GPRS;
4533 break;
4534 case TelephonyManager.NETWORK_TYPE_UMTS:
4535 bin = DATA_CONNECTION_UMTS;
4536 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004537 case TelephonyManager.NETWORK_TYPE_CDMA:
4538 bin = DATA_CONNECTION_CDMA;
4539 break;
4540 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4541 bin = DATA_CONNECTION_EVDO_0;
4542 break;
4543 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4544 bin = DATA_CONNECTION_EVDO_A;
4545 break;
4546 case TelephonyManager.NETWORK_TYPE_1xRTT:
4547 bin = DATA_CONNECTION_1xRTT;
4548 break;
4549 case TelephonyManager.NETWORK_TYPE_HSDPA:
4550 bin = DATA_CONNECTION_HSDPA;
4551 break;
4552 case TelephonyManager.NETWORK_TYPE_HSUPA:
4553 bin = DATA_CONNECTION_HSUPA;
4554 break;
4555 case TelephonyManager.NETWORK_TYPE_HSPA:
4556 bin = DATA_CONNECTION_HSPA;
4557 break;
4558 case TelephonyManager.NETWORK_TYPE_IDEN:
4559 bin = DATA_CONNECTION_IDEN;
4560 break;
4561 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4562 bin = DATA_CONNECTION_EVDO_B;
4563 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004564 case TelephonyManager.NETWORK_TYPE_LTE:
4565 bin = DATA_CONNECTION_LTE;
4566 break;
4567 case TelephonyManager.NETWORK_TYPE_EHRPD:
4568 bin = DATA_CONNECTION_EHRPD;
4569 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004570 case TelephonyManager.NETWORK_TYPE_HSPAP:
4571 bin = DATA_CONNECTION_HSPAP;
4572 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004573 default:
4574 bin = DATA_CONNECTION_OTHER;
4575 break;
4576 }
4577 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004578 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004579 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004580 final long elapsedRealtime = mClocks.elapsedRealtime();
4581 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004582 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4583 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004584 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4585 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004586 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004587 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004588 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004589 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004590 }
4591 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004592 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004593 }
4594 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004595
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004596 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004597 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004598 final long elapsedRealtime = mClocks.elapsedRealtime();
4599 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004600 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004601 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4602 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004603 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004604 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004605 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004606 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004607 }
4608 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004609
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004610 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004611 final long elapsedRealtime = mClocks.elapsedRealtime();
4612 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004613 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004614 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004615 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4616 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004617 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004618 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004619 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004620 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004621 }
4622 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004623
4624 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004625 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004626 final long elapsedRealtime = mClocks.elapsedRealtime();
4627 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004628 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004629 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004630 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4631 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004632 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004633 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004634 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004635 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004636 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004637 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004638
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004639 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004640 if (mAudioOnNesting == 0) {
4641 return;
4642 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004643 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004644 final long elapsedRealtime = mClocks.elapsedRealtime();
4645 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004646 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004647 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004648 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4649 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004650 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004651 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004652 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004653 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004654 }
4655
4656 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004657 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004658 final long elapsedRealtime = mClocks.elapsedRealtime();
4659 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004660 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004661 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004662 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4663 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004664 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004665 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004666 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004667 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004668 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004669 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004670
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004671 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004672 if (mVideoOnNesting == 0) {
4673 return;
4674 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004675 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004676 final long elapsedRealtime = mClocks.elapsedRealtime();
4677 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004678 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004679 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004680 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4681 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004682 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004683 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004684 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004685 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004686 }
4687
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004688 public void noteResetAudioLocked() {
4689 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004690 final long elapsedRealtime = mClocks.elapsedRealtime();
4691 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004692 mAudioOnNesting = 0;
4693 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4694 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4695 + Integer.toHexString(mHistoryCur.states));
4696 addHistoryRecordLocked(elapsedRealtime, uptime);
4697 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4698 for (int i=0; i<mUidStats.size(); i++) {
4699 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4700 uid.noteResetAudioLocked(elapsedRealtime);
4701 }
4702 }
4703 }
4704
4705 public void noteResetVideoLocked() {
4706 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004707 final long elapsedRealtime = mClocks.elapsedRealtime();
4708 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004709 mAudioOnNesting = 0;
4710 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4711 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4712 + Integer.toHexString(mHistoryCur.states));
4713 addHistoryRecordLocked(elapsedRealtime, uptime);
4714 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4715 for (int i=0; i<mUidStats.size(); i++) {
4716 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4717 uid.noteResetVideoLocked(elapsedRealtime);
4718 }
4719 }
4720 }
4721
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004722 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004723 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004724 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004725 }
4726
4727 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004728 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004729 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004730 }
4731
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004732 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004733 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004734 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4735 }
4736
4737 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004738 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004739 getUidStatsLocked(uid).noteVibratorOffLocked();
4740 }
4741
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004742 public void noteFlashlightOnLocked(int uid) {
4743 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004744 final long elapsedRealtime = mClocks.elapsedRealtime();
4745 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004746 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004747 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4748 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004749 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004750 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004751 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4752 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004753 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4754 }
4755
4756 public void noteFlashlightOffLocked(int uid) {
4757 if (mFlashlightOnNesting == 0) {
4758 return;
4759 }
4760 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004761 final long elapsedRealtime = mClocks.elapsedRealtime();
4762 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004763 if (--mFlashlightOnNesting == 0) {
4764 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4765 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4766 + Integer.toHexString(mHistoryCur.states2));
4767 addHistoryRecordLocked(elapsedRealtime, uptime);
4768 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4769 }
4770 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4771 }
4772
4773 public void noteCameraOnLocked(int uid) {
4774 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004775 final long elapsedRealtime = mClocks.elapsedRealtime();
4776 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004777 if (mCameraOnNesting++ == 0) {
4778 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4779 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4780 + Integer.toHexString(mHistoryCur.states2));
4781 addHistoryRecordLocked(elapsedRealtime, uptime);
4782 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4783 }
4784 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4785 }
4786
4787 public void noteCameraOffLocked(int uid) {
4788 if (mCameraOnNesting == 0) {
4789 return;
4790 }
4791 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004792 final long elapsedRealtime = mClocks.elapsedRealtime();
4793 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004794 if (--mCameraOnNesting == 0) {
4795 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4796 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4797 + Integer.toHexString(mHistoryCur.states2));
4798 addHistoryRecordLocked(elapsedRealtime, uptime);
4799 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4800 }
4801 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4802 }
4803
4804 public void noteResetCameraLocked() {
4805 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004806 final long elapsedRealtime = mClocks.elapsedRealtime();
4807 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004808 mCameraOnNesting = 0;
4809 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4810 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4811 + Integer.toHexString(mHistoryCur.states2));
4812 addHistoryRecordLocked(elapsedRealtime, uptime);
4813 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4814 for (int i=0; i<mUidStats.size(); i++) {
4815 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4816 uid.noteResetCameraLocked(elapsedRealtime);
4817 }
4818 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004819 }
4820
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004821 public void noteResetFlashlightLocked() {
4822 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004823 final long elapsedRealtime = mClocks.elapsedRealtime();
4824 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004825 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004826 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4827 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004828 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004829 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004830 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4831 for (int i=0; i<mUidStats.size(); i++) {
4832 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4833 uid.noteResetFlashlightLocked(elapsedRealtime);
4834 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004835 }
4836 }
4837
Bookatzb1f04f32017-05-19 13:57:32 -07004838 private void noteBluetoothScanStartedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004839 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004840 final long elapsedRealtime = mClocks.elapsedRealtime();
4841 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004842 if (mBluetoothScanNesting == 0) {
4843 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4844 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4845 + Integer.toHexString(mHistoryCur.states2));
4846 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004847 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004848 }
4849 mBluetoothScanNesting++;
Bookatzb1f04f32017-05-19 13:57:32 -07004850 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004851 }
4852
Bookatzb1f04f32017-05-19 13:57:32 -07004853 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004854 final int N = ws.size();
4855 for (int i = 0; i < N; i++) {
Bookatzb1f04f32017-05-19 13:57:32 -07004856 noteBluetoothScanStartedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004857 }
4858 }
4859
Bookatz94c5a312017-07-11 16:49:17 -07004860 private void noteBluetoothScanStoppedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004861 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004862 final long elapsedRealtime = mClocks.elapsedRealtime();
4863 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004864 mBluetoothScanNesting--;
4865 if (mBluetoothScanNesting == 0) {
4866 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4867 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4868 + Integer.toHexString(mHistoryCur.states2));
4869 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004870 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004871 }
Bookatz94c5a312017-07-11 16:49:17 -07004872 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004873 }
4874
Bookatz94c5a312017-07-11 16:49:17 -07004875 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004876 final int N = ws.size();
4877 for (int i = 0; i < N; i++) {
Bookatz94c5a312017-07-11 16:49:17 -07004878 noteBluetoothScanStoppedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004879 }
4880 }
4881
4882 public void noteResetBluetoothScanLocked() {
4883 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004884 final long elapsedRealtime = mClocks.elapsedRealtime();
4885 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004886 mBluetoothScanNesting = 0;
4887 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4888 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4889 + Integer.toHexString(mHistoryCur.states2));
4890 addHistoryRecordLocked(elapsedRealtime, uptime);
4891 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4892 for (int i=0; i<mUidStats.size(); i++) {
4893 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4894 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4895 }
4896 }
4897 }
4898
Bookatz4ebc0642017-05-11 12:21:19 -07004899 public void noteBluetoothScanResultsFromSourceLocked(WorkSource ws, int numNewResults) {
Bookatz956f36bf2017-04-28 09:48:17 -07004900 final int N = ws.size();
4901 for (int i = 0; i < N; i++) {
4902 int uid = mapUid(ws.get(i));
Bookatz4ebc0642017-05-11 12:21:19 -07004903 getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07004904 }
4905 }
4906
Adam Lesinski5f056f62016-07-14 16:56:08 -07004907 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4908 final long uptimeMillis, int uid) {
4909 uid = mapUid(uid);
4910 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4911 uid);
4912 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4913 }
4914
4915 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004916 final long elapsedRealtime = mClocks.elapsedRealtime();
4917 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004918 if (mWifiRadioPowerState != powerState) {
4919 final boolean active =
4920 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4921 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4922 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004923 if (uid > 0) {
4924 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4925 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004926 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4927 } else {
4928 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4929 }
4930 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4931 + Integer.toHexString(mHistoryCur.states));
4932 addHistoryRecordLocked(elapsedRealtime, uptime);
4933 mWifiRadioPowerState = powerState;
4934 }
4935 }
4936
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004937 public void noteWifiRunningLocked(WorkSource ws) {
4938 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004939 final long elapsedRealtime = mClocks.elapsedRealtime();
4940 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004941 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004942 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4943 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004944 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004945 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004946 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004947 int N = ws.size();
4948 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004949 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004950 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004951 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004952 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004953 } else {
4954 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004955 }
4956 }
4957
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004958 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4959 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004960 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004961 int N = oldWs.size();
4962 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004963 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004964 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004965 }
4966 N = newWs.size();
4967 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004968 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004969 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004970 }
4971 } else {
4972 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4973 }
4974 }
4975
4976 public void noteWifiStoppedLocked(WorkSource ws) {
4977 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004978 final long elapsedRealtime = mClocks.elapsedRealtime();
4979 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004980 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004981 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4982 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004983 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004984 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004985 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004986 int N = ws.size();
4987 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004988 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004989 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004990 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004991 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004992 } else {
4993 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004994 }
4995 }
4996
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004997 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4998 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4999 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005000 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005001 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005002 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005003 }
5004 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005005 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005006 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005007 }
5008 }
5009
Dianne Hackborn3251b902014-06-20 14:40:53 -07005010 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
5011 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
5012 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005013 final long elapsedRealtime = mClocks.elapsedRealtime();
5014 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005015 if (mWifiSupplState >= 0) {
5016 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
5017 }
5018 mWifiSupplState = supplState;
5019 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
5020 mHistoryCur.states2 =
5021 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
5022 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
5023 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
5024 + Integer.toHexString(mHistoryCur.states2));
5025 addHistoryRecordLocked(elapsedRealtime, uptime);
5026 }
5027 }
5028
5029 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08005030 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005031 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
5032 if (i == except) {
5033 continue;
5034 }
5035 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
5036 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
5037 }
5038 }
5039 }
5040
5041 public void noteWifiRssiChangedLocked(int newRssi) {
5042 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
5043 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
5044 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08005045 final long elapsedRealtime = mClocks.elapsedRealtime();
5046 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005047 if (mWifiSignalStrengthBin >= 0) {
5048 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
5049 elapsedRealtime);
5050 }
5051 if (strengthBin >= 0) {
5052 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
5053 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
5054 }
5055 mHistoryCur.states2 =
5056 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
5057 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
5058 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
5059 + Integer.toHexString(mHistoryCur.states2));
5060 addHistoryRecordLocked(elapsedRealtime, uptime);
5061 } else {
5062 stopAllWifiSignalStrengthTimersLocked(-1);
5063 }
5064 mWifiSignalStrengthBin = strengthBin;
5065 }
5066 }
5067
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005068 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005069
The Android Open Source Project10592532009-03-18 17:39:46 -07005070 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005071 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005072 final long elapsedRealtime = mClocks.elapsedRealtime();
5073 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005074 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005075 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005076 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
5077 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005078 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005079 }
5080 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005081 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005082 }
5083
5084 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005085 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005086 final long elapsedRealtime = mClocks.elapsedRealtime();
5087 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005088 mWifiFullLockNesting--;
5089 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005090 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005091 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
5092 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005093 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005094 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005095 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005096 }
5097
Nick Pelly6ccaa542012-06-15 15:22:47 -07005098 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005099
Nick Pelly6ccaa542012-06-15 15:22:47 -07005100 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005101 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005102 final long elapsedRealtime = mClocks.elapsedRealtime();
5103 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005104 if (mWifiScanNesting == 0) {
5105 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
5106 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005107 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005108 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005109 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005110 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005111 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005112 }
5113
Nick Pelly6ccaa542012-06-15 15:22:47 -07005114 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005115 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005116 final long elapsedRealtime = mClocks.elapsedRealtime();
5117 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005118 mWifiScanNesting--;
5119 if (mWifiScanNesting == 0) {
5120 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
5121 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005122 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005123 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005124 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005125 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005126 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005127
Robert Greenwalta029ea12013-09-25 16:38:12 -07005128 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005129 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005130 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005131 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005132 }
5133
5134 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005135 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005136 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005137 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005138 }
5139
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005140 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005141
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005142 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005143 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005144 final long elapsedRealtime = mClocks.elapsedRealtime();
5145 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005146 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005147 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005148 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
5149 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005150 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005151 }
5152 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005153 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005154 }
5155
5156 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005157 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005158 final long elapsedRealtime = mClocks.elapsedRealtime();
5159 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005160 mWifiMulticastNesting--;
5161 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005162 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005163 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
5164 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005165 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005166 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005167 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005168 }
5169
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005170 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
5171 int N = ws.size();
5172 for (int i=0; i<N; i++) {
5173 noteFullWifiLockAcquiredLocked(ws.get(i));
5174 }
5175 }
5176
5177 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
5178 int N = ws.size();
5179 for (int i=0; i<N; i++) {
5180 noteFullWifiLockReleasedLocked(ws.get(i));
5181 }
5182 }
5183
Nick Pelly6ccaa542012-06-15 15:22:47 -07005184 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005185 int N = ws.size();
5186 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005187 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005188 }
5189 }
5190
Nick Pelly6ccaa542012-06-15 15:22:47 -07005191 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005192 int N = ws.size();
5193 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005194 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005195 }
5196 }
5197
Robert Greenwalta029ea12013-09-25 16:38:12 -07005198 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
5199 int N = ws.size();
5200 for (int i=0; i<N; i++) {
5201 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
5202 }
5203 }
5204
5205 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
5206 int N = ws.size();
5207 for (int i=0; i<N; i++) {
5208 noteWifiBatchedScanStoppedLocked(ws.get(i));
5209 }
5210 }
5211
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005212 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
5213 int N = ws.size();
5214 for (int i=0; i<N; i++) {
5215 noteWifiMulticastEnabledLocked(ws.get(i));
5216 }
5217 }
5218
5219 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5220 int N = ws.size();
5221 for (int i=0; i<N; i++) {
5222 noteWifiMulticastDisabledLocked(ws.get(i));
5223 }
5224 }
5225
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005226 private static String[] includeInStringArray(String[] array, String str) {
5227 if (ArrayUtils.indexOf(array, str) >= 0) {
5228 return array;
5229 }
5230 String[] newArray = new String[array.length+1];
5231 System.arraycopy(array, 0, newArray, 0, array.length);
5232 newArray[array.length] = str;
5233 return newArray;
5234 }
5235
5236 private static String[] excludeFromStringArray(String[] array, String str) {
5237 int index = ArrayUtils.indexOf(array, str);
5238 if (index >= 0) {
5239 String[] newArray = new String[array.length-1];
5240 if (index > 0) {
5241 System.arraycopy(array, 0, newArray, 0, index);
5242 }
5243 if (index < array.length-1) {
5244 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5245 }
5246 return newArray;
5247 }
5248 return array;
5249 }
5250
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005251 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005252 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005253
Adam Lesinski14ae39a2017-05-26 11:50:40 -07005254 synchronized (mModemNetworkLock) {
5255 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
5256 mModemIfaces = includeInStringArray(mModemIfaces, iface);
5257 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
5258 } else {
5259 mModemIfaces = excludeFromStringArray(mModemIfaces, iface);
5260 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mModemIfaces);
5261 }
5262 }
5263
5264 synchronized (mWifiNetworkLock) {
5265 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
5266 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5267 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
5268 } else {
5269 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5270 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
5271 }
5272 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005273 }
5274
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005275 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5276 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005278
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005279 @Override public int getScreenOnCount(int which) {
5280 return mScreenOnTimer.getCountLocked(which);
5281 }
5282
Dianne Hackborn617f8772009-03-31 15:04:46 -07005283 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005284 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005285 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005286 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005287 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005288
Jeff Browne95c3cd2014-05-02 16:59:26 -07005289 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5290 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005291 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005292
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005293 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5294 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005295 }
5296
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005297 @Override public int getPowerSaveModeEnabledCount(int which) {
5298 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5299 }
5300
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005301 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5302 int which) {
5303 switch (mode) {
5304 case DEVICE_IDLE_MODE_LIGHT:
5305 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005306 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005307 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5308 }
5309 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005310 }
5311
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005312 @Override public int getDeviceIdleModeCount(int mode, int which) {
5313 switch (mode) {
5314 case DEVICE_IDLE_MODE_LIGHT:
5315 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005316 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005317 return mDeviceIdleModeFullTimer.getCountLocked(which);
5318 }
5319 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005320 }
5321
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005322 @Override public long getLongestDeviceIdleModeTime(int mode) {
5323 switch (mode) {
5324 case DEVICE_IDLE_MODE_LIGHT:
5325 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005326 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005327 return mLongestFullIdleTime;
5328 }
5329 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005330 }
5331
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005332 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5333 switch (mode) {
5334 case DEVICE_IDLE_MODE_LIGHT:
5335 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005336 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005337 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5338 }
5339 return 0;
5340 }
5341
5342 @Override public int getDeviceIdlingCount(int mode, int which) {
5343 switch (mode) {
5344 case DEVICE_IDLE_MODE_LIGHT:
5345 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005346 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005347 return mDeviceIdlingTimer.getCountLocked(which);
5348 }
5349 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005350 }
5351
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005352 @Override public int getNumConnectivityChange(int which) {
5353 int val = mNumConnectivityChange;
5354 if (which == STATS_CURRENT) {
5355 val -= mLoadedNumConnectivityChange;
5356 } else if (which == STATS_SINCE_UNPLUGGED) {
5357 val -= mUnpluggedNumConnectivityChange;
5358 }
5359 return val;
5360 }
5361
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005362 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5363 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005365
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005366 @Override public int getPhoneOnCount(int which) {
5367 return mPhoneOnTimer.getCountLocked(which);
5368 }
5369
Dianne Hackborn627bba72009-03-24 22:32:56 -07005370 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005371 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005372 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005373 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005374 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005375
5376 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005377 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005378 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005379 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005380 }
5381
Catherine Liufb900812012-07-17 14:12:56 -05005382 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5383 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005384 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005385
Dianne Hackborn627bba72009-03-24 22:32:56 -07005386 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005387 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005388 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005389 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005390 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005391
Dianne Hackborn617f8772009-03-31 15:04:46 -07005392 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005393 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005394 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005395
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005396 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5397 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005398 }
5399
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005400 @Override public int getMobileRadioActiveCount(int which) {
5401 return mMobileRadioActiveTimer.getCountLocked(which);
5402 }
5403
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005404 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5405 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5406 }
5407
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005408 @Override public long getMobileRadioActiveUnknownTime(int which) {
5409 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5410 }
5411
5412 @Override public int getMobileRadioActiveUnknownCount(int which) {
5413 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5414 }
5415
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005416 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5417 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005418 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005419
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005420 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5421 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005422 }
5423
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005424 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005425 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005426 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005427 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005428 }
5429
5430 @Override public int getWifiStateCount(int wifiState, int which) {
5431 return mWifiStateTimer[wifiState].getCountLocked(which);
5432 }
5433
Dianne Hackborn3251b902014-06-20 14:40:53 -07005434 @Override public long getWifiSupplStateTime(int state,
5435 long elapsedRealtimeUs, int which) {
5436 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5437 elapsedRealtimeUs, which);
5438 }
5439
5440 @Override public int getWifiSupplStateCount(int state, int which) {
5441 return mWifiSupplStateTimer[state].getCountLocked(which);
5442 }
5443
5444 @Override public long getWifiSignalStrengthTime(int strengthBin,
5445 long elapsedRealtimeUs, int which) {
5446 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5447 elapsedRealtimeUs, which);
5448 }
5449
5450 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5451 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5452 }
5453
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005454 @Override
5455 public ControllerActivityCounter getBluetoothControllerActivity() {
5456 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005457 }
5458
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005459 @Override
5460 public ControllerActivityCounter getWifiControllerActivity() {
5461 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005462 }
5463
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005464 @Override
5465 public ControllerActivityCounter getModemControllerActivity() {
5466 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005467 }
5468
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005469 @Override
5470 public boolean hasBluetoothActivityReporting() {
5471 return mHasBluetoothReporting;
5472 }
5473
5474 @Override
5475 public boolean hasWifiActivityReporting() {
5476 return mHasWifiReporting;
5477 }
5478
5479 @Override
5480 public boolean hasModemActivityReporting() {
5481 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005482 }
5483
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005484 @Override
5485 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005486 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5487 }
5488
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005489 @Override
5490 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005491 return mFlashlightOnTimer.getCountLocked(which);
5492 }
5493
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005494 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005495 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5496 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5497 }
5498
5499 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005500 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5501 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5502 }
5503
5504 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005505 public long getNetworkActivityBytes(int type, int which) {
5506 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5507 return mNetworkByteActivityCounters[type].getCountLocked(which);
5508 } else {
5509 return 0;
5510 }
5511 }
5512
5513 @Override
5514 public long getNetworkActivityPackets(int type, int which) {
5515 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5516 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005517 } else {
5518 return 0;
5519 }
5520 }
5521
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005522 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005523 final long currentTime = System.currentTimeMillis();
5524 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005525 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5526 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005527 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005528 return mStartClockTime;
5529 }
5530
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005531 @Override public String getStartPlatformVersion() {
5532 return mStartPlatformVersion;
5533 }
5534
5535 @Override public String getEndPlatformVersion() {
5536 return mEndPlatformVersion;
5537 }
5538
5539 @Override public int getParcelVersion() {
5540 return VERSION;
5541 }
5542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 @Override public boolean getIsOnBattery() {
5544 return mOnBattery;
5545 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5548 return mUidStats;
5549 }
5550
Adam Lesinski5f056f62016-07-14 16:56:08 -07005551 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5552 if (timer != null) {
5553 timer.detach();
5554 }
5555 }
5556
5557 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5558 boolean detachIfReset) {
5559 if (timer != null) {
5560 return timer.reset(detachIfReset);
5561 }
5562 return true;
5563 }
5564
Bookatz867c0d72017-03-07 18:23:42 -08005565 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5566 if (timer != null) {
5567 return timer.reset(detachIfReset);
5568 }
5569 return true;
5570 }
5571
Adam Lesinski5f056f62016-07-14 16:56:08 -07005572 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5573 if (counter != null) {
5574 counter.detach();
5575 }
5576 }
5577
5578 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5579 boolean detachIfReset) {
5580 if (counter != null) {
5581 counter.reset(detachIfReset);
5582 }
5583 }
5584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 /**
5586 * The statistics associated with a particular uid.
5587 */
Joe Onoratoabded112016-02-08 16:49:39 -08005588 public static class Uid extends BatteryStats.Uid {
5589 /**
5590 * BatteryStatsImpl that we are associated with.
5591 */
5592 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005595
Bookatz867c0d72017-03-07 18:23:42 -08005596 /** TimeBase for when uid is in background and device is on battery. */
5597 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5598 public final TimeBase mOnBatteryBackgroundTimeBase;
Bookatzc8c44962017-05-11 12:12:54 -07005599 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5600 public final TimeBase mOnBatteryScreenOffBackgroundTimeBase;
Bookatz867c0d72017-03-07 18:23:42 -08005601
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005602 boolean mWifiRunning;
5603 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005604
The Android Open Source Project10592532009-03-18 17:39:46 -07005605 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005606 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005607
Nick Pelly6ccaa542012-06-15 15:22:47 -07005608 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005609 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005610
Dianne Hackborn61659e52014-07-09 16:13:01 -07005611 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005612 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5613 StopwatchTimer[] mWifiBatchedScanTimer;
5614
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005615 boolean mWifiMulticastEnabled;
5616 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005617
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005618 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005619 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005620 StopwatchTimer mFlashlightTurnedOnTimer;
5621 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005622 StopwatchTimer mForegroundActivityTimer;
Bookatzc8c44962017-05-11 12:12:54 -07005623 /** Total time spent by the uid holding any partial wakelocks. */
5624 DualTimer mAggregatedPartialWakelockTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005625 DualTimer mBluetoothScanTimer;
Bookatzb1f04f32017-05-19 13:57:32 -07005626 DualTimer mBluetoothUnoptimizedScanTimer;
Bookatz956f36bf2017-04-28 09:48:17 -07005627 Counter mBluetoothScanResultCounter;
Bookatzb1f04f32017-05-19 13:57:32 -07005628 Counter mBluetoothScanResultBgCounter;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005629
Dianne Hackborna8d10942015-11-19 17:55:19 -08005630 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005631 StopwatchTimer[] mProcessStateTimer;
5632
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005633 BatchTimer mVibratorOnTimer;
5634
Dianne Hackborn617f8772009-03-31 15:04:46 -07005635 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005636
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005637 LongSamplingCounter[] mNetworkByteActivityCounters;
5638 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005639 LongSamplingCounter mMobileRadioActiveTime;
5640 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005643 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5644 */
5645 private LongSamplingCounter mMobileRadioApWakeupCount;
5646
5647 /**
5648 * How many times this UID woke up the Application Processor due to a Wifi packet.
5649 */
5650 private LongSamplingCounter mWifiRadioApWakeupCount;
5651
5652 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005653 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005654 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005655 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005656 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005657
5658 /**
5659 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005660 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005661 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005662 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5663
5664 /**
5665 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5666 * Can be null if the UID has had no such activity.
5667 */
5668 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005669
5670 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005671 * The CPU times we had at the last history details update.
5672 */
5673 long mLastStepUserTime;
5674 long mLastStepSystemTime;
5675 long mCurStepUserTime;
5676 long mCurStepSystemTime;
5677
Joe Onoratoabded112016-02-08 16:49:39 -08005678 LongSamplingCounter mUserCpuTime;
5679 LongSamplingCounter mSystemCpuTime;
Adam Lesinski6832f392015-09-05 18:05:40 -07005680 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005681
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005682 LongSamplingCounterArray mCpuFreqTimeMs;
5683 LongSamplingCounterArray mScreenOffCpuFreqTimeMs;
5684
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 * The statistics we have collected for this uid's wake locks.
5687 */
Joe Onoratoabded112016-02-08 16:49:39 -08005688 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689
5690 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005691 * The statistics we have collected for this uid's syncs.
5692 */
Bookatz2bffb5b2017-04-13 11:59:33 -07005693 final OverflowArrayMap<DualTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005694
5695 /**
5696 * The statistics we have collected for this uid's jobs.
5697 */
Bookatzaa4594a2017-03-24 12:39:56 -07005698 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005699
5700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 * The statistics we have collected for this uid's sensor activations.
5702 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005703 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704
5705 /**
5706 * The statistics we have collected for this uid's processes.
5707 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005708 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709
5710 /**
5711 * The statistics we have collected for this uid's processes.
5712 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005713 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005714
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005715 /**
5716 * The transient wake stats we have collected for this uid's pids.
5717 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005718 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005719
Joe Onoratoabded112016-02-08 16:49:39 -08005720 public Uid(BatteryStatsImpl bsi, int uid) {
5721 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005723
Bookatz867c0d72017-03-07 18:23:42 -08005724 mOnBatteryBackgroundTimeBase = new TimeBase();
5725 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5726 mBsi.mClocks.elapsedRealtime() * 1000);
5727
Bookatzc8c44962017-05-11 12:12:54 -07005728 mOnBatteryScreenOffBackgroundTimeBase = new TimeBase();
5729 mOnBatteryScreenOffBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5730 mBsi.mClocks.elapsedRealtime() * 1000);
5731
Joe Onoratoabded112016-02-08 16:49:39 -08005732 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5733 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005734
Dianne Hackborn657153b2016-07-29 14:54:14 -07005735 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005736 @Override public Wakelock instantiateObject() {
5737 return new Wakelock(mBsi, Uid.this);
5738 }
5739 };
Bookatz2bffb5b2017-04-13 11:59:33 -07005740 mSyncStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5741 @Override public DualTimer instantiateObject() {
5742 return new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
5743 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005744 }
5745 };
Bookatzaa4594a2017-03-24 12:39:56 -07005746 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5747 @Override public DualTimer instantiateObject() {
5748 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
5749 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005750 }
5751 };
5752
5753 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5754 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5755 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5756 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005757 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5758 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005759 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005760 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5761 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005762 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 }
5764
5765 @Override
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005766 public long[] getCpuFreqTimes(int which) {
5767 if (mCpuFreqTimeMs == null) {
5768 return null;
5769 }
5770 final long[] cpuFreqTimes = mCpuFreqTimeMs.getCountsLocked(which);
5771 if (cpuFreqTimes == null) {
5772 return null;
5773 }
5774 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5775 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5776 if (cpuFreqTimes[i] != 0) {
5777 return cpuFreqTimes;
5778 }
5779 }
5780 return null;
5781 }
5782
5783 @Override
5784 public long[] getScreenOffCpuFreqTimes(int which) {
5785 if (mScreenOffCpuFreqTimeMs == null) {
5786 return null;
5787 }
5788 final long[] cpuFreqTimes = mScreenOffCpuFreqTimeMs.getCountsLocked(which);
5789 if (cpuFreqTimes == null) {
5790 return null;
5791 }
5792 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5793 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5794 if (cpuFreqTimes[i] != 0) {
5795 return cpuFreqTimes;
5796 }
5797 }
5798 return null;
5799 }
5800
5801 @Override
Bookatzc8c44962017-05-11 12:12:54 -07005802 public Timer getAggregatedPartialWakelockTimer() {
5803 return mAggregatedPartialWakelockTimer;
5804 }
5805
5806 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005807 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005808 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 }
5810
5811 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005812 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005813 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005814 }
5815
5816 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005817 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005818 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005819 }
5820
5821 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005822 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 return mSensorStats;
5824 }
5825
5826 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005827 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 return mProcessStats;
5829 }
5830
5831 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005832 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833 return mPackageStats;
5834 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005835
5836 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 public int getUid() {
5838 return mUid;
5839 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005840
5841 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005842 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005843 if (!mWifiRunning) {
5844 mWifiRunning = true;
5845 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005846 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5847 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005848 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005849 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005850 }
5851 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005852
Dianne Hackborn617f8772009-03-31 15:04:46 -07005853 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005854 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005855 if (mWifiRunning) {
5856 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005857 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005858 }
5859 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005860
Dianne Hackborn617f8772009-03-31 15:04:46 -07005861 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005862 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005863 if (!mFullWifiLockOut) {
5864 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005865 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005866 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5867 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005868 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005869 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005870 }
5871 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005872
The Android Open Source Project10592532009-03-18 17:39:46 -07005873 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005874 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005875 if (mFullWifiLockOut) {
5876 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005877 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005878 }
5879 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005880
The Android Open Source Project10592532009-03-18 17:39:46 -07005881 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005882 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005883 if (!mWifiScanStarted) {
5884 mWifiScanStarted = true;
5885 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005886 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5887 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
5888 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005889 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005890 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005891 }
5892 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005893
The Android Open Source Project10592532009-03-18 17:39:46 -07005894 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005895 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005896 if (mWifiScanStarted) {
5897 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005898 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005899 }
5900 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005901
5902 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005903 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005904 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005905 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005906 csph = csph >> 3;
5907 bin++;
5908 }
5909
5910 if (mWifiBatchedScanBinStarted == bin) return;
5911
5912 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5913 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005914 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005915 }
5916 mWifiBatchedScanBinStarted = bin;
5917 if (mWifiBatchedScanTimer[bin] == null) {
5918 makeWifiBatchedScanBin(bin, null);
5919 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005920 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005921 }
5922
5923 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005924 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005925 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5926 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005927 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005928 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5929 }
5930 }
5931
5932 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005933 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005934 if (!mWifiMulticastEnabled) {
5935 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005936 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005937 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5938 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005939 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005940 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005941 }
5942 }
5943
5944 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005945 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005946 if (mWifiMulticastEnabled) {
5947 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005948 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005949 }
5950 }
5951
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005952 @Override
5953 public ControllerActivityCounter getWifiControllerActivity() {
5954 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005955 }
5956
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005957 @Override
5958 public ControllerActivityCounter getBluetoothControllerActivity() {
5959 return mBluetoothControllerActivity;
5960 }
5961
5962 @Override
5963 public ControllerActivityCounter getModemControllerActivity() {
5964 return mModemControllerActivity;
5965 }
5966
5967 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5968 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005969 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005970 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005971 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005972 return mWifiControllerActivity;
5973 }
5974
5975 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5976 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005977 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005978 NUM_BT_TX_LEVELS);
5979 }
5980 return mBluetoothControllerActivity;
5981 }
5982
5983 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5984 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005985 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005986 ModemActivityInfo.TX_POWER_LEVELS);
5987 }
5988 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005989 }
5990
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005991 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5992 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005993 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5994 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005995 }
5996 return mAudioTurnedOnTimer;
5997 }
5998
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005999 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006000 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6001 }
6002
6003 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
6004 if (mAudioTurnedOnTimer != null) {
6005 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006006 }
6007 }
6008
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006009 public void noteResetAudioLocked(long elapsedRealtimeMs) {
6010 if (mAudioTurnedOnTimer != null) {
6011 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006012 }
6013 }
6014
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006015 public StopwatchTimer createVideoTurnedOnTimerLocked() {
6016 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006017 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6018 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006019 }
6020 return mVideoTurnedOnTimer;
6021 }
6022
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006023 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006024 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6025 }
6026
6027 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
6028 if (mVideoTurnedOnTimer != null) {
6029 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006030 }
6031 }
6032
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006033 public void noteResetVideoLocked(long elapsedRealtimeMs) {
6034 if (mVideoTurnedOnTimer != null) {
6035 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006036 }
6037 }
6038
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006039 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
6040 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006041 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6042 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006043 }
6044 return mFlashlightTurnedOnTimer;
6045 }
6046
6047 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
6048 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6049 }
6050
6051 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
6052 if (mFlashlightTurnedOnTimer != null) {
6053 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6054 }
6055 }
6056
6057 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
6058 if (mFlashlightTurnedOnTimer != null) {
6059 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6060 }
6061 }
6062
6063 public StopwatchTimer createCameraTurnedOnTimerLocked() {
6064 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006065 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6066 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006067 }
6068 return mCameraTurnedOnTimer;
6069 }
6070
6071 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
6072 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6073 }
6074
6075 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
6076 if (mCameraTurnedOnTimer != null) {
6077 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6078 }
6079 }
6080
6081 public void noteResetCameraLocked(long elapsedRealtimeMs) {
6082 if (mCameraTurnedOnTimer != null) {
6083 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6084 }
6085 }
6086
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006087 public StopwatchTimer createForegroundActivityTimerLocked() {
6088 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006089 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6090 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006091 }
6092 return mForegroundActivityTimer;
6093 }
6094
Bookatzc8c44962017-05-11 12:12:54 -07006095 public DualTimer createAggregatedPartialWakelockTimerLocked() {
6096 if (mAggregatedPartialWakelockTimer == null) {
6097 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
6098 AGGREGATED_WAKE_TYPE_PARTIAL, null,
6099 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase);
6100 }
6101 return mAggregatedPartialWakelockTimer;
6102 }
6103
Bookatz867c0d72017-03-07 18:23:42 -08006104 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006105 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006106 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6107 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6108 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006109 }
6110 return mBluetoothScanTimer;
6111 }
6112
Bookatzb1f04f32017-05-19 13:57:32 -07006113 public DualTimer createBluetoothUnoptimizedScanTimerLocked() {
6114 if (mBluetoothUnoptimizedScanTimer == null) {
6115 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
6116 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
6117 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
6118 }
6119 return mBluetoothUnoptimizedScanTimer;
6120 }
6121
6122 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006123 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07006124 if (isUnoptimized) {
6125 createBluetoothUnoptimizedScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
6126 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006127 }
6128
Bookatz94c5a312017-07-11 16:49:17 -07006129 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006130 if (mBluetoothScanTimer != null) {
6131 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
6132 }
Bookatz94c5a312017-07-11 16:49:17 -07006133 if (isUnoptimized && mBluetoothUnoptimizedScanTimer != null) {
Bookatzb1f04f32017-05-19 13:57:32 -07006134 mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
6135 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006136 }
6137
6138 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
6139 if (mBluetoothScanTimer != null) {
6140 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6141 }
Bookatzb1f04f32017-05-19 13:57:32 -07006142 if (mBluetoothUnoptimizedScanTimer != null) {
6143 mBluetoothUnoptimizedScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6144 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006145 }
6146
Bookatz956f36bf2017-04-28 09:48:17 -07006147 public Counter createBluetoothScanResultCounterLocked() {
6148 if (mBluetoothScanResultCounter == null) {
6149 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase);
6150 }
6151 return mBluetoothScanResultCounter;
6152 }
6153
Bookatzb1f04f32017-05-19 13:57:32 -07006154 public Counter createBluetoothScanResultBgCounterLocked() {
6155 if (mBluetoothScanResultBgCounter == null) {
6156 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase);
6157 }
6158 return mBluetoothScanResultBgCounter;
6159 }
6160
Bookatz4ebc0642017-05-11 12:21:19 -07006161 public void noteBluetoothScanResultsLocked(int numNewResults) {
6162 createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
Bookatzb1f04f32017-05-19 13:57:32 -07006163 // Uses background timebase, so the count will only be incremented if uid in background.
6164 createBluetoothScanResultBgCounterLocked().addAtomic(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07006165 }
6166
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006167 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006168 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006169 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006170 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006171 }
6172
6173 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006174 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006175 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006176 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006177 }
6178 }
6179
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006180 public BatchTimer createVibratorOnTimerLocked() {
6181 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006182 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6183 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006184 }
6185 return mVibratorOnTimer;
6186 }
6187
6188 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08006189 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006190 }
6191
6192 public void noteVibratorOffLocked() {
6193 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006194 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006195 }
6196 }
6197
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006198 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006199 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006200 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006201 return 0;
6202 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006203 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006204 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006205
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006206 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006207 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006208 if (mFullWifiLockTimer == null) {
6209 return 0;
6210 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006211 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006212 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006213
6214 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006215 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006216 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006217 return 0;
6218 }
Bookatzaa4594a2017-03-24 12:39:56 -07006219 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006220 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006221
6222 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006223 public int getWifiScanCount(int which) {
6224 if (mWifiScanTimer == null) {
6225 return 0;
6226 }
Bookatzaa4594a2017-03-24 12:39:56 -07006227 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08006228 }
6229
6230 @Override
6231 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07006232 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006233 return 0;
6234 }
6235 return mWifiScanTimer.getSubTimer().getCountLocked(which);
6236 }
6237
6238 @Override
6239 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
6240 if (mWifiScanTimer == null) {
6241 return 0;
6242 }
6243 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07006244 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08006245 }
6246
6247 @Override
6248 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07006249 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006250 return 0;
6251 }
6252 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
6253 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07006254 }
6255
6256 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006257 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006258 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6259 if (mWifiBatchedScanTimer[csphBin] == null) {
6260 return 0;
6261 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006262 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006263 }
6264
6265 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006266 public int getWifiBatchedScanCount(int csphBin, int which) {
6267 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6268 if (mWifiBatchedScanTimer[csphBin] == null) {
6269 return 0;
6270 }
6271 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
6272 }
6273
6274 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006275 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006276 if (mWifiMulticastTimer == null) {
6277 return 0;
6278 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006279 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006280 }
6281
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006282 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006283 public Timer getAudioTurnedOnTimer() {
6284 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006285 }
6286
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006287 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006288 public Timer getVideoTurnedOnTimer() {
6289 return mVideoTurnedOnTimer;
6290 }
6291
6292 @Override
6293 public Timer getFlashlightTurnedOnTimer() {
6294 return mFlashlightTurnedOnTimer;
6295 }
6296
6297 @Override
6298 public Timer getCameraTurnedOnTimer() {
6299 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006300 }
6301
Dianne Hackborn617f8772009-03-31 15:04:46 -07006302 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006303 public Timer getForegroundActivityTimer() {
6304 return mForegroundActivityTimer;
6305 }
6306
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006307 @Override
6308 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07006309 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08006310 }
6311
6312 @Override
6313 public Timer getBluetoothScanBackgroundTimer() {
6314 if (mBluetoothScanTimer == null) {
6315 return null;
6316 }
6317 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006318 }
6319
Bookatz956f36bf2017-04-28 09:48:17 -07006320 @Override
Bookatzb1f04f32017-05-19 13:57:32 -07006321 public Timer getBluetoothUnoptimizedScanTimer() {
6322 return mBluetoothUnoptimizedScanTimer;
6323 }
6324
6325 @Override
6326 public Timer getBluetoothUnoptimizedScanBackgroundTimer() {
6327 if (mBluetoothUnoptimizedScanTimer == null) {
6328 return null;
6329 }
6330 return mBluetoothUnoptimizedScanTimer.getSubTimer();
6331 }
6332
6333 @Override
Bookatz956f36bf2017-04-28 09:48:17 -07006334 public Counter getBluetoothScanResultCounter() {
6335 return mBluetoothScanResultCounter;
6336 }
6337
Bookatzb1f04f32017-05-19 13:57:32 -07006338 @Override
6339 public Counter getBluetoothScanResultBgCounter() {
6340 return mBluetoothScanResultBgCounter;
6341 }
6342
Dianne Hackborn61659e52014-07-09 16:13:01 -07006343 void makeProcessState(int i, Parcel in) {
6344 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6345
6346 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006347 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6348 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006349 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006350 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6351 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006352 }
6353 }
6354
6355 @Override
6356 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6357 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6358 if (mProcessStateTimer[state] == null) {
6359 return 0;
6360 }
6361 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6362 }
6363
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006364 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006365 public Timer getProcessStateTimer(int state) {
6366 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6367 return mProcessStateTimer[state];
6368 }
6369
6370 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006371 public Timer getVibratorOnTimer() {
6372 return mVibratorOnTimer;
6373 }
6374
6375 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006376 public void noteUserActivityLocked(int type) {
6377 if (mUserActivityCounters == null) {
6378 initUserActivityLocked();
6379 }
Jeff Browndf693de2012-07-27 12:03:38 -07006380 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6381 mUserActivityCounters[type].stepAtomic();
6382 } else {
6383 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6384 new Throwable());
6385 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006386 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006387
Dianne Hackborn617f8772009-03-31 15:04:46 -07006388 @Override
6389 public boolean hasUserActivity() {
6390 return mUserActivityCounters != null;
6391 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006392
Dianne Hackborn617f8772009-03-31 15:04:46 -07006393 @Override
6394 public int getUserActivityCount(int type, int which) {
6395 if (mUserActivityCounters == null) {
6396 return 0;
6397 }
Evan Millarc64edde2009-04-18 12:26:32 -07006398 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006399 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006400
Robert Greenwalta029ea12013-09-25 16:38:12 -07006401 void makeWifiBatchedScanBin(int i, Parcel in) {
6402 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6403
Joe Onoratoabded112016-02-08 16:49:39 -08006404 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006405 if (collected == null) {
6406 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006407 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006408 }
6409 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006410 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6411 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006412 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006413 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6414 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006415 }
6416 }
6417
6418
Dianne Hackborn617f8772009-03-31 15:04:46 -07006419 void initUserActivityLocked() {
6420 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6421 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006422 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006423 }
6424 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006425
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006426 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6427 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006428 initNetworkActivityLocked();
6429 }
6430 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006431 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6432 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006433 } else {
6434 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6435 new Throwable());
6436 }
6437 }
6438
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006439 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6440 if (mNetworkByteActivityCounters == null) {
6441 initNetworkActivityLocked();
6442 }
6443 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6444 mMobileRadioActiveCount.addCountLocked(1);
6445 }
6446
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006447 @Override
6448 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006449 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006450 }
6451
6452 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006453 public long getNetworkActivityBytes(int type, int which) {
6454 if (mNetworkByteActivityCounters != null && type >= 0
6455 && type < mNetworkByteActivityCounters.length) {
6456 return mNetworkByteActivityCounters[type].getCountLocked(which);
6457 } else {
6458 return 0;
6459 }
6460 }
6461
6462 @Override
6463 public long getNetworkActivityPackets(int type, int which) {
6464 if (mNetworkPacketActivityCounters != null && type >= 0
6465 && type < mNetworkPacketActivityCounters.length) {
6466 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006467 } else {
6468 return 0;
6469 }
6470 }
6471
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006472 @Override
6473 public long getMobileRadioActiveTime(int which) {
6474 return mMobileRadioActiveTime != null
6475 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6476 }
6477
6478 @Override
6479 public int getMobileRadioActiveCount(int which) {
6480 return mMobileRadioActiveCount != null
6481 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6482 }
6483
Adam Lesinskie08af192015-03-25 16:42:59 -07006484 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006485 public long getUserCpuTimeUs(int which) {
6486 return mUserCpuTime.getCountLocked(which);
6487 }
6488
6489 @Override
6490 public long getSystemCpuTimeUs(int which) {
6491 return mSystemCpuTime.getCountLocked(which);
6492 }
6493
6494 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006495 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
6496 if (mCpuClusterSpeed != null) {
6497 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
6498 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
6499 if (cpuSpeeds != null) {
6500 if (step >= 0 && step < cpuSpeeds.length) {
6501 final LongSamplingCounter c = cpuSpeeds[step];
6502 if (c != null) {
6503 return c.getCountLocked(which);
6504 }
6505 }
6506 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006507 }
6508 }
6509 return 0;
6510 }
6511
Adam Lesinski5f056f62016-07-14 16:56:08 -07006512 public void noteMobileRadioApWakeupLocked() {
6513 if (mMobileRadioApWakeupCount == null) {
6514 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6515 }
6516 mMobileRadioApWakeupCount.addCountLocked(1);
6517 }
6518
6519 @Override
6520 public long getMobileRadioApWakeupCount(int which) {
6521 if (mMobileRadioApWakeupCount != null) {
6522 return mMobileRadioApWakeupCount.getCountLocked(which);
6523 }
6524 return 0;
6525 }
6526
6527 public void noteWifiRadioApWakeupLocked() {
6528 if (mWifiRadioApWakeupCount == null) {
6529 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6530 }
6531 mWifiRadioApWakeupCount.addCountLocked(1);
6532 }
6533
6534 @Override
6535 public long getWifiRadioApWakeupCount(int which) {
6536 if (mWifiRadioApWakeupCount != null) {
6537 return mWifiRadioApWakeupCount.getCountLocked(which);
6538 }
6539 return 0;
6540 }
6541
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006542 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006543 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6544 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006545 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006546 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6547 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006548 }
Joe Onoratoabded112016-02-08 16:49:39 -08006549 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6550 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006552
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006553 /**
6554 * Clear all stats for this uid. Returns true if the uid is completely
6555 * inactive so can be dropped.
6556 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006557 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
Bookatz993a0be2017-07-21 09:03:23 -07006558 public boolean reset(long uptime, long realtime) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006559 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006560
Bookatz993a0be2017-07-21 09:03:23 -07006561 mOnBatteryBackgroundTimeBase.init(uptime, realtime);
6562 mOnBatteryScreenOffBackgroundTimeBase.init(uptime, realtime);
6563
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006564 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006565 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006566 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006567 }
6568 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006569 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006570 active |= mFullWifiLockOut;
6571 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006572 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006573 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006574 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006575 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006576 if (mWifiBatchedScanTimer != null) {
6577 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6578 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006579 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006580 }
6581 }
6582 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6583 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006584 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006585 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006586 active |= mWifiMulticastEnabled;
6587 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006588
6589 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6590 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6591 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6592 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6593 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
Bookatzc8c44962017-05-11 12:12:54 -07006594 active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
Adam Lesinski5f056f62016-07-14 16:56:08 -07006595 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
Bookatzb1f04f32017-05-19 13:57:32 -07006596 active |= !resetTimerIfNotNull(mBluetoothUnoptimizedScanTimer, false);
Bookatz956f36bf2017-04-28 09:48:17 -07006597 if (mBluetoothScanResultCounter != null) {
6598 mBluetoothScanResultCounter.reset(false);
6599 }
Bookatzb1f04f32017-05-19 13:57:32 -07006600 if (mBluetoothScanResultBgCounter != null) {
6601 mBluetoothScanResultBgCounter.reset(false);
6602 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006603
Dianne Hackborn61659e52014-07-09 16:13:01 -07006604 if (mProcessStateTimer != null) {
6605 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6606 if (mProcessStateTimer[i] != null) {
6607 active |= !mProcessStateTimer[i].reset(false);
6608 }
6609 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006610 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006611 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006612 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006613 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006614 mVibratorOnTimer.detach();
6615 mVibratorOnTimer = null;
6616 } else {
6617 active = true;
6618 }
6619 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006620
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006621 if (mUserActivityCounters != null) {
6622 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6623 mUserActivityCounters[i].reset(false);
6624 }
6625 }
6626
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006627 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006628 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006629 mNetworkByteActivityCounters[i].reset(false);
6630 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006631 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006632 mMobileRadioActiveTime.reset(false);
6633 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006634 }
6635
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006636 if (mWifiControllerActivity != null) {
6637 mWifiControllerActivity.reset(false);
6638 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006639
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006640 if (mBluetoothControllerActivity != null) {
6641 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006642 }
6643
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006644 if (mModemControllerActivity != null) {
6645 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006646 }
6647
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006648 mUserCpuTime.reset(false);
6649 mSystemCpuTime.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006650
6651 if (mCpuClusterSpeed != null) {
6652 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
6653 if (speeds != null) {
6654 for (LongSamplingCounter speed : speeds) {
6655 if (speed != null) {
6656 speed.reset(false);
6657 }
6658 }
6659 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006660 }
6661 }
6662
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006663 if (mCpuFreqTimeMs != null) {
6664 mCpuFreqTimeMs.reset(false);
6665 }
6666 if (mScreenOffCpuFreqTimeMs != null) {
6667 mScreenOffCpuFreqTimeMs.reset(false);
6668 }
6669
Adam Lesinski5f056f62016-07-14 16:56:08 -07006670 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6671 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6672
Dianne Hackbornd953c532014-08-16 18:17:38 -07006673 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6674 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6675 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006676 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006677 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006678 } else {
6679 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006680 }
6681 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006682 mWakelockStats.cleanup();
Bookatz2bffb5b2017-04-13 11:59:33 -07006683 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006684 for (int is=syncStats.size()-1; is>=0; is--) {
Bookatz2bffb5b2017-04-13 11:59:33 -07006685 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006686 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006687 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006688 timer.detach();
6689 } else {
6690 active = true;
6691 }
6692 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006693 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07006694 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006695 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07006696 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006697 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006698 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006699 timer.detach();
6700 } else {
6701 active = true;
6702 }
6703 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006704 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006705 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6706 Sensor s = mSensorStats.valueAt(ise);
6707 if (s.reset()) {
6708 mSensorStats.removeAt(ise);
6709 } else {
6710 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006711 }
6712 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006713 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6714 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006715 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006716 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006717 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006718 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006719 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006720 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006721 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006722 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006723 } else {
6724 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006725 }
6726 }
6727 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006728 if (mPackageStats.size() > 0) {
6729 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6730 while (it.hasNext()) {
6731 Map.Entry<String, Pkg> pkgEntry = it.next();
6732 Pkg p = pkgEntry.getValue();
6733 p.detach();
6734 if (p.mServiceStats.size() > 0) {
6735 Iterator<Map.Entry<String, Pkg.Serv>> it2
6736 = p.mServiceStats.entrySet().iterator();
6737 while (it2.hasNext()) {
6738 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6739 servEntry.getValue().detach();
6740 }
6741 }
6742 }
6743 mPackageStats.clear();
6744 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006745
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006746 mLastStepUserTime = mLastStepSystemTime = 0;
6747 mCurStepUserTime = mCurStepSystemTime = 0;
6748
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006749 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006750 if (mWifiRunningTimer != null) {
6751 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006752 }
6753 if (mFullWifiLockTimer != null) {
6754 mFullWifiLockTimer.detach();
6755 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006756 if (mWifiScanTimer != null) {
6757 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006758 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006759 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6760 if (mWifiBatchedScanTimer[i] != null) {
6761 mWifiBatchedScanTimer[i].detach();
6762 }
6763 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006764 if (mWifiMulticastTimer != null) {
6765 mWifiMulticastTimer.detach();
6766 }
6767 if (mAudioTurnedOnTimer != null) {
6768 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006769 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006770 }
6771 if (mVideoTurnedOnTimer != null) {
6772 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006773 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006774 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006775 if (mFlashlightTurnedOnTimer != null) {
6776 mFlashlightTurnedOnTimer.detach();
6777 mFlashlightTurnedOnTimer = null;
6778 }
6779 if (mCameraTurnedOnTimer != null) {
6780 mCameraTurnedOnTimer.detach();
6781 mCameraTurnedOnTimer = null;
6782 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006783 if (mForegroundActivityTimer != null) {
6784 mForegroundActivityTimer.detach();
6785 mForegroundActivityTimer = null;
6786 }
Bookatzc8c44962017-05-11 12:12:54 -07006787 if (mAggregatedPartialWakelockTimer != null) {
6788 mAggregatedPartialWakelockTimer.detach();
6789 mAggregatedPartialWakelockTimer = null;
6790 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006791 if (mBluetoothScanTimer != null) {
6792 mBluetoothScanTimer.detach();
6793 mBluetoothScanTimer = null;
6794 }
Bookatzb1f04f32017-05-19 13:57:32 -07006795 if (mBluetoothUnoptimizedScanTimer != null) {
6796 mBluetoothUnoptimizedScanTimer.detach();
6797 mBluetoothUnoptimizedScanTimer = null;
6798 }
Bookatz956f36bf2017-04-28 09:48:17 -07006799 if (mBluetoothScanResultCounter != null) {
6800 mBluetoothScanResultCounter.detach();
6801 mBluetoothScanResultCounter = null;
6802 }
Bookatzb1f04f32017-05-19 13:57:32 -07006803 if (mBluetoothScanResultBgCounter != null) {
6804 mBluetoothScanResultBgCounter.detach();
6805 mBluetoothScanResultBgCounter = null;
6806 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006807 if (mUserActivityCounters != null) {
6808 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6809 mUserActivityCounters[i].detach();
6810 }
6811 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006812 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006813 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006814 mNetworkByteActivityCounters[i].detach();
6815 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006816 }
6817 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006818
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006819 if (mWifiControllerActivity != null) {
6820 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006821 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006822
6823 if (mBluetoothControllerActivity != null) {
6824 mBluetoothControllerActivity.detach();
6825 }
6826
6827 if (mModemControllerActivity != null) {
6828 mModemControllerActivity.detach();
6829 }
6830
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006831 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006832
6833 mUserCpuTime.detach();
6834 mSystemCpuTime.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006835
6836 if (mCpuClusterSpeed != null) {
6837 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6838 if (cpuSpeeds != null) {
6839 for (LongSamplingCounter c : cpuSpeeds) {
6840 if (c != null) {
6841 c.detach();
6842 }
6843 }
6844 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006845 }
6846 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006847
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006848 if (mCpuFreqTimeMs != null) {
6849 mCpuFreqTimeMs.detach();
6850 }
6851 if (mScreenOffCpuFreqTimeMs != null) {
6852 mScreenOffCpuFreqTimeMs.detach();
6853 }
6854
Adam Lesinski5f056f62016-07-14 16:56:08 -07006855 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6856 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006857 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006858
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006859 return !active;
6860 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006861
Bookatz867c0d72017-03-07 18:23:42 -08006862 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
6863 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatzc8c44962017-05-11 12:12:54 -07006864 mOnBatteryScreenOffBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08006865
Dianne Hackbornd953c532014-08-16 18:17:38 -07006866 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6867 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006868 out.writeInt(NW);
6869 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006870 out.writeString(wakeStats.keyAt(iw));
6871 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006872 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 }
6874
Bookatz2bffb5b2017-04-13 11:59:33 -07006875 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006876 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006877 out.writeInt(NS);
6878 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006879 out.writeString(syncStats.keyAt(is));
Bookatz2bffb5b2017-04-13 11:59:33 -07006880 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006881 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6882 }
6883
Bookatzaa4594a2017-03-24 12:39:56 -07006884 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006885 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006886 out.writeInt(NJ);
6887 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006888 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07006889 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006890 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6891 }
6892
Dianne Hackborn61659e52014-07-09 16:13:01 -07006893 int NSE = mSensorStats.size();
6894 out.writeInt(NSE);
6895 for (int ise=0; ise<NSE; ise++) {
6896 out.writeInt(mSensorStats.keyAt(ise));
6897 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006898 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 }
6900
Dianne Hackborn61659e52014-07-09 16:13:01 -07006901 int NP = mProcessStats.size();
6902 out.writeInt(NP);
6903 for (int ip=0; ip<NP; ip++) {
6904 out.writeString(mProcessStats.keyAt(ip));
6905 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 proc.writeToParcelLocked(out);
6907 }
6908
6909 out.writeInt(mPackageStats.size());
6910 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6911 out.writeString(pkgEntry.getKey());
6912 Uid.Pkg pkg = pkgEntry.getValue();
6913 pkg.writeToParcelLocked(out);
6914 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006915
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006916 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006917 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006918 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006919 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006920 out.writeInt(0);
6921 }
6922 if (mFullWifiLockTimer != null) {
6923 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006924 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006925 } else {
6926 out.writeInt(0);
6927 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006928 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006929 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006930 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006931 } else {
6932 out.writeInt(0);
6933 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006934 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6935 if (mWifiBatchedScanTimer[i] != null) {
6936 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006937 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006938 } else {
6939 out.writeInt(0);
6940 }
6941 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006942 if (mWifiMulticastTimer != null) {
6943 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006944 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006945 } else {
6946 out.writeInt(0);
6947 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006948
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006949 if (mAudioTurnedOnTimer != null) {
6950 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006951 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006952 } else {
6953 out.writeInt(0);
6954 }
6955 if (mVideoTurnedOnTimer != null) {
6956 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006957 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006958 } else {
6959 out.writeInt(0);
6960 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006961 if (mFlashlightTurnedOnTimer != null) {
6962 out.writeInt(1);
6963 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6964 } else {
6965 out.writeInt(0);
6966 }
6967 if (mCameraTurnedOnTimer != null) {
6968 out.writeInt(1);
6969 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6970 } else {
6971 out.writeInt(0);
6972 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006973 if (mForegroundActivityTimer != null) {
6974 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006975 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006976 } else {
6977 out.writeInt(0);
6978 }
Bookatzc8c44962017-05-11 12:12:54 -07006979 if (mAggregatedPartialWakelockTimer != null) {
6980 out.writeInt(1);
6981 mAggregatedPartialWakelockTimer.writeToParcel(out, elapsedRealtimeUs);
6982 } else {
6983 out.writeInt(0);
6984 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006985 if (mBluetoothScanTimer != null) {
6986 out.writeInt(1);
6987 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6988 } else {
6989 out.writeInt(0);
6990 }
Bookatzb1f04f32017-05-19 13:57:32 -07006991 if (mBluetoothUnoptimizedScanTimer != null) {
6992 out.writeInt(1);
6993 mBluetoothUnoptimizedScanTimer.writeToParcel(out, elapsedRealtimeUs);
6994 } else {
6995 out.writeInt(0);
6996 }
Bookatz956f36bf2017-04-28 09:48:17 -07006997 if (mBluetoothScanResultCounter != null) {
6998 out.writeInt(1);
6999 mBluetoothScanResultCounter.writeToParcel(out);
7000 } else {
7001 out.writeInt(0);
7002 }
Bookatzb1f04f32017-05-19 13:57:32 -07007003 if (mBluetoothScanResultBgCounter != null) {
7004 out.writeInt(1);
7005 mBluetoothScanResultBgCounter.writeToParcel(out);
7006 } else {
7007 out.writeInt(0);
7008 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007009 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7010 if (mProcessStateTimer[i] != null) {
7011 out.writeInt(1);
7012 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
7013 } else {
7014 out.writeInt(0);
7015 }
7016 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007017 if (mVibratorOnTimer != null) {
7018 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007019 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007020 } else {
7021 out.writeInt(0);
7022 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007023 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007024 out.writeInt(1);
7025 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
7026 mUserActivityCounters[i].writeToParcel(out);
7027 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007028 } else {
7029 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007030 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007031 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007032 out.writeInt(1);
7033 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007034 mNetworkByteActivityCounters[i].writeToParcel(out);
7035 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007036 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007037 mMobileRadioActiveTime.writeToParcel(out);
7038 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007039 } else {
7040 out.writeInt(0);
7041 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007042
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007043 if (mWifiControllerActivity != null) {
7044 out.writeInt(1);
7045 mWifiControllerActivity.writeToParcel(out, 0);
7046 } else {
7047 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007048 }
7049
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007050 if (mBluetoothControllerActivity != null) {
7051 out.writeInt(1);
7052 mBluetoothControllerActivity.writeToParcel(out, 0);
7053 } else {
7054 out.writeInt(0);
7055 }
7056
7057 if (mModemControllerActivity != null) {
7058 out.writeInt(1);
7059 mModemControllerActivity.writeToParcel(out, 0);
7060 } else {
7061 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007062 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007063
7064 mUserCpuTime.writeToParcel(out);
7065 mSystemCpuTime.writeToParcel(out);
7066
Adam Lesinski6832f392015-09-05 18:05:40 -07007067 if (mCpuClusterSpeed != null) {
7068 out.writeInt(1);
7069 out.writeInt(mCpuClusterSpeed.length);
7070 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
7071 if (cpuSpeeds != null) {
7072 out.writeInt(1);
7073 out.writeInt(cpuSpeeds.length);
7074 for (LongSamplingCounter c : cpuSpeeds) {
7075 if (c != null) {
7076 out.writeInt(1);
7077 c.writeToParcel(out);
7078 } else {
7079 out.writeInt(0);
7080 }
7081 }
7082 } else {
7083 out.writeInt(0);
7084 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007085 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007086 } else {
7087 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007088 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007089
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007090 LongSamplingCounterArray.writeToParcel(out, mCpuFreqTimeMs);
7091 LongSamplingCounterArray.writeToParcel(out, mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007092
Adam Lesinski5f056f62016-07-14 16:56:08 -07007093 if (mMobileRadioApWakeupCount != null) {
7094 out.writeInt(1);
7095 mMobileRadioApWakeupCount.writeToParcel(out);
7096 } else {
7097 out.writeInt(0);
7098 }
7099
7100 if (mWifiRadioApWakeupCount != null) {
7101 out.writeInt(1);
7102 mWifiRadioApWakeupCount.writeToParcel(out);
7103 } else {
7104 out.writeInt(0);
7105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 }
7107
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007108 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08007109 mOnBatteryBackgroundTimeBase.readFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -07007110 mOnBatteryScreenOffBackgroundTimeBase.readFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -08007111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 int numWakelocks = in.readInt();
7113 mWakelockStats.clear();
7114 for (int j = 0; j < numWakelocks; j++) {
7115 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007116 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Bookatz5b5ec322017-05-26 09:40:38 -07007117 wakelock.readFromParcelLocked(
7118 timeBase, screenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007119 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 }
7121
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007122 int numSyncs = in.readInt();
7123 mSyncStats.clear();
7124 for (int j = 0; j < numSyncs; j++) {
7125 String syncName = in.readString();
7126 if (in.readInt() != 0) {
Bookatz2bffb5b2017-04-13 11:59:33 -07007127 mSyncStats.add(syncName, new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
7128 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007129 }
7130 }
7131
7132 int numJobs = in.readInt();
7133 mJobStats.clear();
7134 for (int j = 0; j < numJobs; j++) {
7135 String jobName = in.readString();
7136 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07007137 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
7138 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007139 }
7140 }
7141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 int numSensors = in.readInt();
7143 mSensorStats.clear();
7144 for (int k = 0; k < numSensors; k++) {
7145 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007146 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08007147 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7148 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 mSensorStats.put(sensorNumber, sensor);
7150 }
7151
7152 int numProcs = in.readInt();
7153 mProcessStats.clear();
7154 for (int k = 0; k < numProcs; k++) {
7155 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007156 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007157 proc.readFromParcelLocked(in);
7158 mProcessStats.put(processName, proc);
7159 }
7160
7161 int numPkgs = in.readInt();
7162 mPackageStats.clear();
7163 for (int l = 0; l < numPkgs; l++) {
7164 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007165 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 pkg.readFromParcelLocked(in);
7167 mPackageStats.put(packageName, pkg);
7168 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007169
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007170 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007171 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007172 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
7173 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007174 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007175 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007176 }
7177 mFullWifiLockOut = false;
7178 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007179 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
7180 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007181 } else {
7182 mFullWifiLockTimer = null;
7183 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007184 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007185 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007186 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
7187 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7188 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007189 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007190 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007191 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007192 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
7193 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7194 if (in.readInt() != 0) {
7195 makeWifiBatchedScanBin(i, in);
7196 } else {
7197 mWifiBatchedScanTimer[i] = null;
7198 }
7199 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007200 mWifiMulticastEnabled = false;
7201 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007202 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
7203 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007204 } else {
7205 mWifiMulticastTimer = null;
7206 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007207 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007208 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
7209 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007210 } else {
7211 mAudioTurnedOnTimer = null;
7212 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007213 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007214 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
7215 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007216 } else {
7217 mVideoTurnedOnTimer = null;
7218 }
7219 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007220 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7221 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007222 } else {
7223 mFlashlightTurnedOnTimer = null;
7224 }
7225 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007226 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
7227 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007228 } else {
7229 mCameraTurnedOnTimer = null;
7230 }
7231 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007232 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7233 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007234 } else {
7235 mForegroundActivityTimer = null;
7236 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007237 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -07007238 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
7239 AGGREGATED_WAKE_TYPE_PARTIAL, null,
7240 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase,
7241 in);
7242 } else {
7243 mAggregatedPartialWakelockTimer = null;
7244 }
7245 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007246 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
7247 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
7248 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007249 } else {
7250 mBluetoothScanTimer = null;
7251 }
Bookatz956f36bf2017-04-28 09:48:17 -07007252 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -07007253 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
7254 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
7255 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in);
7256 } else {
7257 mBluetoothUnoptimizedScanTimer = null;
7258 }
7259 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -07007260 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
7261 } else {
7262 mBluetoothScanResultCounter = null;
7263 }
Bookatzb1f04f32017-05-19 13:57:32 -07007264 if (in.readInt() != 0) {
7265 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase, in);
7266 } else {
7267 mBluetoothScanResultBgCounter = null;
7268 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007269 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007270 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7271 if (in.readInt() != 0) {
7272 makeProcessState(i, in);
7273 } else {
7274 mProcessStateTimer[i] = null;
7275 }
7276 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007277 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007278 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
7279 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007280 } else {
7281 mVibratorOnTimer = null;
7282 }
7283 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007284 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
7285 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007286 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007287 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007288 } else {
7289 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07007290 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007291 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007292 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
7293 mNetworkPacketActivityCounters
7294 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007295 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007296 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007297 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007298 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007299 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007300 }
Joe Onoratoabded112016-02-08 16:49:39 -08007301 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7302 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007303 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007304 mNetworkByteActivityCounters = null;
7305 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007306 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007307
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007308 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007309 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007310 NUM_WIFI_TX_LEVELS, in);
7311 } else {
7312 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007313 }
7314
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007315 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007316 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007317 NUM_BT_TX_LEVELS, in);
7318 } else {
7319 mBluetoothControllerActivity = null;
7320 }
7321
7322 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007323 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007324 ModemActivityInfo.TX_POWER_LEVELS, in);
7325 } else {
7326 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007327 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007328
Joe Onoratoabded112016-02-08 16:49:39 -08007329 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7330 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007331
Adam Lesinski6832f392015-09-05 18:05:40 -07007332 if (in.readInt() != 0) {
7333 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007334 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007335 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007336 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007337
7338 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
7339 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
7340 if (in.readInt() != 0) {
7341 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007342 if (mBsi.mPowerProfile != null &&
7343 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007344 throw new ParcelFormatException("Incompatible number of cpu speeds");
7345 }
7346
7347 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
7348 mCpuClusterSpeed[cluster] = cpuSpeeds;
7349 for (int speed = 0; speed < numSpeeds; speed++) {
7350 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007351 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07007352 }
7353 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07007354 } else {
7355 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07007356 }
7357 }
7358 } else {
7359 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007360 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007361
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007362 mCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(in, mBsi.mOnBatteryTimeBase);
7363 mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(
7364 in, mBsi.mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007365
7366 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07007367 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7368 } else {
7369 mMobileRadioApWakeupCount = null;
7370 }
7371
7372 if (in.readInt() != 0) {
7373 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7374 } else {
7375 mWifiRadioApWakeupCount = null;
7376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 }
7378
7379 /**
7380 * The statistics associated with a particular wake lock.
7381 */
Joe Onoratoabded112016-02-08 16:49:39 -08007382 public static class Wakelock extends BatteryStats.Uid.Wakelock {
7383 /**
7384 * BatteryStatsImpl that we are associated with.
7385 */
7386 protected BatteryStatsImpl mBsi;
7387
7388 /**
7389 * BatteryStatsImpl that we are associated with.
7390 */
7391 protected Uid mUid;
7392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 /**
7394 * How long (in ms) this uid has been keeping the device partially awake.
Bookatz5b5ec322017-05-26 09:40:38 -07007395 * Tracks both the total time and the time while the app was in the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 */
Bookatz5b5ec322017-05-26 09:40:38 -07007397 DualTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398
7399 /**
7400 * How long (in ms) this uid has been keeping the device fully awake.
7401 */
Evan Millarc64edde2009-04-18 12:26:32 -07007402 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403
7404 /**
7405 * How long (in ms) this uid has had a window keeping the device awake.
7406 */
Evan Millarc64edde2009-04-18 12:26:32 -07007407 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408
7409 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007410 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07007411 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007412 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007413
Joe Onoratoabded112016-02-08 16:49:39 -08007414 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
7415 mBsi = bsi;
7416 mUid = uid;
7417 }
7418
Adam Lesinski9425fe22015-06-19 12:02:13 -07007419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7421 * proper timer pool from the given BatteryStatsImpl object.
7422 *
7423 * @param in the Parcel to be read from.
7424 * return a new Timer, or null.
7425 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007426 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7427 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 if (in.readInt() == 0) {
7429 return null;
7430 }
7431
Joe Onoratoabded112016-02-08 16:49:39 -08007432 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 }
7434
Joe Onorato92fd23f2016-07-25 11:18:42 -07007435 /**
7436 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7437 * proper timer pool from the given BatteryStatsImpl object.
7438 *
7439 * @param in the Parcel to be read from.
7440 * return a new Timer, or null.
7441 */
Bookatz5b5ec322017-05-26 09:40:38 -07007442 private DualTimer readDualTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
7443 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007444 if (in.readInt() == 0) {
7445 return null;
7446 }
7447
Bookatz5b5ec322017-05-26 09:40:38 -07007448 return new DualTimer(mBsi.mClocks, mUid, type, pool, timeBase, bgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007449 }
7450
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007451 boolean reset() {
7452 boolean wlactive = false;
7453 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007454 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007455 }
7456 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007457 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007458 }
7459 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007460 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007461 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007462 if (mTimerDraw != null) {
7463 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007464 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007465 if (!wlactive) {
7466 if (mTimerFull != null) {
7467 mTimerFull.detach();
7468 mTimerFull = null;
7469 }
7470 if (mTimerPartial != null) {
7471 mTimerPartial.detach();
7472 mTimerPartial = null;
7473 }
7474 if (mTimerWindow != null) {
7475 mTimerWindow.detach();
7476 mTimerWindow = null;
7477 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007478 if (mTimerDraw != null) {
7479 mTimerDraw.detach();
7480 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007481 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007482 }
7483 return !wlactive;
7484 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007485
Bookatz5b5ec322017-05-26 09:40:38 -07007486 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase,
7487 TimeBase screenOffBgTimeBase, Parcel in) {
7488 mTimerPartial = readDualTimerFromParcel(WAKE_TYPE_PARTIAL,
7489 mBsi.mPartialTimers, screenOffTimeBase, screenOffBgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007490 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7491 mBsi.mFullTimers, timeBase, in);
7492 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7493 mBsi.mWindowTimers, timeBase, in);
7494 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7495 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 }
7497
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007498 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7499 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7500 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7501 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007502 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 }
7504
7505 @Override
7506 public Timer getWakeTime(int type) {
7507 switch (type) {
7508 case WAKE_TYPE_FULL: return mTimerFull;
7509 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7510 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007511 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 default: throw new IllegalArgumentException("type = " + type);
7513 }
7514 }
7515 }
7516
Joe Onoratoabded112016-02-08 16:49:39 -08007517 public static class Sensor extends BatteryStats.Uid.Sensor {
7518 /**
7519 * BatteryStatsImpl that we are associated with.
7520 */
7521 protected BatteryStatsImpl mBsi;
7522
7523 /**
Bookatz867c0d72017-03-07 18:23:42 -08007524 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007525 */
7526 protected Uid mUid;
7527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007528 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007529 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007530
Joe Onoratoabded112016-02-08 16:49:39 -08007531 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7532 mBsi = bsi;
7533 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 mHandle = handle;
7535 }
7536
Bookatz867c0d72017-03-07 18:23:42 -08007537 private DualTimer readTimersFromParcel(
7538 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 if (in.readInt() == 0) {
7540 return null;
7541 }
7542
Joe Onoratoabded112016-02-08 16:49:39 -08007543 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007544 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007545 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007546 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 }
Bookatz867c0d72017-03-07 18:23:42 -08007548 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007549 }
7550
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007551 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007552 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007553 mTimer = null;
7554 return true;
7555 }
7556 return false;
7557 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007558
Bookatz867c0d72017-03-07 18:23:42 -08007559 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7560 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 }
7562
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007563 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007564 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 }
7566
7567 @Override
7568 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07007569 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007571
7572 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007573 public Timer getSensorBackgroundTime() {
7574 if (mTimer == null) {
7575 return null;
7576 }
7577 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007578 }
7579
7580 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 public int getHandle() {
7582 return mHandle;
7583 }
7584 }
7585
7586 /**
7587 * The statistics associated with a particular process.
7588 */
Joe Onoratoabded112016-02-08 16:49:39 -08007589 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7590 /**
7591 * BatteryStatsImpl that we are associated with.
7592 */
7593 protected BatteryStatsImpl mBsi;
7594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007596 * The name of this process.
7597 */
7598 final String mName;
7599
7600 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007601 * Remains true until removed from the stats.
7602 */
7603 boolean mActive = true;
7604
7605 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007606 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 */
7608 long mUserTime;
7609
7610 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007611 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 */
7613 long mSystemTime;
7614
7615 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007616 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007617 */
7618 long mForegroundTime;
7619
7620 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007621 * Number of times the process has been started.
7622 */
7623 int mStarts;
7624
7625 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007626 * Number of times the process has crashed.
7627 */
7628 int mNumCrashes;
7629
7630 /**
7631 * Number of times the process has had an ANR.
7632 */
7633 int mNumAnrs;
7634
7635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 * The amount of user time loaded from a previous save.
7637 */
7638 long mLoadedUserTime;
7639
7640 /**
7641 * The amount of system time loaded from a previous save.
7642 */
7643 long mLoadedSystemTime;
7644
7645 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007646 * The amount of foreground time loaded from a previous save.
7647 */
7648 long mLoadedForegroundTime;
7649
7650 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007651 * The number of times the process has started from a previous save.
7652 */
7653 int mLoadedStarts;
7654
7655 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007656 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007658 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659
7660 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007661 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007663 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007664
7665 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 * The amount of user time when last unplugged.
7667 */
7668 long mUnpluggedUserTime;
7669
7670 /**
7671 * The amount of system time when last unplugged.
7672 */
7673 long mUnpluggedSystemTime;
7674
7675 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007676 * The amount of foreground time since unplugged.
7677 */
7678 long mUnpluggedForegroundTime;
7679
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007680 /**
7681 * The number of times the process has started before unplugged.
7682 */
7683 int mUnpluggedStarts;
7684
Dianne Hackborn61659e52014-07-09 16:13:01 -07007685 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007686 * Number of times the process has crashed before unplugged.
7687 */
7688 int mUnpluggedNumCrashes;
7689
7690 /**
7691 * Number of times the process has had an ANR before unplugged.
7692 */
7693 int mUnpluggedNumAnrs;
7694
Dianne Hackborn287952c2010-09-22 22:34:31 -07007695 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007696
Joe Onoratoabded112016-02-08 16:49:39 -08007697 public Proc(BatteryStatsImpl bsi, String name) {
7698 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007699 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007700 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007702
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007703 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 mUnpluggedUserTime = mUserTime;
7705 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007706 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007707 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007708 mUnpluggedNumCrashes = mNumCrashes;
7709 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 }
7711
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007712 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007714
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007715 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007716 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007717 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007718 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007719
Dianne Hackborn287952c2010-09-22 22:34:31 -07007720 public int countExcessivePowers() {
7721 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007722 }
7723
Dianne Hackborn287952c2010-09-22 22:34:31 -07007724 public ExcessivePower getExcessivePower(int i) {
7725 if (mExcessivePower != null) {
7726 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007727 }
7728 return null;
7729 }
7730
7731 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007732 if (mExcessivePower == null) {
7733 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007734 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007735 ExcessivePower ew = new ExcessivePower();
7736 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007737 ew.overTime = overTime;
7738 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007739 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007740 }
7741
Dianne Hackborn287952c2010-09-22 22:34:31 -07007742 public void addExcessiveCpu(long overTime, long usedTime) {
7743 if (mExcessivePower == null) {
7744 mExcessivePower = new ArrayList<ExcessivePower>();
7745 }
7746 ExcessivePower ew = new ExcessivePower();
7747 ew.type = ExcessivePower.TYPE_CPU;
7748 ew.overTime = overTime;
7749 ew.usedTime = usedTime;
7750 mExcessivePower.add(ew);
7751 }
7752
7753 void writeExcessivePowerToParcelLocked(Parcel out) {
7754 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007755 out.writeInt(0);
7756 return;
7757 }
7758
Dianne Hackborn287952c2010-09-22 22:34:31 -07007759 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007760 out.writeInt(N);
7761 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007762 ExcessivePower ew = mExcessivePower.get(i);
7763 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007764 out.writeLong(ew.overTime);
7765 out.writeLong(ew.usedTime);
7766 }
7767 }
7768
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007769 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007770 final int N = in.readInt();
7771 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007772 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007773 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007774 }
7775
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007776 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007777 throw new ParcelFormatException(
7778 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007779 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007780
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007781 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007782 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007783 ExcessivePower ew = new ExcessivePower();
7784 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007785 ew.overTime = in.readLong();
7786 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007787 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007788 }
7789 }
7790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 void writeToParcelLocked(Parcel out) {
7792 out.writeLong(mUserTime);
7793 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007794 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007796 out.writeInt(mNumCrashes);
7797 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 out.writeLong(mLoadedUserTime);
7799 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007800 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007802 out.writeInt(mLoadedNumCrashes);
7803 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 out.writeLong(mUnpluggedUserTime);
7805 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007806 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007808 out.writeInt(mUnpluggedNumCrashes);
7809 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007810 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 }
7812
7813 void readFromParcelLocked(Parcel in) {
7814 mUserTime = in.readLong();
7815 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007816 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007818 mNumCrashes = in.readInt();
7819 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 mLoadedUserTime = in.readLong();
7821 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007822 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007824 mLoadedNumCrashes = in.readInt();
7825 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 mUnpluggedUserTime = in.readLong();
7827 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007828 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007830 mUnpluggedNumCrashes = in.readInt();
7831 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007832 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 }
7834
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007835 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 mUserTime += utime;
7837 mSystemTime += stime;
7838 }
7839
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007840 public void addForegroundTimeLocked(long ttime) {
7841 mForegroundTime += ttime;
7842 }
7843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 public void incStartsLocked() {
7845 mStarts++;
7846 }
7847
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007848 public void incNumCrashesLocked() {
7849 mNumCrashes++;
7850 }
7851
7852 public void incNumAnrsLocked() {
7853 mNumAnrs++;
7854 }
7855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007856 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007857 public boolean isActive() {
7858 return mActive;
7859 }
7860
7861 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007863 long val = mUserTime;
7864 if (which == STATS_CURRENT) {
7865 val -= mLoadedUserTime;
7866 } else if (which == STATS_SINCE_UNPLUGGED) {
7867 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 }
7869 return val;
7870 }
7871
7872 @Override
7873 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007874 long val = mSystemTime;
7875 if (which == STATS_CURRENT) {
7876 val -= mLoadedSystemTime;
7877 } else if (which == STATS_SINCE_UNPLUGGED) {
7878 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 }
7880 return val;
7881 }
7882
7883 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007884 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007885 long val = mForegroundTime;
7886 if (which == STATS_CURRENT) {
7887 val -= mLoadedForegroundTime;
7888 } else if (which == STATS_SINCE_UNPLUGGED) {
7889 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007890 }
7891 return val;
7892 }
7893
7894 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007896 int val = mStarts;
7897 if (which == STATS_CURRENT) {
7898 val -= mLoadedStarts;
7899 } else if (which == STATS_SINCE_UNPLUGGED) {
7900 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 }
7902 return val;
7903 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007904
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007905 @Override
7906 public int getNumCrashes(int which) {
7907 int val = mNumCrashes;
7908 if (which == STATS_CURRENT) {
7909 val -= mLoadedNumCrashes;
7910 } else if (which == STATS_SINCE_UNPLUGGED) {
7911 val -= mUnpluggedNumCrashes;
7912 }
7913 return val;
7914 }
7915
7916 @Override
7917 public int getNumAnrs(int which) {
7918 int val = mNumAnrs;
7919 if (which == STATS_CURRENT) {
7920 val -= mLoadedNumAnrs;
7921 } else if (which == STATS_SINCE_UNPLUGGED) {
7922 val -= mUnpluggedNumAnrs;
7923 }
7924 return val;
7925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007926 }
7927
7928 /**
7929 * The statistics associated with a particular package.
7930 */
Joe Onoratoabded112016-02-08 16:49:39 -08007931 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7932 /**
7933 * BatteryStatsImpl that we are associated with.
7934 */
7935 protected BatteryStatsImpl mBsi;
7936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007938 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007939 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007940 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941
7942 /**
7943 * The statics we have collected for this package's services.
7944 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007945 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946
Joe Onoratoabded112016-02-08 16:49:39 -08007947 public Pkg(BatteryStatsImpl bsi) {
7948 mBsi = bsi;
7949 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007950 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007951
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007952 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 }
7954
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007955 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007957
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007958 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007959 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007960 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007962 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007963 int numWA = in.readInt();
7964 mWakeupAlarms.clear();
7965 for (int i=0; i<numWA; i++) {
7966 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007967 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969
7970 int numServs = in.readInt();
7971 mServiceStats.clear();
7972 for (int m = 0; m < numServs; m++) {
7973 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007974 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 mServiceStats.put(serviceName, serv);
7976
7977 serv.readFromParcelLocked(in);
7978 }
7979 }
7980
7981 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007982 int numWA = mWakeupAlarms.size();
7983 out.writeInt(numWA);
7984 for (int i=0; i<numWA; i++) {
7985 out.writeString(mWakeupAlarms.keyAt(i));
7986 mWakeupAlarms.valueAt(i).writeToParcel(out);
7987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007989 final int NS = mServiceStats.size();
7990 out.writeInt(NS);
7991 for (int i=0; i<NS; i++) {
7992 out.writeString(mServiceStats.keyAt(i));
7993 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007994 serv.writeToParcelLocked(out);
7995 }
7996 }
7997
7998 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007999 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
8000 return mWakeupAlarms;
8001 }
8002
8003 public void noteWakeupAlarmLocked(String tag) {
8004 Counter c = mWakeupAlarms.get(tag);
8005 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008006 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008007 mWakeupAlarms.put(tag, c);
8008 }
8009 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 }
8011
8012 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008013 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
8014 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 }
8016
8017 /**
8018 * The statistics associated with a particular service.
8019 */
Joe Onoratoabded112016-02-08 16:49:39 -08008020 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
8021 /**
8022 * BatteryStatsImpl that we are associated with.
8023 */
8024 protected BatteryStatsImpl mBsi;
8025
8026 /**
8027 * The android package in which this service resides.
8028 */
8029 protected Pkg mPkg;
8030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 /**
8032 * Total time (ms in battery uptime) the service has been left started.
8033 */
Joe Onoratoabded112016-02-08 16:49:39 -08008034 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035
8036 /**
8037 * If service has been started and not yet stopped, this is
8038 * when it was started.
8039 */
Joe Onoratoabded112016-02-08 16:49:39 -08008040 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041
8042 /**
8043 * True if we are currently running.
8044 */
Joe Onoratoabded112016-02-08 16:49:39 -08008045 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046
8047 /**
8048 * Total number of times startService() has been called.
8049 */
Joe Onoratoabded112016-02-08 16:49:39 -08008050 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051
8052 /**
8053 * Total time (ms in battery uptime) the service has been left launched.
8054 */
Joe Onoratoabded112016-02-08 16:49:39 -08008055 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056
8057 /**
8058 * If service has been launched and not yet exited, this is
8059 * when it was launched (ms in battery uptime).
8060 */
Joe Onoratoabded112016-02-08 16:49:39 -08008061 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008062
8063 /**
8064 * True if we are currently launched.
8065 */
Joe Onoratoabded112016-02-08 16:49:39 -08008066 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008067
8068 /**
8069 * Total number times the service has been launched.
8070 */
Joe Onoratoabded112016-02-08 16:49:39 -08008071 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072
8073 /**
8074 * The amount of time spent started loaded from a previous save
8075 * (ms in battery uptime).
8076 */
Joe Onoratoabded112016-02-08 16:49:39 -08008077 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078
8079 /**
8080 * The number of starts loaded from a previous save.
8081 */
Joe Onoratoabded112016-02-08 16:49:39 -08008082 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083
8084 /**
8085 * The number of launches loaded from a previous save.
8086 */
Joe Onoratoabded112016-02-08 16:49:39 -08008087 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088
8089 /**
8090 * The amount of time spent started as of the last run (ms
8091 * in battery uptime).
8092 */
Joe Onoratoabded112016-02-08 16:49:39 -08008093 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094
8095 /**
8096 * The number of starts as of the last run.
8097 */
Joe Onoratoabded112016-02-08 16:49:39 -08008098 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099
8100 /**
8101 * The number of launches as of the last run.
8102 */
Joe Onoratoabded112016-02-08 16:49:39 -08008103 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104
8105 /**
8106 * The amount of time spent started when last unplugged (ms
8107 * in battery uptime).
8108 */
Joe Onoratoabded112016-02-08 16:49:39 -08008109 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110
8111 /**
8112 * The number of starts when last unplugged.
8113 */
Joe Onoratoabded112016-02-08 16:49:39 -08008114 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115
8116 /**
8117 * The number of launches when last unplugged.
8118 */
Joe Onoratoabded112016-02-08 16:49:39 -08008119 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120
Joe Onoratoabded112016-02-08 16:49:39 -08008121 /**
8122 * Construct a Serv. Also adds it to the on-battery time base as a listener.
8123 */
8124 public Serv(BatteryStatsImpl bsi) {
8125 mBsi = bsi;
8126 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008128
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008129 public void onTimeStarted(long elapsedRealtime, long baseUptime,
8130 long baseRealtime) {
8131 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 mUnpluggedStarts = mStarts;
8133 mUnpluggedLaunches = mLaunches;
8134 }
8135
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008136 public void onTimeStopped(long elapsedRealtime, long baseUptime,
8137 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008138 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008139
Joe Onoratoabded112016-02-08 16:49:39 -08008140 /**
8141 * Remove this Serv as a listener from the time base.
8142 */
8143 public void detach() {
8144 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008145 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008146
Joe Onoratoabded112016-02-08 16:49:39 -08008147 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008148 mStartTime = in.readLong();
8149 mRunningSince = in.readLong();
8150 mRunning = in.readInt() != 0;
8151 mStarts = in.readInt();
8152 mLaunchedTime = in.readLong();
8153 mLaunchedSince = in.readLong();
8154 mLaunched = in.readInt() != 0;
8155 mLaunches = in.readInt();
8156 mLoadedStartTime = in.readLong();
8157 mLoadedStarts = in.readInt();
8158 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008159 mLastStartTime = 0;
8160 mLastStarts = 0;
8161 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 mUnpluggedStartTime = in.readLong();
8163 mUnpluggedStarts = in.readInt();
8164 mUnpluggedLaunches = in.readInt();
8165 }
8166
Joe Onoratoabded112016-02-08 16:49:39 -08008167 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 out.writeLong(mStartTime);
8169 out.writeLong(mRunningSince);
8170 out.writeInt(mRunning ? 1 : 0);
8171 out.writeInt(mStarts);
8172 out.writeLong(mLaunchedTime);
8173 out.writeLong(mLaunchedSince);
8174 out.writeInt(mLaunched ? 1 : 0);
8175 out.writeInt(mLaunches);
8176 out.writeLong(mLoadedStartTime);
8177 out.writeInt(mLoadedStarts);
8178 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 out.writeLong(mUnpluggedStartTime);
8180 out.writeInt(mUnpluggedStarts);
8181 out.writeInt(mUnpluggedLaunches);
8182 }
8183
Joe Onoratoabded112016-02-08 16:49:39 -08008184 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008185 if (!mLaunched) return mLaunchedTime;
8186 return mLaunchedTime + batteryUptime - mLaunchedSince;
8187 }
8188
Joe Onoratoabded112016-02-08 16:49:39 -08008189 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 if (!mRunning) return mStartTime;
8191 return mStartTime + batteryUptime - mRunningSince;
8192 }
8193
8194 public void startLaunchedLocked() {
8195 if (!mLaunched) {
8196 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08008197 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 mLaunched = true;
8199 }
8200 }
8201
8202 public void stopLaunchedLocked() {
8203 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08008204 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008205 if (time > 0) {
8206 mLaunchedTime += time;
8207 } else {
8208 mLaunches--;
8209 }
8210 mLaunched = false;
8211 }
8212 }
8213
8214 public void startRunningLocked() {
8215 if (!mRunning) {
8216 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08008217 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008218 mRunning = true;
8219 }
8220 }
8221
8222 public void stopRunningLocked() {
8223 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08008224 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 if (time > 0) {
8226 mStartTime += time;
8227 } else {
8228 mStarts--;
8229 }
8230 mRunning = false;
8231 }
8232 }
8233
8234 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008235 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 }
8237
8238 @Override
8239 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008240 int val = mLaunches;
8241 if (which == STATS_CURRENT) {
8242 val -= mLoadedLaunches;
8243 } else if (which == STATS_SINCE_UNPLUGGED) {
8244 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246 return val;
8247 }
8248
8249 @Override
8250 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008251 long val = getStartTimeToNowLocked(now);
8252 if (which == STATS_CURRENT) {
8253 val -= mLoadedStartTime;
8254 } else if (which == STATS_SINCE_UNPLUGGED) {
8255 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 return val;
8258 }
8259
8260 @Override
8261 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008262 int val = mStarts;
8263 if (which == STATS_CURRENT) {
8264 val -= mLoadedStarts;
8265 } else if (which == STATS_SINCE_UNPLUGGED) {
8266 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 }
8268
8269 return val;
8270 }
8271 }
8272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08008274 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 }
8276 }
8277
8278 /**
8279 * Retrieve the statistics object for a particular process, creating
8280 * if needed.
8281 */
8282 public Proc getProcessStatsLocked(String name) {
8283 Proc ps = mProcessStats.get(name);
8284 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008285 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 mProcessStats.put(name, ps);
8287 }
8288
8289 return ps;
8290 }
8291
Dianne Hackborna8d10942015-11-19 17:55:19 -08008292 public void updateUidProcessStateLocked(int procState) {
8293 int uidRunningState;
8294 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
8295 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
8296 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
8297 uidRunningState = PROCESS_STATE_TOP;
8298 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
8299 // Persistent and other foreground states go here.
8300 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
8301 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
8302 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
8303 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
8304 // Persistent and other foreground states go here.
8305 uidRunningState = PROCESS_STATE_FOREGROUND;
8306 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
8307 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008308 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08008309 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008310 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008311
Dianne Hackborna8d10942015-11-19 17:55:19 -08008312 if (mProcessState == uidRunningState) return;
8313
Bookatz867c0d72017-03-07 18:23:42 -08008314 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
8315 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08008316
8317 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
Bookatz867c0d72017-03-07 18:23:42 -08008318 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborna8d10942015-11-19 17:55:19 -08008319 }
8320 mProcessState = uidRunningState;
8321 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8322 if (mProcessStateTimer[uidRunningState] == null) {
8323 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008324 }
Bookatz867c0d72017-03-07 18:23:42 -08008325 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008326 }
Bookatz867c0d72017-03-07 18:23:42 -08008327
Bookatzc8c44962017-05-11 12:12:54 -07008328 updateOnBatteryBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
8329 updateOnBatteryScreenOffBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
Bookatz867c0d72017-03-07 18:23:42 -08008330 }
8331
Bookatzc8c44962017-05-11 12:12:54 -07008332 /** Whether to consider Uid to be in the background for background timebase purposes. */
8333 public boolean isInBackground() {
Bookatz867c0d72017-03-07 18:23:42 -08008334 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
8335 // also considered to be 'background' for our purposes, because it's not foreground.
Bookatzc8c44962017-05-11 12:12:54 -07008336 return mProcessState >= PROCESS_STATE_BACKGROUND;
8337 }
8338
8339 public boolean updateOnBatteryBgTimeBase(long uptimeUs, long realtimeUs) {
8340 boolean on = mBsi.mOnBatteryTimeBase.isRunning() && isInBackground();
8341 return mOnBatteryBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
8342 }
8343
8344 public boolean updateOnBatteryScreenOffBgTimeBase(long uptimeUs, long realtimeUs) {
8345 boolean on = mBsi.mOnBatteryScreenOffTimeBase.isRunning() && isInBackground();
8346 return mOnBatteryScreenOffBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008347 }
8348
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008349 public SparseArray<? extends Pid> getPidStats() {
8350 return mPids;
8351 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008352
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008353 public Pid getPidStatsLocked(int pid) {
8354 Pid p = mPids.get(pid);
8355 if (p == null) {
8356 p = new Pid();
8357 mPids.put(pid, p);
8358 }
8359 return p;
8360 }
8361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 /**
8363 * Retrieve the statistics object for a particular service, creating
8364 * if needed.
8365 */
8366 public Pkg getPackageStatsLocked(String name) {
8367 Pkg ps = mPackageStats.get(name);
8368 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008369 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008370 mPackageStats.put(name, ps);
8371 }
8372
8373 return ps;
8374 }
8375
8376 /**
8377 * Retrieve the statistics object for a particular service, creating
8378 * if needed.
8379 */
8380 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
8381 Pkg ps = getPackageStatsLocked(pkg);
8382 Pkg.Serv ss = ps.mServiceStats.get(serv);
8383 if (ss == null) {
8384 ss = ps.newServiceStatsLocked();
8385 ps.mServiceStats.put(serv, ss);
8386 }
8387
8388 return ss;
8389 }
8390
Dianne Hackbornd953c532014-08-16 18:17:38 -07008391 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008392 DualTimer timer = mSyncStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008393 timer.readSummaryFromParcelLocked(in);
8394 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008395 }
8396
Dianne Hackbornd953c532014-08-16 18:17:38 -07008397 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07008398 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008399 timer.readSummaryFromParcelLocked(in);
8400 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008401 }
8402
Dianne Hackbornd953c532014-08-16 18:17:38 -07008403 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008404 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008405 mWakelockStats.add(wlName, wl);
8406 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008407 getWakelockTimerLocked(wl, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008409 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008410 getWakelockTimerLocked(wl, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008411 }
8412 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008413 getWakelockTimerLocked(wl, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008415 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008416 getWakelockTimerLocked(wl, WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 }
8419
Bookatz867c0d72017-03-07 18:23:42 -08008420 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008421 Sensor se = mSensorStats.get(sensor);
8422 if (se == null) {
8423 if (!create) {
8424 return null;
8425 }
Joe Onoratoabded112016-02-08 16:49:39 -08008426 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 mSensorStats.put(sensor, se);
8428 }
Bookatz867c0d72017-03-07 18:23:42 -08008429 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 if (t != null) {
8431 return t;
8432 }
Joe Onoratoabded112016-02-08 16:49:39 -08008433 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008434 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008435 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008436 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008437 }
Bookatz867c0d72017-03-07 18:23:42 -08008438 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8439 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440 se.mTimer = t;
8441 return t;
8442 }
8443
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008444 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008445 DualTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008446 if (t != null) {
8447 t.startRunningLocked(elapsedRealtimeMs);
8448 }
8449 }
8450
8451 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008452 DualTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008453 if (t != null) {
8454 t.stopRunningLocked(elapsedRealtimeMs);
8455 }
8456 }
8457
8458 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008459 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008460 if (t != null) {
8461 t.startRunningLocked(elapsedRealtimeMs);
8462 }
8463 }
8464
8465 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008466 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008467 if (t != null) {
8468 t.stopRunningLocked(elapsedRealtimeMs);
8469 }
8470 }
8471
Bookatz5b5ec322017-05-26 09:40:38 -07008472 public StopwatchTimer getWakelockTimerLocked(Wakelock wl, int type) {
8473 if (wl == null) {
8474 return null;
8475 }
8476 switch (type) {
8477 case WAKE_TYPE_PARTIAL: {
8478 DualTimer t = wl.mTimerPartial;
8479 if (t == null) {
8480 t = new DualTimer(mBsi.mClocks, this, WAKE_TYPE_PARTIAL,
8481 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase,
8482 mOnBatteryScreenOffBackgroundTimeBase);
8483 wl.mTimerPartial = t;
8484 }
8485 return t;
8486 }
8487 case WAKE_TYPE_FULL: {
8488 StopwatchTimer t = wl.mTimerFull;
8489 if (t == null) {
8490 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_FULL,
8491 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
8492 wl.mTimerFull = t;
8493 }
8494 return t;
8495 }
8496 case WAKE_TYPE_WINDOW: {
8497 StopwatchTimer t = wl.mTimerWindow;
8498 if (t == null) {
8499 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_WINDOW,
8500 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
8501 wl.mTimerWindow = t;
8502 }
8503 return t;
8504 }
8505 case WAKE_TYPE_DRAW: {
8506 StopwatchTimer t = wl.mTimerDraw;
8507 if (t == null) {
8508 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_DRAW,
8509 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
8510 wl.mTimerDraw = t;
8511 }
8512 return t;
8513 }
8514 default:
8515 throw new IllegalArgumentException("type=" + type);
8516 }
8517 }
8518
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008519 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008520 Wakelock wl = mWakelockStats.startObject(name);
8521 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008522 getWakelockTimerLocked(wl, type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 }
Bookatzc8c44962017-05-11 12:12:54 -07008524 if (type == WAKE_TYPE_PARTIAL) {
8525 createAggregatedPartialWakelockTimerLocked().startRunningLocked(elapsedRealtimeMs);
8526 if (pid >= 0) {
8527 Pid p = getPidStatsLocked(pid);
8528 if (p.mWakeNesting++ == 0) {
8529 p.mWakeStartMs = elapsedRealtimeMs;
8530 }
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008531 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 }
8534
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008535 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008536 Wakelock wl = mWakelockStats.stopObject(name);
8537 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008538 getWakelockTimerLocked(wl, type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 }
Bookatzc8c44962017-05-11 12:12:54 -07008540 if (type == WAKE_TYPE_PARTIAL) {
8541 if (mAggregatedPartialWakelockTimer != null) {
8542 mAggregatedPartialWakelockTimer.stopRunningLocked(elapsedRealtimeMs);
8543 }
8544 if (pid >= 0) {
8545 Pid p = mPids.get(pid);
8546 if (p != null && p.mWakeNesting > 0) {
8547 if (p.mWakeNesting-- == 1) {
8548 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8549 p.mWakeStartMs = 0;
8550 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008551 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008552 }
8553 }
8554 }
8555
8556 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
8557 Proc p = getProcessStatsLocked(proc);
8558 if (p != null) {
8559 p.addExcessiveWake(overTime, usedTime);
8560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008562
Dianne Hackborn287952c2010-09-22 22:34:31 -07008563 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8564 Proc p = getProcessStatsLocked(proc);
8565 if (p != null) {
8566 p.addExcessiveCpu(overTime, usedTime);
8567 }
8568 }
8569
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008570 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008571 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008572 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 }
8574
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008575 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008576 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008577 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008578 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008579 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008581 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008582
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008583 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008584 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008586
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008587 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008588 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 }
8590
8591 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008592 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008593 }
8594 }
8595
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008596 public long[] getCpuFreqs() {
8597 return mCpuFreqs;
8598 }
8599
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008600 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008601 this(new SystemClocks(), systemDir, handler, externalSync, null);
8602 }
8603
8604 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
8605 PlatformIdleStateCallback cb) {
8606 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08008607 }
8608
8609 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008610 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08008611 init(clocks);
8612
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008613 if (systemDir != null) {
8614 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
8615 new File(systemDir, "batterystats.bin.tmp"));
8616 } else {
8617 mFile = null;
8618 }
8619 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008620 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008621 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08008622 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008623 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08008624 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008625 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008626 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008627 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008628 }
Joe Onoratoabded112016-02-08 16:49:39 -08008629 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8630 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8631 mOnBatteryTimeBase);
8632 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8633 mOnBatteryTimeBase);
8634 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8635 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8636 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8637 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8638 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8639 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8640 mOnBatteryTimeBase);
8641 }
8642 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8643 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008644 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008645 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008646 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008647 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008648 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008649 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8650 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008651 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008652 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8653 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8654 NUM_BT_TX_LEVELS);
8655 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8656 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008657 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8658 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8659 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008660 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008661 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8662 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008663 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8664 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008665 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008666 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008667 mOnBatteryTimeBase);
8668 }
Joe Onoratoabded112016-02-08 16:49:39 -08008669 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8670 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8671 mOnBatteryTimeBase);
8672 }
8673 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8674 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8675 mOnBatteryTimeBase);
8676 }
8677 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8678 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8679 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8680 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8681 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008682 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8683 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008685 long uptime = mClocks.uptimeMillis() * 1000;
8686 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008687 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008688 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008689 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008690 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008691 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008692 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008693 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008694 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008695 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008696 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008697 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 }
8699
8700 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008701 this(new SystemClocks(), p);
8702 }
8703
8704 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8705 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008706 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008707 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008708 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008709 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008710 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008711 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008712 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008713 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 }
8715
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008716 public void setPowerProfileLocked(PowerProfile profile) {
8717 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008718
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008719 // We need to initialize the KernelCpuSpeedReaders to read from
8720 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8721 // information.
8722 final int numClusters = mPowerProfile.getNumCpuClusters();
8723 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8724 int firstCpuOfCluster = 0;
8725 for (int i = 0; i < numClusters; i++) {
8726 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8727 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8728 numSpeedSteps);
8729 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8730 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008731
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008732 if (mEstimatedBatteryCapacity == -1) {
8733 // Initialize the estimated battery capacity to a known preset one.
8734 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
Adam Lesinskie08af192015-03-25 16:42:59 -07008735 }
8736 }
8737
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008738 public void setCallback(BatteryCallback cb) {
8739 mCallback = cb;
8740 }
8741
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008742 public void setRadioScanningTimeoutLocked(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07008743 if (mPhoneSignalScanningTimer != null) {
8744 mPhoneSignalScanningTimer.setTimeout(timeout);
8745 }
8746 }
8747
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008748 public void updateDailyDeadlineLocked() {
8749 // Get the current time.
8750 long currentTime = mDailyStartTime = System.currentTimeMillis();
8751 Calendar calDeadline = Calendar.getInstance();
8752 calDeadline.setTimeInMillis(currentTime);
8753
8754 // Move time up to the next day, ranging from 1am to 3pm.
8755 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8756 calDeadline.set(Calendar.MILLISECOND, 0);
8757 calDeadline.set(Calendar.SECOND, 0);
8758 calDeadline.set(Calendar.MINUTE, 0);
8759 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8760 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8761 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8762 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8763 }
8764
8765 public void recordDailyStatsIfNeededLocked(boolean settled) {
8766 long currentTime = System.currentTimeMillis();
8767 if (currentTime >= mNextMaxDailyDeadline) {
8768 recordDailyStatsLocked();
8769 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8770 recordDailyStatsLocked();
8771 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8772 recordDailyStatsLocked();
8773 }
8774 }
8775
8776 public void recordDailyStatsLocked() {
8777 DailyItem item = new DailyItem();
8778 item.mStartTime = mDailyStartTime;
8779 item.mEndTime = System.currentTimeMillis();
8780 boolean hasData = false;
8781 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8782 hasData = true;
8783 item.mDischargeSteps = new LevelStepTracker(
8784 mDailyDischargeStepTracker.mNumStepDurations,
8785 mDailyDischargeStepTracker.mStepDurations);
8786 }
8787 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8788 hasData = true;
8789 item.mChargeSteps = new LevelStepTracker(
8790 mDailyChargeStepTracker.mNumStepDurations,
8791 mDailyChargeStepTracker.mStepDurations);
8792 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008793 if (mDailyPackageChanges != null) {
8794 hasData = true;
8795 item.mPackageChanges = mDailyPackageChanges;
8796 mDailyPackageChanges = null;
8797 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008798 mDailyDischargeStepTracker.init();
8799 mDailyChargeStepTracker.init();
8800 updateDailyDeadlineLocked();
8801
8802 if (hasData) {
8803 mDailyItems.add(item);
8804 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8805 mDailyItems.remove(0);
8806 }
8807 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8808 try {
8809 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008810 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008811 writeDailyItemsLocked(out);
8812 BackgroundThread.getHandler().post(new Runnable() {
8813 @Override
8814 public void run() {
8815 synchronized (mCheckinFile) {
8816 FileOutputStream stream = null;
8817 try {
8818 stream = mDailyFile.startWrite();
8819 memStream.writeTo(stream);
8820 stream.flush();
8821 FileUtils.sync(stream);
8822 stream.close();
8823 mDailyFile.finishWrite(stream);
8824 } catch (IOException e) {
8825 Slog.w("BatteryStats",
8826 "Error writing battery daily items", e);
8827 mDailyFile.failWrite(stream);
8828 }
8829 }
8830 }
8831 });
8832 } catch (IOException e) {
8833 }
8834 }
8835 }
8836
8837 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8838 StringBuilder sb = new StringBuilder(64);
8839 out.startDocument(null, true);
8840 out.startTag(null, "daily-items");
8841 for (int i=0; i<mDailyItems.size(); i++) {
8842 final DailyItem dit = mDailyItems.get(i);
8843 out.startTag(null, "item");
8844 out.attribute(null, "start", Long.toString(dit.mStartTime));
8845 out.attribute(null, "end", Long.toString(dit.mEndTime));
8846 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8847 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008848 if (dit.mPackageChanges != null) {
8849 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8850 PackageChange pc = dit.mPackageChanges.get(j);
8851 if (pc.mUpdate) {
8852 out.startTag(null, "upd");
8853 out.attribute(null, "pkg", pc.mPackageName);
8854 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8855 out.endTag(null, "upd");
8856 } else {
8857 out.startTag(null, "rem");
8858 out.attribute(null, "pkg", pc.mPackageName);
8859 out.endTag(null, "rem");
8860 }
8861 }
8862 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008863 out.endTag(null, "item");
8864 }
8865 out.endTag(null, "daily-items");
8866 out.endDocument();
8867 }
8868
8869 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
8870 StringBuilder tmpBuilder) throws IOException {
8871 if (steps != null) {
8872 out.startTag(null, tag);
8873 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
8874 for (int i=0; i<steps.mNumStepDurations; i++) {
8875 out.startTag(null, "s");
8876 tmpBuilder.setLength(0);
8877 steps.encodeEntryAt(i, tmpBuilder);
8878 out.attribute(null, "v", tmpBuilder.toString());
8879 out.endTag(null, "s");
8880 }
8881 out.endTag(null, tag);
8882 }
8883 }
8884
8885 public void readDailyStatsLocked() {
8886 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
8887 mDailyItems.clear();
8888 FileInputStream stream;
8889 try {
8890 stream = mDailyFile.openRead();
8891 } catch (FileNotFoundException e) {
8892 return;
8893 }
8894 try {
8895 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008896 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008897 readDailyItemsLocked(parser);
8898 } catch (XmlPullParserException e) {
8899 } finally {
8900 try {
8901 stream.close();
8902 } catch (IOException e) {
8903 }
8904 }
8905 }
8906
8907 private void readDailyItemsLocked(XmlPullParser parser) {
8908 try {
8909 int type;
8910 while ((type = parser.next()) != XmlPullParser.START_TAG
8911 && type != XmlPullParser.END_DOCUMENT) {
8912 ;
8913 }
8914
8915 if (type != XmlPullParser.START_TAG) {
8916 throw new IllegalStateException("no start tag found");
8917 }
8918
8919 int outerDepth = parser.getDepth();
8920 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8921 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8922 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8923 continue;
8924 }
8925
8926 String tagName = parser.getName();
8927 if (tagName.equals("item")) {
8928 readDailyItemTagLocked(parser);
8929 } else {
8930 Slog.w(TAG, "Unknown element under <daily-items>: "
8931 + parser.getName());
8932 XmlUtils.skipCurrentTag(parser);
8933 }
8934 }
8935
8936 } catch (IllegalStateException e) {
8937 Slog.w(TAG, "Failed parsing daily " + e);
8938 } catch (NullPointerException e) {
8939 Slog.w(TAG, "Failed parsing daily " + e);
8940 } catch (NumberFormatException e) {
8941 Slog.w(TAG, "Failed parsing daily " + e);
8942 } catch (XmlPullParserException e) {
8943 Slog.w(TAG, "Failed parsing daily " + e);
8944 } catch (IOException e) {
8945 Slog.w(TAG, "Failed parsing daily " + e);
8946 } catch (IndexOutOfBoundsException e) {
8947 Slog.w(TAG, "Failed parsing daily " + e);
8948 }
8949 }
8950
8951 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8952 XmlPullParserException, IOException {
8953 DailyItem dit = new DailyItem();
8954 String attr = parser.getAttributeValue(null, "start");
8955 if (attr != null) {
8956 dit.mStartTime = Long.parseLong(attr);
8957 }
8958 attr = parser.getAttributeValue(null, "end");
8959 if (attr != null) {
8960 dit.mEndTime = Long.parseLong(attr);
8961 }
8962 int outerDepth = parser.getDepth();
8963 int type;
8964 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8965 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8966 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8967 continue;
8968 }
8969
8970 String tagName = parser.getName();
8971 if (tagName.equals("dis")) {
8972 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8973 } else if (tagName.equals("chg")) {
8974 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008975 } else if (tagName.equals("upd")) {
8976 if (dit.mPackageChanges == null) {
8977 dit.mPackageChanges = new ArrayList<>();
8978 }
8979 PackageChange pc = new PackageChange();
8980 pc.mUpdate = true;
8981 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8982 String verStr = parser.getAttributeValue(null, "ver");
8983 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8984 dit.mPackageChanges.add(pc);
8985 XmlUtils.skipCurrentTag(parser);
8986 } else if (tagName.equals("rem")) {
8987 if (dit.mPackageChanges == null) {
8988 dit.mPackageChanges = new ArrayList<>();
8989 }
8990 PackageChange pc = new PackageChange();
8991 pc.mUpdate = false;
8992 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8993 dit.mPackageChanges.add(pc);
8994 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008995 } else {
8996 Slog.w(TAG, "Unknown element under <item>: "
8997 + parser.getName());
8998 XmlUtils.skipCurrentTag(parser);
8999 }
9000 }
9001 mDailyItems.add(dit);
9002 }
9003
9004 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
9005 String tag)
9006 throws NumberFormatException, XmlPullParserException, IOException {
9007 final String numAttr = parser.getAttributeValue(null, "n");
9008 if (numAttr == null) {
9009 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
9010 XmlUtils.skipCurrentTag(parser);
9011 return;
9012 }
9013 final int num = Integer.parseInt(numAttr);
9014 LevelStepTracker steps = new LevelStepTracker(num);
9015 if (isCharge) {
9016 dit.mChargeSteps = steps;
9017 } else {
9018 dit.mDischargeSteps = steps;
9019 }
9020 int i = 0;
9021 int outerDepth = parser.getDepth();
9022 int type;
9023 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9024 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9025 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9026 continue;
9027 }
9028
9029 String tagName = parser.getName();
9030 if ("s".equals(tagName)) {
9031 if (i < num) {
9032 String valueAttr = parser.getAttributeValue(null, "v");
9033 if (valueAttr != null) {
9034 steps.decodeEntryAt(i, valueAttr);
9035 i++;
9036 }
9037 }
9038 } else {
9039 Slog.w(TAG, "Unknown element under <" + tag + ">: "
9040 + parser.getName());
9041 XmlUtils.skipCurrentTag(parser);
9042 }
9043 }
9044 steps.mNumStepDurations = i;
9045 }
9046
9047 @Override
9048 public DailyItem getDailyItemLocked(int daysAgo) {
9049 int index = mDailyItems.size()-1-daysAgo;
9050 return index >= 0 ? mDailyItems.get(index) : null;
9051 }
9052
9053 @Override
9054 public long getCurrentDailyStartTime() {
9055 return mDailyStartTime;
9056 }
9057
9058 @Override
9059 public long getNextMinDailyDeadline() {
9060 return mNextMinDailyDeadline;
9061 }
9062
9063 @Override
9064 public long getNextMaxDailyDeadline() {
9065 return mNextMaxDailyDeadline;
9066 }
9067
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009068 @Override
9069 public boolean startIteratingOldHistoryLocked() {
9070 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9071 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009072 if ((mHistoryIterator = mHistory) == null) {
9073 return false;
9074 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009075 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009076 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009077 mReadOverflow = false;
9078 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009079 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009080 }
9081
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009082 @Override
9083 public boolean getNextOldHistoryLocked(HistoryItem out) {
9084 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
9085 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009086 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009087 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009088 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009089 HistoryItem cur = mHistoryIterator;
9090 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009091 if (!mReadOverflow && !end) {
9092 Slog.w(TAG, "Old history ends before new history!");
9093 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009094 return false;
9095 }
9096 out.setTo(cur);
9097 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009098 if (!mReadOverflow) {
9099 if (end) {
9100 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009101 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07009102 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009103 pw.println("Histories differ!");
9104 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009105 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009106 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009107 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
9108 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07009109 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009110 }
9111 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009112 return true;
9113 }
9114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009116 public void finishIteratingOldHistoryLocked() {
9117 mIteratingHistory = false;
9118 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009119 mHistoryIterator = null;
9120 }
9121
9122 public int getHistoryTotalSize() {
9123 return MAX_HISTORY_BUFFER;
9124 }
9125
9126 public int getHistoryUsedSize() {
9127 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009128 }
9129
9130 @Override
9131 public boolean startIteratingHistoryLocked() {
9132 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9133 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009134 if (mHistoryBuffer.dataSize() <= 0) {
9135 return false;
9136 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009137 mHistoryBuffer.setDataPosition(0);
9138 mReadOverflow = false;
9139 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009140 mReadHistoryStrings = new String[mHistoryTagPool.size()];
9141 mReadHistoryUids = new int[mHistoryTagPool.size()];
9142 mReadHistoryChars = 0;
9143 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9144 final HistoryTag tag = ent.getKey();
9145 final int idx = ent.getValue();
9146 mReadHistoryStrings[idx] = tag.string;
9147 mReadHistoryUids[idx] = tag.uid;
9148 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009149 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009150 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009151 }
9152
9153 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08009154 public int getHistoryStringPoolSize() {
9155 return mReadHistoryStrings.length;
9156 }
9157
9158 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009159 public int getHistoryStringPoolBytes() {
9160 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
9161 // Each string character is 2 bytes.
9162 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
9163 }
9164
9165 @Override
9166 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009167 return mReadHistoryStrings[index];
9168 }
9169
9170 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009171 public int getHistoryTagPoolUid(int index) {
9172 return mReadHistoryUids[index];
9173 }
9174
9175 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009176 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009177 final int pos = mHistoryBuffer.dataPosition();
9178 if (pos == 0) {
9179 out.clear();
9180 }
9181 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009182 if (end) {
9183 return false;
9184 }
9185
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009186 final long lastRealtime = out.time;
9187 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009188 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07009189 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
9190 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009191 out.currentTime = lastWalltime + (out.time - lastRealtime);
9192 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009193 return true;
9194 }
9195
9196 @Override
9197 public void finishIteratingHistoryLocked() {
9198 mIteratingHistory = false;
9199 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08009200 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009201 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009202
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009203 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009204 public long getHistoryBaseTime() {
9205 return mHistoryBaseTime;
9206 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009207
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009208 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009209 public int getStartCount() {
9210 return mStartCount;
9211 }
9212
9213 public boolean isOnBattery() {
9214 return mOnBattery;
9215 }
9216
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009217 public boolean isCharging() {
9218 return mCharging;
9219 }
9220
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009221 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009222 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009223 }
9224
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009225 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009226 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009227 mOnBatteryTimeBase.init(uptime, realtime);
9228 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009229 mRealtime = 0;
9230 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009231 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07009232 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009233 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009234
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009235 void initDischarge() {
9236 mLowDischargeAmountSinceCharge = 0;
9237 mHighDischargeAmountSinceCharge = 0;
9238 mDischargeAmountScreenOn = 0;
9239 mDischargeAmountScreenOnSinceCharge = 0;
9240 mDischargeAmountScreenOff = 0;
9241 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009242 mDischargeStepTracker.init();
9243 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009244 mDischargeScreenOffCounter.reset(false);
9245 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009246 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009247
9248 public void resetAllStatsCmdLocked() {
9249 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08009250 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009251 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08009252 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009253 long realtime = mSecRealtime * 1000;
9254 mDischargeStartLevel = mHistoryCur.batteryLevel;
9255 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009256 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009257 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
9258 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009259 mOnBatteryTimeBase.reset(uptime, realtime);
9260 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
9261 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009262 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009263 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
9264 mDischargeScreenOffUnplugLevel = 0;
9265 } else {
9266 mDischargeScreenOnUnplugLevel = 0;
9267 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
9268 }
9269 mDischargeAmountScreenOn = 0;
9270 mDischargeAmountScreenOff = 0;
9271 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009272 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009273 }
9274
9275 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07009276 final long uptimeMillis = mClocks.uptimeMillis();
9277 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009278 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07009279 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009280 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009281 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009282 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009283 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07009284
9285 if (mPowerProfile != null) {
9286 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
9287 } else {
9288 mEstimatedBatteryCapacity = -1;
9289 }
Jocelyn Dangc627d102017-04-14 13:15:14 -07009290 mMinLearnedBatteryCapacity = -1;
9291 mMaxLearnedBatteryCapacity = -1;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009292 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009293 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07009294 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009295 mLongestLightIdleTime = 0;
9296 mLongestFullIdleTime = 0;
9297 mDeviceIdleModeLightTimer.reset(false);
9298 mDeviceIdleModeFullTimer.reset(false);
9299 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009300 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009301 mPhoneOnTimer.reset(false);
9302 mAudioOnTimer.reset(false);
9303 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009304 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009305 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009306 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08009307 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009308 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009309 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009310 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009311 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009312 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009313 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009314 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009315 mNetworkByteActivityCounters[i].reset(false);
9316 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009317 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009318 mMobileRadioActiveTimer.reset(false);
9319 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009320 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009321 mMobileRadioActiveUnknownTime.reset(false);
9322 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009323 mWifiOnTimer.reset(false);
9324 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009325 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009326 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009327 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009328 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9329 mWifiSupplStateTimer[i].reset(false);
9330 }
9331 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9332 mWifiSignalStrengthsTimer[i].reset(false);
9333 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009334 mWifiActivity.reset(false);
9335 mBluetoothActivity.reset(false);
9336 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009337 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009338
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009339 for (int i=0; i<mUidStats.size(); i++) {
Bookatz993a0be2017-07-21 09:03:23 -07009340 if (mUidStats.valueAt(i).reset(uptimeMillis * 1000, elapsedRealtimeMillis * 1000)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009341 mUidStats.remove(mUidStats.keyAt(i));
9342 i--;
9343 }
9344 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009345
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009346 if (mKernelWakelockStats.size() > 0) {
9347 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009348 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009349 }
9350 mKernelWakelockStats.clear();
9351 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009352
James Carr3a226052016-07-01 14:49:52 -07009353 if (mKernelMemoryStats.size() > 0) {
9354 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
9355 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
9356 }
9357 mKernelMemoryStats.clear();
9358 }
9359
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009360 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009361 for (SamplingTimer timer : mWakeupReasonStats.values()) {
9362 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009363 }
9364 mWakeupReasonStats.clear();
9365 }
9366
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009367 mLastHistoryStepDetails = null;
9368 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
9369 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
9370 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
9371 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
9372 mLastStepStatUserTime = mCurStepStatUserTime = 0;
9373 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
9374 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
9375 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
9376 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
9377 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
9378
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009379 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009380
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009381 clearHistoryLocked();
9382 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009383
Dianne Hackborn40c87252014-03-19 16:55:40 -07009384 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009385 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009386 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
9387 // Not recording process starts/stops.
9388 continue;
9389 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009390 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009391 if (active == null) {
9392 continue;
9393 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009394 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
9395 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009396 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07009397 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
9398 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009399 }
9400 }
9401 }
9402 }
9403
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009404 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009405 if (oldScreenOn) {
9406 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
9407 if (diff > 0) {
9408 mDischargeAmountScreenOn += diff;
9409 mDischargeAmountScreenOnSinceCharge += diff;
9410 }
9411 } else {
9412 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
9413 if (diff > 0) {
9414 mDischargeAmountScreenOff += diff;
9415 mDischargeAmountScreenOffSinceCharge += diff;
9416 }
9417 }
9418 if (newScreenOn) {
9419 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
9420 mDischargeScreenOffUnplugLevel = 0;
9421 } else {
9422 mDischargeScreenOnUnplugLevel = 0;
9423 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
9424 }
9425 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009426
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009427 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009428 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009429 final boolean screenOn = mScreenState == Display.STATE_ON;
9430 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009431 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009432 }
9433
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009434 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
9435 private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
9436
9437 private final Object mWifiNetworkLock = new Object();
9438
9439 @GuardedBy("mWifiNetworkLock")
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009440 private String[] mWifiIfaces = EmptyArray.STRING;
9441
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009442 @GuardedBy("mWifiNetworkLock")
9443 private NetworkStats mLastWifiNetworkStats = new NetworkStats(0, -1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009444
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009445 private final Object mModemNetworkLock = new Object();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009446
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009447 @GuardedBy("mModemNetworkLock")
9448 private String[] mModemIfaces = EmptyArray.STRING;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009449
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009450 @GuardedBy("mModemNetworkLock")
9451 private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
9452
9453 private NetworkStats readNetworkStatsLocked(String[] ifaces) {
9454 try {
9455 if (!ArrayUtils.isEmpty(ifaces)) {
9456 return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
9457 NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
9458 }
9459 } catch (IOException e) {
9460 Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009461 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009462 return null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009463 }
9464
9465 /**
9466 * Distribute WiFi energy info and network traffic to apps.
9467 * @param info The energy information from the WiFi controller.
9468 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009469 public void updateWifiState(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009470 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009471 Slog.d(TAG, "Updating wifi stats: " + Arrays.toString(mWifiIfaces));
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009472 }
9473
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009474 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009475 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009476 synchronized (mWifiNetworkLock) {
9477 final NetworkStats latestStats = readNetworkStatsLocked(mWifiIfaces);
9478 if (latestStats != null) {
9479 delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
9480 mNetworkStatsPool.acquire());
9481 mNetworkStatsPool.release(mLastWifiNetworkStats);
9482 mLastWifiNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009483 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009484 }
9485
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009486 synchronized (this) {
9487 if (!mOnBatteryInternal) {
9488 if (delta != null) {
9489 mNetworkStatsPool.release(delta);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009490 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009491 return;
9492 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009493
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009494 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
9495 SparseLongArray rxPackets = new SparseLongArray();
9496 SparseLongArray txPackets = new SparseLongArray();
9497 long totalTxPackets = 0;
9498 long totalRxPackets = 0;
9499 if (delta != null) {
9500 NetworkStats.Entry entry = new NetworkStats.Entry();
9501 final int size = delta.size();
9502 for (int i = 0; i < size; i++) {
9503 entry = delta.getValues(i, entry);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009504
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009505 if (DEBUG_ENERGY) {
9506 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
9507 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9508 + " txPackets=" + entry.txPackets);
9509 }
9510
9511 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9512 // Skip the lookup below since there is no work to do.
9513 continue;
9514 }
9515
9516 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9517 if (entry.rxBytes != 0) {
9518 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009519 entry.rxPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009520 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9521 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
9522 entry.rxPackets);
9523 }
9524 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9525 entry.rxBytes);
9526 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9527 entry.rxPackets);
9528
9529 rxPackets.put(u.getUid(), entry.rxPackets);
9530
9531 // Sum the total number of packets so that the Rx Power can
9532 // be evenly distributed amongst the apps.
9533 totalRxPackets += entry.rxPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009534 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009535
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009536 if (entry.txBytes != 0) {
9537 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009538 entry.txPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009539 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9540 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
9541 entry.txPackets);
9542 }
9543 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9544 entry.txBytes);
9545 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9546 entry.txPackets);
9547
9548 txPackets.put(u.getUid(), entry.txPackets);
9549
9550 // Sum the total number of packets so that the Tx Power can
9551 // be evenly distributed amongst the apps.
9552 totalTxPackets += entry.txPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009553 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009554 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009555 mNetworkStatsPool.release(delta);
9556 delta = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07009557 }
9558
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009559 if (info != null) {
9560 mHasWifiReporting = true;
Adam Lesinskie08af192015-03-25 16:42:59 -07009561
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009562 // Measured in mAms
9563 final long txTimeMs = info.getControllerTxTimeMillis();
9564 final long rxTimeMs = info.getControllerRxTimeMillis();
9565 final long idleTimeMs = info.getControllerIdleTimeMillis();
9566 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009567
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009568 long leftOverRxTimeMs = rxTimeMs;
9569 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009570
Adam Lesinskie08af192015-03-25 16:42:59 -07009571 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009572 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
9573 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9574 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9575 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
9576 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009577 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009578
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009579 long totalWifiLockTimeMs = 0;
9580 long totalScanTimeMs = 0;
9581
9582 // On the first pass, collect some totals so that we can normalize power
9583 // calculations if we need to.
9584 final int uidStatsSize = mUidStats.size();
9585 for (int i = 0; i < uidStatsSize; i++) {
9586 final Uid uid = mUidStats.valueAt(i);
9587
9588 // Sum the total scan power for all apps.
9589 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
9590 elapsedRealtimeMs * 1000) / 1000;
9591
9592 // Sum the total time holding wifi lock for all apps.
9593 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9594 elapsedRealtimeMs * 1000) / 1000;
9595 }
9596
9597 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
9598 Slog.d(TAG,
9599 " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
9600 + rxTimeMs + " ms). Normalizing scan time.");
9601 }
9602 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
9603 Slog.d(TAG,
9604 " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
9605 + txTimeMs + " ms). Normalizing scan time.");
9606 }
9607
9608 // Actually assign and distribute power usage to apps.
9609 for (int i = 0; i < uidStatsSize; i++) {
9610 final Uid uid = mUidStats.valueAt(i);
9611
9612 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
9613 elapsedRealtimeMs * 1000) / 1000;
9614 if (scanTimeSinceMarkMs > 0) {
9615 // Set the new mark so that next time we get new data since this point.
9616 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
9617
9618 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
9619 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
9620
9621 // Our total scan time is more than the reported Tx/Rx time.
9622 // This is possible because the cost of a scan is approximate.
9623 // Let's normalize the result so that we evenly blame each app
9624 // scanning.
9625 //
9626 // This means that we may have apps that transmitted/received packets not be
9627 // blamed for this, but this is fine as scans are relatively more expensive.
9628 if (totalScanTimeMs > rxTimeMs) {
9629 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
9630 totalScanTimeMs;
9631 }
9632 if (totalScanTimeMs > txTimeMs) {
9633 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9634 totalScanTimeMs;
9635 }
9636
9637 if (DEBUG_ENERGY) {
9638 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9639 + scanRxTimeSinceMarkMs + " ms Tx:"
9640 + scanTxTimeSinceMarkMs + " ms)");
9641 }
9642
9643 ControllerActivityCounterImpl activityCounter =
9644 uid.getOrCreateWifiControllerActivityLocked();
9645 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9646 activityCounter.getTxTimeCounters()[0].addCountLocked(
9647 scanTxTimeSinceMarkMs);
9648 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9649 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
9650 }
9651
9652 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9653 // lock.
9654 final long wifiLockTimeSinceMarkMs =
9655 uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9656 elapsedRealtimeMs * 1000) / 1000;
9657 if (wifiLockTimeSinceMarkMs > 0) {
9658 // Set the new mark so that next time we get new data since this point.
9659 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9660
9661 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9662 / totalWifiLockTimeMs;
9663 if (DEBUG_ENERGY) {
9664 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9665 + myIdleTimeMs + " ms");
9666 }
9667 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9668 .addCountLocked(myIdleTimeMs);
9669 }
9670 }
9671
Adam Lesinskie08af192015-03-25 16:42:59 -07009672 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009673 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
9674 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009675 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009676
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009677 // Distribute the remaining Tx power appropriately between all apps that transmitted
9678 // packets.
9679 for (int i = 0; i < txPackets.size(); i++) {
9680 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
9681 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs)
9682 / totalTxPackets;
9683 if (DEBUG_ENERGY) {
9684 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9685 }
9686 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9687 .addCountLocked(myTxTimeMs);
9688 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009689
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009690 // Distribute the remaining Rx power appropriately between all apps that received
9691 // packets.
9692 for (int i = 0; i < rxPackets.size(); i++) {
9693 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9694 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs)
9695 / totalRxPackets;
9696 if (DEBUG_ENERGY) {
9697 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9698 }
9699 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9700 .addCountLocked(myRxTimeMs);
9701 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009702
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009703 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9704
9705
9706 // Update WiFi controller stats.
9707 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9708 mWifiActivity.getTxTimeCounters()[0].addCountLocked(
9709 info.getControllerTxTimeMillis());
9710 mWifiActivity.getIdleTimeCounter().addCountLocked(
9711 info.getControllerIdleTimeMillis());
9712
9713 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9714 final double opVolt = mPowerProfile.getAveragePower(
9715 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9716 if (opVolt != 0) {
9717 // We store the power drain as mAms.
9718 mWifiActivity.getPowerCounter().addCountLocked(
9719 (long) (info.getControllerEnergyUsed() / opVolt));
9720 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009721 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009722 }
9723 }
9724
9725 /**
9726 * Distribute Cell radio energy info and network traffic to apps.
9727 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009728 public void updateMobileRadioState(@Nullable final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009729 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009730 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009731 }
9732
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009733 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009734 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009735 synchronized (mModemNetworkLock) {
9736 final NetworkStats latestStats = readNetworkStatsLocked(mModemIfaces);
9737 if (latestStats != null) {
9738 delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
9739 mNetworkStatsPool.acquire());
9740 mNetworkStatsPool.release(mLastModemNetworkStats);
9741 mLastModemNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009742 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009743 }
9744
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009745 synchronized (this) {
9746 if (!mOnBatteryInternal) {
9747 if (delta != null) {
9748 mNetworkStatsPool.release(delta);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009749 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009750 return;
Adam Lesinskie08af192015-03-25 16:42:59 -07009751 }
9752
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009753 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
9754 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9755 elapsedRealtimeMs * 1000);
9756 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
9757
9758 long totalRxPackets = 0;
9759 long totalTxPackets = 0;
9760 if (delta != null) {
9761 NetworkStats.Entry entry = new NetworkStats.Entry();
9762 final int size = delta.size();
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009763 for (int i = 0; i < size; i++) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009764 entry = delta.getValues(i, entry);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009765 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9766 continue;
9767 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009768
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009769 if (DEBUG_ENERGY) {
9770 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9771 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9772 + " txPackets=" + entry.txPackets);
9773 }
9774
9775 totalRxPackets += entry.rxPackets;
9776 totalTxPackets += entry.txPackets;
9777
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009778 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009779 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
9780 entry.rxPackets);
9781 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
9782 entry.txPackets);
9783 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9784 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
9785 entry.rxBytes, entry.rxPackets);
9786 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
9787 entry.txBytes, entry.txPackets);
9788 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009789
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009790 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9791 entry.rxBytes);
9792 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9793 entry.txBytes);
9794 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9795 entry.rxPackets);
9796 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9797 entry.txPackets);
9798 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009799
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009800 // Now distribute proportional blame to the apps that did networking.
9801 long totalPackets = totalRxPackets + totalTxPackets;
9802 if (totalPackets > 0) {
9803 for (int i = 0; i < size; i++) {
9804 entry = delta.getValues(i, entry);
9805 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9806 continue;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009807 }
9808
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009809 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9810
9811 // Distribute total radio active time in to this app.
9812 final long appPackets = entry.rxPackets + entry.txPackets;
9813 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9814 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9815
9816 // Remove this app from the totals, so that we don't lose any time
9817 // due to rounding.
9818 radioTime -= appRadioTime;
9819 totalPackets -= appPackets;
9820
9821 if (activityInfo != null) {
9822 ControllerActivityCounterImpl activityCounter =
9823 u.getOrCreateModemControllerActivityLocked();
9824 if (totalRxPackets > 0 && entry.rxPackets > 0) {
9825 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9826 / totalRxPackets;
9827 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9828 }
9829
9830 if (totalTxPackets > 0 && entry.txPackets > 0) {
9831 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9832 long txMs =
9833 entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9834 txMs /= totalTxPackets;
9835 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9836 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009837 }
9838 }
9839 }
9840 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009841
9842 if (radioTime > 0) {
9843 // Whoops, there is some radio time we can't blame on an app!
9844 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9845 mMobileRadioActiveUnknownCount.addCountLocked(1);
9846 }
9847
9848 mNetworkStatsPool.release(delta);
9849 delta = null;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009850 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009851
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009852 if (activityInfo != null) {
9853 mHasModemReporting = true;
9854 mModemActivity.getIdleTimeCounter().addCountLocked(
9855 activityInfo.getIdleTimeMillis());
9856 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9857 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9858 mModemActivity.getTxTimeCounters()[lvl]
9859 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9860 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009861
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009862 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9863 final double opVolt = mPowerProfile.getAveragePower(
9864 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9865 if (opVolt != 0) {
9866 // We store the power drain as mAms.
9867 mModemActivity.getPowerCounter().addCountLocked(
9868 (long) (activityInfo.getEnergyUsed() / opVolt));
9869 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009870 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009871 }
9872 }
9873
9874 /**
9875 * Distribute Bluetooth energy info and network traffic to apps.
9876 * @param info The energy information from the bluetooth controller.
9877 */
9878 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009879 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009880 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009881 }
9882
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009883 if (info == null || !mOnBatteryInternal) {
9884 return;
9885 }
Adam Lesinskie283d332015-04-16 12:29:25 -07009886
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009887 mHasBluetoothReporting = true;
9888
Bookatz867c0d72017-03-07 18:23:42 -08009889 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009890 final long rxTimeMs = info.getControllerRxTimeMillis();
9891 final long txTimeMs = info.getControllerTxTimeMillis();
9892
9893 if (DEBUG_ENERGY) {
9894 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
9895 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9896 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9897 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
9898 }
9899
9900 long totalScanTimeMs = 0;
9901
9902 final int uidCount = mUidStats.size();
9903 for (int i = 0; i < uidCount; i++) {
9904 final Uid u = mUidStats.valueAt(i);
9905 if (u.mBluetoothScanTimer == null) {
9906 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07009907 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009908
Bookatzaa4594a2017-03-24 12:39:56 -07009909 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009910 elapsedRealtimeMs * 1000) / 1000;
9911 }
9912
9913 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
9914 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
9915
9916 if (DEBUG_ENERGY) {
9917 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
9918 + " TX=" + normalizeScanTxTime);
9919 }
9920
9921 long leftOverRxTimeMs = rxTimeMs;
9922 long leftOverTxTimeMs = txTimeMs;
9923
9924 for (int i = 0; i < uidCount; i++) {
9925 final Uid u = mUidStats.valueAt(i);
9926 if (u.mBluetoothScanTimer == null) {
9927 continue;
9928 }
9929
Bookatzaa4594a2017-03-24 12:39:56 -07009930 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009931 elapsedRealtimeMs * 1000) / 1000;
9932 if (scanTimeSinceMarkMs > 0) {
9933 // Set the new mark so that next time we get new data since this point.
9934 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
9935
9936 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
9937 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
9938
9939 if (normalizeScanRxTime) {
9940 // Scan time is longer than the total rx time in the controller,
9941 // so distribute the scan time proportionately. This means regular traffic
9942 // will not blamed, but scans are more expensive anyways.
9943 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
9944 }
9945
9946 if (normalizeScanTxTime) {
9947 // Scan time is longer than the total tx time in the controller,
9948 // so distribute the scan time proportionately. This means regular traffic
9949 // will not blamed, but scans are more expensive anyways.
9950 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
9951 }
9952
9953 final ControllerActivityCounterImpl counter =
9954 u.getOrCreateBluetoothControllerActivityLocked();
9955 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
9956 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
9957
9958 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
9959 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
9960 }
9961 }
9962
9963 if (DEBUG_ENERGY) {
9964 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
9965 + " TX=" + leftOverTxTimeMs);
9966 }
9967
9968 //
9969 // Now distribute blame to apps that did bluetooth traffic.
9970 //
9971
9972 long totalTxBytes = 0;
9973 long totalRxBytes = 0;
9974
9975 final UidTraffic[] uidTraffic = info.getUidTraffic();
9976 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9977 for (int i = 0; i < numUids; i++) {
9978 final UidTraffic traffic = uidTraffic[i];
9979
9980 // Add to the global counters.
9981 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9982 traffic.getRxBytes());
9983 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9984 traffic.getTxBytes());
9985
9986 // Add to the UID counters.
9987 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9988 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9989 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9990
9991 // Calculate the total traffic.
9992 totalTxBytes += traffic.getTxBytes();
9993 totalRxBytes += traffic.getRxBytes();
9994 }
9995
9996 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9997 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009998 for (int i = 0; i < numUids; i++) {
9999 final UidTraffic traffic = uidTraffic[i];
10000
Adam Lesinski50e47602015-12-04 17:04:54 -080010001 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010002 final ControllerActivityCounterImpl counter =
10003 u.getOrCreateBluetoothControllerActivityLocked();
10004
10005 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
10006 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
10007
10008 if (DEBUG_ENERGY) {
10009 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
10010 + " rx_time=" + timeRxMs);
10011 }
10012 counter.getRxTimeCounter().addCountLocked(timeRxMs);
10013 leftOverRxTimeMs -= timeRxMs;
10014 }
10015
10016 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
10017 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
10018
10019 if (DEBUG_ENERGY) {
10020 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
10021 + " tx_time=" + timeTxMs);
10022 }
10023
10024 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
10025 leftOverTxTimeMs -= timeTxMs;
10026 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010027 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010028 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010029
10030 mBluetoothActivity.getRxTimeCounter().addCountLocked(
10031 info.getControllerRxTimeMillis());
10032 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
10033 info.getControllerTxTimeMillis());
10034 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
10035 info.getControllerIdleTimeMillis());
10036
10037 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10038 final double opVolt = mPowerProfile.getAveragePower(
10039 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10040 if (opVolt != 0) {
10041 // We store the power drain as mAms.
10042 mBluetoothActivity.getPowerCounter().addCountLocked(
10043 (long) (info.getControllerEnergyUsed() / opVolt));
10044 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010045 }
10046
10047 /**
10048 * Read and distribute kernel wake lock use across apps.
10049 */
10050 public void updateKernelWakelocksLocked() {
10051 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
10052 mTmpWakelockStats);
10053 if (wakelockStats == null) {
10054 // Not crashing might make board bringup easier.
10055 Slog.w(TAG, "Couldn't get kernel wake lock stats");
10056 return;
10057 }
10058
10059 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
10060 String name = ent.getKey();
10061 KernelWakelockStats.Entry kws = ent.getValue();
10062
10063 SamplingTimer kwlt = mKernelWakelockStats.get(name);
10064 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010065 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010066 mKernelWakelockStats.put(name, kwlt);
10067 }
Adam Lesinskid84ad302016-05-17 18:31:02 -070010068
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010069 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010070 kwlt.setUpdateVersion(kws.mVersion);
10071 }
10072
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010073 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -070010074 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
10075 // this time.
10076 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10077 SamplingTimer st = ent.getValue();
10078 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
10079 st.endSample();
10080 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010081 }
10082 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010083
Adam Lesinskid84ad302016-05-17 18:31:02 -070010084 // Record whether we've seen a non-zero time (for debugging b/22716723).
10085 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010086 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
10087 }
10088
10089 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
10090 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
10091 wakelockStats.kernelWakelockVersion);
10092 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010093 }
10094
Adam Lesinski72478f02015-06-17 15:39:43 -070010095 // We use an anonymous class to access these variables,
10096 // so they can't live on the stack or they'd have to be
10097 // final MutableLong objects (more allocations).
10098 // Used in updateCpuTimeLocked().
10099 long mTempTotalCpuUserTimeUs;
10100 long mTempTotalCpuSystemTimeUs;
10101
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010102 /**
James Carr3a226052016-07-01 14:49:52 -070010103 * Reads the newest memory stats from the kernel.
10104 */
10105 public void updateKernelMemoryBandwidthLocked() {
10106 mKernelMemoryBandwidthStats.updateStats();
10107 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
10108 final int bandwidthEntryCount = bandwidthEntries.size();
10109 int index;
10110 for (int i = 0; i < bandwidthEntryCount; i++) {
10111 SamplingTimer timer;
10112 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
10113 timer = mKernelMemoryStats.valueAt(index);
10114 } else {
10115 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
10116 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
10117 }
10118 timer.update(bandwidthEntries.valueAt(i), 1);
10119 if (DEBUG_MEMORY) {
10120 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
10121 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
10122 mKernelMemoryStats.get(
10123 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
10124 mKernelMemoryStats.size()));
10125 }
10126 }
10127 }
10128
10129 /**
Adam Lesinski72478f02015-06-17 15:39:43 -070010130 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
10131 * and we are on battery with screen off, we give more of the cpu time to those apps holding
10132 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010133 */
Sudheer Shanka671985f2017-05-19 11:33:42 -070010134 public void updateCpuTimeLocked(boolean updateCpuFreqData) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010135 if (mPowerProfile == null) {
10136 return;
10137 }
10138
Adam Lesinski72478f02015-06-17 15:39:43 -070010139 if (DEBUG_ENERGY_CPU) {
10140 Slog.d(TAG, "!Cpu updating!");
10141 }
10142
10143 // Holding a wakelock costs more than just using the cpu.
10144 // Currently, we assign only half the cpu time to an app that is running but
10145 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
10146 // If no app is holding a wakelock, then the distribution is normal.
10147 final int wakelockWeight = 50;
10148
Adam Lesinski6832f392015-09-05 18:05:40 -070010149 // Read the time spent for each cluster at various cpu frequencies.
10150 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
10151 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
10152 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
10153 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010154
10155 int numWakelocks = 0;
10156
10157 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
10158 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
10159 final int numPartialTimers = mPartialTimers.size();
10160 if (mOnBatteryScreenOffTimeBase.isRunning()) {
10161 for (int i = 0; i < numPartialTimers; i++) {
10162 final StopwatchTimer timer = mPartialTimers.get(i);
10163 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
10164 // Since the collection and blaming of wakelocks can be scheduled to run after
10165 // some delay, the mPartialTimers list may have new entries. We can't blame
10166 // the newly added timer for past cpu time, so we only consider timers that
10167 // were present for one round of collection. Once a timer has gone through
10168 // a round of collection, its mInList field is set to true.
10169 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010170 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010171 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010172 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010173
10174 final int numWakelocksF = numWakelocks;
10175 mTempTotalCpuUserTimeUs = 0;
10176 mTempTotalCpuSystemTimeUs = 0;
10177
10178 // Read the CPU data for each UID. This will internally generate a snapshot so next time
10179 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
10180 // we just ignore the data.
Amith Yamasanid0ddeba2017-05-26 09:46:58 -070010181 final long startTimeMs = mClocks.uptimeMillis();
Adam Lesinski72478f02015-06-17 15:39:43 -070010182 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
10183 new KernelUidCpuTimeReader.Callback() {
10184 @Override
Adam Lesinskid4abd1e2017-04-12 11:29:13 -070010185 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs) {
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010186 uid = mapUid(uid);
10187 if (Process.isIsolated(uid)) {
10188 // This could happen if the isolated uid mapping was removed before
10189 // that process was actually killed.
10190 mKernelUidCpuTimeReader.removeUid(uid);
10191 Slog.d(TAG, "Got readings for an isolated uid with"
10192 + " no mapping to owning uid: " + uid);
10193 return;
10194 }
10195 final Uid u = getUidStatsLocked(uid);
Adam Lesinski72478f02015-06-17 15:39:43 -070010196
10197 // Accumulate the total system and user time.
10198 mTempTotalCpuUserTimeUs += userTimeUs;
10199 mTempTotalCpuSystemTimeUs += systemTimeUs;
10200
10201 StringBuilder sb = null;
10202 if (DEBUG_ENERGY_CPU) {
10203 sb = new StringBuilder();
10204 sb.append(" got time for uid=").append(u.mUid).append(": u=");
10205 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10206 sb.append(" s=");
10207 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -070010208 sb.append("\n");
Adam Lesinski72478f02015-06-17 15:39:43 -070010209 }
10210
10211 if (numWakelocksF > 0) {
10212 // We have wakelocks being held, so only give a portion of the
10213 // time to the process. The rest will be distributed among wakelock
10214 // holders.
10215 userTimeUs = (userTimeUs * wakelockWeight) / 100;
10216 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
10217 }
10218
10219 if (sb != null) {
10220 sb.append(" adding to uid=").append(u.mUid).append(": u=");
10221 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10222 sb.append(" s=");
10223 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10224 Slog.d(TAG, sb.toString());
10225 }
10226
10227 u.mUserCpuTime.addCountLocked(userTimeUs);
10228 u.mSystemCpuTime.addCountLocked(systemTimeUs);
10229
10230 // Add the cpu speeds to this UID. These are used as a ratio
10231 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010232 final int numClusters = mPowerProfile.getNumCpuClusters();
10233 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
10234 numClusters) {
10235 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -070010236 }
10237
10238 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010239 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
10240 cluster);
10241 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
10242 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010243 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010244 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -070010245 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010246
10247 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
10248 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
10249 if (cpuSpeeds[speed] == null) {
10250 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10251 }
10252 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
10253 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010254 }
10255 }
10256 });
10257
Sudheer Shanka671985f2017-05-19 11:33:42 -070010258 if (updateCpuFreqData) {
10259 readKernelUidCpuFreqTimesLocked();
10260 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010261
Amith Yamasanid0ddeba2017-05-26 09:46:58 -070010262 final long elapse = (mClocks.uptimeMillis() - startTimeMs);
Makoto Onuki3f8e2972017-05-11 10:52:37 -070010263 if (DEBUG_ENERGY_CPU || (elapse >= 100)) {
10264 Slog.d(TAG, "Reading cpu stats took " + elapse + " ms");
Adam Lesinski72478f02015-06-17 15:39:43 -070010265 }
10266
10267 if (mOnBatteryInternal && numWakelocks > 0) {
10268 // Distribute a portion of the total cpu time to wakelock holders.
10269 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
10270 mTempTotalCpuSystemTimeUs =
10271 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
10272
10273 for (int i = 0; i < numPartialTimers; i++) {
10274 final StopwatchTimer timer = mPartialTimers.get(i);
10275
10276 // The system does not share any blame, as it is usually holding the wakelock
10277 // on behalf of an app.
10278 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
10279 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
10280 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
10281
10282 if (DEBUG_ENERGY_CPU) {
10283 StringBuilder sb = new StringBuilder();
10284 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
10285 .append(": u=");
10286 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10287 sb.append(" s=");
10288 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10289 Slog.d(TAG, sb.toString());
10290 }
10291
10292 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
10293 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
10294
10295 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -070010296 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -070010297
10298 mTempTotalCpuUserTimeUs -= userTimeUs;
10299 mTempTotalCpuSystemTimeUs -= systemTimeUs;
10300 numWakelocks--;
10301 }
10302 }
10303
10304 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
10305 // Anything left over is given to the system.
10306 if (DEBUG_ENERGY_CPU) {
10307 StringBuilder sb = new StringBuilder();
10308 sb.append(" Distributing lost time to system: u=");
10309 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
10310 sb.append(" s=");
10311 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
10312 Slog.d(TAG, sb.toString());
10313 }
10314
10315 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
10316 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
10317 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
10318
10319 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -070010320 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
10321 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -070010322 }
10323 }
10324
10325 // See if there is a difference in wakelocks between this collection and the last
10326 // collection.
10327 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
10328 // No difference, so each timer is now considered for the next collection.
10329 for (int i = 0; i < numPartialTimers; i++) {
10330 mPartialTimers.get(i).mInList = true;
10331 }
10332 } else {
10333 // The lists are different, meaning we added (or removed a timer) since the last
10334 // collection.
10335 final int numLastPartialTimers = mLastPartialTimers.size();
10336 for (int i = 0; i < numLastPartialTimers; i++) {
10337 mLastPartialTimers.get(i).mInList = false;
10338 }
10339 mLastPartialTimers.clear();
10340
10341 // Mark the current timers as gone through a collection.
10342 for (int i = 0; i < numPartialTimers; i++) {
10343 final StopwatchTimer timer = mPartialTimers.get(i);
10344 timer.mInList = true;
10345 mLastPartialTimers.add(timer);
10346 }
10347 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010348 }
10349
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010350 void readKernelUidCpuFreqTimesLocked() {
10351 mKernelUidCpuFreqTimeReader.readDelta(!mOnBatteryInternal ? null :
10352 new KernelUidCpuFreqTimeReader.Callback() {
10353 @Override
10354 public void onCpuFreqs(long[] cpuFreqs) {
10355 mCpuFreqs = cpuFreqs;
10356 }
10357
10358 @Override
10359 public void onUidCpuFreqTime(int uid, long[] cpuFreqTimeMs) {
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010360 uid = mapUid(uid);
10361 if (Process.isIsolated(uid)) {
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070010362 mKernelUidCpuFreqTimeReader.removeUid(uid);
10363 Slog.d(TAG, "Got freq readings for an isolated uid with"
10364 + " no mapping to owning uid: " + uid);
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010365 return;
10366 }
10367 final Uid u = getUidStatsLocked(uid);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010368 if (u.mCpuFreqTimeMs == null) {
10369 u.mCpuFreqTimeMs = new LongSamplingCounterArray(mOnBatteryTimeBase);
10370 }
10371 u.mCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
10372 if (u.mScreenOffCpuFreqTimeMs == null) {
10373 u.mScreenOffCpuFreqTimeMs = new LongSamplingCounterArray(
10374 mOnBatteryScreenOffTimeBase);
10375 }
10376 u.mScreenOffCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
10377 }
10378 });
10379 }
10380
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010381 boolean setChargingLocked(boolean charging) {
10382 if (mCharging != charging) {
10383 mCharging = charging;
10384 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010385 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010386 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010387 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010388 }
10389 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
10390 return true;
10391 }
10392 return false;
10393 }
10394
Dianne Hackborn40c87252014-03-19 16:55:40 -070010395 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010396 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010397 boolean doWrite = false;
10398 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
10399 m.arg1 = onBattery ? 1 : 0;
10400 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010401
Dianne Hackborn40c87252014-03-19 16:55:40 -070010402 final long uptime = mSecUptime * 1000;
10403 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010404 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010405 if (onBattery) {
10406 // We will reset our status if we are unplugging after the
10407 // battery was last full, or the level is at 100, or
10408 // we have gone through a significant charge (from a very low
10409 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010410 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -070010411 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010412 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010413 || (mDischargeCurrentLevel < 20 && level >= 80)
10414 || (getHighDischargeAmountSinceCharge() >= 200
10415 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010416 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010417 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010418 + " lowAmount=" + getLowDischargeAmountSinceCharge()
10419 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010420 // Before we write, collect a snapshot of the final aggregated
10421 // stats to be reported in the next checkin. Only do this if we have
10422 // a sufficient amount of data to make it interesting.
10423 if (getLowDischargeAmountSinceCharge() >= 20) {
10424 final Parcel parcel = Parcel.obtain();
10425 writeSummaryToParcel(parcel, true);
10426 BackgroundThread.getHandler().post(new Runnable() {
10427 @Override public void run() {
10428 synchronized (mCheckinFile) {
10429 FileOutputStream stream = null;
10430 try {
10431 stream = mCheckinFile.startWrite();
10432 stream.write(parcel.marshall());
10433 stream.flush();
10434 FileUtils.sync(stream);
10435 stream.close();
10436 mCheckinFile.finishWrite(stream);
10437 } catch (IOException e) {
10438 Slog.w("BatteryStats",
10439 "Error writing checkin battery statistics", e);
10440 mCheckinFile.failWrite(stream);
10441 } finally {
10442 parcel.recycle();
10443 }
10444 }
10445 }
10446 });
10447 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010448 doWrite = true;
10449 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -080010450 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010451 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -080010452 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010453 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010454 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010455 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010456 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010457 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010458 if (mCharging) {
10459 setChargingLocked(false);
10460 }
10461 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010462 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010463 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010464 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010465 mDischargeStepTracker.clearTime();
10466 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010467 mInitStepMode = mCurStepMode;
10468 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010469 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010470 mHistoryCur.batteryLevel = (byte)level;
10471 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10472 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
10473 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010474 if (reset) {
10475 mRecordingHistory = true;
10476 startRecordingHistory(mSecRealtime, mSecUptime, reset);
10477 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010478 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010479 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010480 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010481 mDischargeScreenOnUnplugLevel = level;
10482 mDischargeScreenOffUnplugLevel = 0;
10483 } else {
10484 mDischargeScreenOnUnplugLevel = 0;
10485 mDischargeScreenOffUnplugLevel = level;
10486 }
10487 mDischargeAmountScreenOn = 0;
10488 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010489 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010490 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010491 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010492 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010493 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010494 mHistoryCur.batteryLevel = (byte)level;
10495 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10496 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
10497 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -070010498 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010499 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010500 if (level < mDischargeUnplugLevel) {
10501 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
10502 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
10503 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010504 updateDischargeScreenLevelsLocked(screenOn, screenOn);
10505 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010506 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010507 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010508 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010509 mInitStepMode = mCurStepMode;
10510 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010511 }
10512 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
10513 if (mFile != null) {
10514 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010515 }
10516 }
10517 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010518
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010519 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
10520 boolean reset) {
10521 mRecordingHistory = true;
10522 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070010523 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
10524 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010525 mHistoryCur);
10526 mHistoryCur.currentTime = 0;
10527 if (reset) {
10528 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
10529 }
10530 }
10531
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010532 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
10533 final long uptimeMs) {
10534 if (mRecordingHistory) {
10535 mHistoryCur.currentTime = currentTime;
10536 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
10537 mHistoryCur);
10538 mHistoryCur.currentTime = 0;
10539 }
10540 }
10541
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010542 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
10543 if (mRecordingHistory) {
10544 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010545 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
10546 mHistoryCur);
10547 mHistoryCur.currentTime = 0;
10548 }
10549 }
10550
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010551 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010552 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010553 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010554 }
10555 }
10556
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010557 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010558 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010559
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010560 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -080010561 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070010562 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
10563 temp = Math.max(0, temp);
10564
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010565 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080010566 final long uptime = mClocks.uptimeMillis();
10567 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010568 if (!mHaveBatteryLevel) {
10569 mHaveBatteryLevel = true;
10570 // We start out assuming that the device is plugged in (not
10571 // on battery). If our first report is now that we are indeed
10572 // plugged in, then twiddle our state to correctly reflect that
10573 // since we won't be going through the full setOnBattery().
10574 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010575 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010576 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010577 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010578 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010579 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010580 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010581 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010582 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010583 mHistoryCur.batteryStatus = (byte)status;
10584 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010585 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010586 mMaxChargeStepLevel = mMinDischargeStepLevel =
10587 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010588 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010589 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
10590 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
10591 }
10592 int oldStatus = mHistoryCur.batteryStatus;
10593 if (onBattery) {
10594 mDischargeCurrentLevel = level;
10595 if (!mRecordingHistory) {
10596 mRecordingHistory = true;
10597 startRecordingHistory(elapsedRealtime, uptime, true);
10598 }
10599 } else if (level < 96) {
10600 if (!mRecordingHistory) {
10601 mRecordingHistory = true;
10602 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010603 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010604 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010605 mCurrentBatteryLevel = level;
10606 if (mDischargePlugLevel < 0) {
10607 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070010608 }
Adam Lesinski926969b2016-04-28 17:31:12 -070010609
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010610 if (onBattery != mOnBattery) {
10611 mHistoryCur.batteryLevel = (byte)level;
10612 mHistoryCur.batteryStatus = (byte)status;
10613 mHistoryCur.batteryHealth = (byte)health;
10614 mHistoryCur.batteryPlugType = (byte)plugType;
10615 mHistoryCur.batteryTemperature = (short)temp;
10616 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010617 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10618 // Only record discharges
10619 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10620 mDischargeCounter.addCountLocked(chargeDiff);
10621 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10622 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010623 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010624 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010625 } else {
10626 boolean changed = false;
10627 if (mHistoryCur.batteryLevel != level) {
10628 mHistoryCur.batteryLevel = (byte)level;
10629 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070010630
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010631 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
10632 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010633 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070010634 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010635 if (mHistoryCur.batteryStatus != status) {
10636 mHistoryCur.batteryStatus = (byte)status;
10637 changed = true;
10638 }
10639 if (mHistoryCur.batteryHealth != health) {
10640 mHistoryCur.batteryHealth = (byte)health;
10641 changed = true;
10642 }
10643 if (mHistoryCur.batteryPlugType != plugType) {
10644 mHistoryCur.batteryPlugType = (byte)plugType;
10645 changed = true;
10646 }
10647 if (temp >= (mHistoryCur.batteryTemperature+10)
10648 || temp <= (mHistoryCur.batteryTemperature-10)) {
10649 mHistoryCur.batteryTemperature = (short)temp;
10650 changed = true;
10651 }
10652 if (volt > (mHistoryCur.batteryVoltage+20)
10653 || volt < (mHistoryCur.batteryVoltage-20)) {
10654 mHistoryCur.batteryVoltage = (char)volt;
10655 changed = true;
10656 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010657 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
10658 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010659 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10660 // Only record discharges
10661 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10662 mDischargeCounter.addCountLocked(chargeDiff);
10663 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10664 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010665 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070010666 changed = true;
10667 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010668 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
10669 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
10670 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
10671 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010672 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010673 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
10674 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10675 modeBits, elapsedRealtime);
10676 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10677 modeBits, elapsedRealtime);
10678 mLastDischargeStepLevel = level;
10679 mMinDischargeStepLevel = level;
10680 mInitStepMode = mCurStepMode;
10681 mModStepMode = 0;
10682 }
10683 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010684 if (level >= 90) {
10685 // If the battery level is at least 90%, always consider the device to be
10686 // charging even if it happens to go down a level.
10687 changed |= setChargingLocked(true);
10688 mLastChargeStepLevel = level;
10689 } if (!mCharging) {
10690 if (mLastChargeStepLevel < level) {
10691 // We have not reporting that we are charging, but the level has now
10692 // gone up, so consider the state to be charging.
10693 changed |= setChargingLocked(true);
10694 mLastChargeStepLevel = level;
10695 }
10696 } else {
10697 if (mLastChargeStepLevel > level) {
10698 // We had reported that the device was charging, but here we are with
10699 // power connected and the level going down. Looks like the current
10700 // power supplied isn't enough, so consider the device to now be
10701 // discharging.
10702 changed |= setChargingLocked(false);
10703 mLastChargeStepLevel = level;
10704 }
10705 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010706 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
10707 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10708 modeBits, elapsedRealtime);
10709 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10710 modeBits, elapsedRealtime);
10711 mLastChargeStepLevel = level;
10712 mMaxChargeStepLevel = level;
10713 mInitStepMode = mCurStepMode;
10714 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070010715 }
10716 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010717 if (changed) {
10718 addHistoryRecordLocked(elapsedRealtime, uptime);
10719 }
Evan Millarc64edde2009-04-18 12:26:32 -070010720 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010721 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10722 // We don't record history while we are plugged in and fully charged.
10723 // The next time we are unplugged, history will be cleared.
10724 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010725 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010726
Jocelyn Dangc627d102017-04-14 13:15:14 -070010727 if (mMinLearnedBatteryCapacity == -1) {
10728 mMinLearnedBatteryCapacity = chargeFullUAh;
10729 } else {
10730 Math.min(mMinLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski041d9172016-12-12 12:03:56 -080010731 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070010732 mMaxLearnedBatteryCapacity = Math.max(mMaxLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski33dac552015-03-09 15:24:48 -070010733 }
10734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 public long getAwakeTimeBattery() {
10736 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
10737 }
10738
10739 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080010740 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010741 }
10742
10743 @Override
10744 public long computeUptime(long curTime, int which) {
10745 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010746 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010748 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 }
10750 return 0;
10751 }
10752
10753 @Override
10754 public long computeRealtime(long curTime, int which) {
10755 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010756 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010758 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010759 }
10760 return 0;
10761 }
10762
10763 @Override
10764 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010765 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 }
10767
10768 @Override
10769 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010770 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 }
10772
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010773 @Override
10774 public long computeBatteryScreenOffUptime(long curTime, int which) {
10775 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
10776 }
10777
10778 @Override
10779 public long computeBatteryScreenOffRealtime(long curTime, int which) {
10780 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010781 }
10782
Dianne Hackborn260c5022014-04-29 11:23:16 -070010783 private long computeTimePerLevel(long[] steps, int numSteps) {
10784 // For now we'll do a simple average across all steps.
10785 if (numSteps <= 0) {
10786 return -1;
10787 }
10788 long total = 0;
10789 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010790 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010791 }
10792 return total / numSteps;
10793 /*
10794 long[] buckets = new long[numSteps];
10795 int numBuckets = 0;
10796 int numToAverage = 4;
10797 int i = 0;
10798 while (i < numSteps) {
10799 long totalTime = 0;
10800 int num = 0;
10801 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010802 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010803 num++;
10804 }
10805 buckets[numBuckets] = totalTime / num;
10806 numBuckets++;
10807 numToAverage *= 2;
10808 i += num;
10809 }
10810 if (numBuckets < 1) {
10811 return -1;
10812 }
10813 long averageTime = buckets[numBuckets-1];
10814 for (i=numBuckets-2; i>=0; i--) {
10815 averageTime = (averageTime + buckets[i]) / 2;
10816 }
10817 return averageTime;
10818 */
10819 }
10820
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010821 @Override
10822 public long computeBatteryTimeRemaining(long curTime) {
10823 if (!mOnBattery) {
10824 return -1;
10825 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010826 /* Simple implementation just looks at the average discharge per level across the
10827 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010828 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
10829 if (discharge < 2) {
10830 return -1;
10831 }
10832 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
10833 if (duration < 1000*1000) {
10834 return -1;
10835 }
10836 long usPerLevel = duration/discharge;
10837 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010838 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010839 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010840 return -1;
10841 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010842 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010843 if (msPerLevel <= 0) {
10844 return -1;
10845 }
10846 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010847 }
10848
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010849 @Override
10850 public LevelStepTracker getDischargeLevelStepTracker() {
10851 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010852 }
10853
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010854 @Override
10855 public LevelStepTracker getDailyDischargeLevelStepTracker() {
10856 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010857 }
10858
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010859 @Override
10860 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010861 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010862 // Not yet working.
10863 return -1;
10864 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010865 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010866 int curLevel = mCurrentBatteryLevel;
10867 int plugLevel = mDischargePlugLevel;
10868 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
10869 return -1;
10870 }
10871 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
10872 if (duration < 1000*1000) {
10873 return -1;
10874 }
10875 long usPerLevel = duration/(curLevel-plugLevel);
10876 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070010877 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010878 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010879 return -1;
10880 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010881 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010882 if (msPerLevel <= 0) {
10883 return -1;
10884 }
10885 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010886 }
10887
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010888 @Override
10889 public LevelStepTracker getChargeLevelStepTracker() {
10890 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010891 }
10892
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010893 @Override
10894 public LevelStepTracker getDailyChargeLevelStepTracker() {
10895 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010896 }
10897
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010898 @Override
10899 public ArrayList<PackageChange> getDailyPackageChanges() {
10900 return mDailyPackageChanges;
10901 }
10902
Joe Onoratoe1acd632016-02-23 13:25:10 -080010903 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010904 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010905 }
10906
10907 @Override
10908 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010909 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010910 }
10911
10912 @Override
10913 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010914 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010915 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070010916
The Android Open Source Project10592532009-03-18 17:39:46 -070010917 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010918 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010919 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010920 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010921 }
10922 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010923
Evan Millar633a1742009-04-02 16:36:33 -070010924 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010925 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010926 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010927
The Android Open Source Project10592532009-03-18 17:39:46 -070010928 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010929 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010930 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010931 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010932 }
10933 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010934
Evan Millar633a1742009-04-02 16:36:33 -070010935 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010936 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010938
Amith Yamasanie43530a2009-08-21 13:11:37 -070010939 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010940 public int getLowDischargeAmountSinceCharge() {
10941 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010942 int val = mLowDischargeAmountSinceCharge;
10943 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10944 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
10945 }
10946 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010947 }
10948 }
10949
10950 @Override
10951 public int getHighDischargeAmountSinceCharge() {
10952 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010953 int val = mHighDischargeAmountSinceCharge;
10954 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10955 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
10956 }
10957 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010958 }
10959 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010960
10961 @Override
10962 public int getDischargeAmount(int which) {
10963 int dischargeAmount = which == STATS_SINCE_CHARGED
10964 ? getHighDischargeAmountSinceCharge()
10965 : (getDischargeStartLevel() - getDischargeCurrentLevel());
10966 if (dischargeAmount < 0) {
10967 dischargeAmount = 0;
10968 }
10969 return dischargeAmount;
10970 }
10971
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010972 public int getDischargeAmountScreenOn() {
10973 synchronized(this) {
10974 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010975 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010976 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10977 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10978 }
10979 return val;
10980 }
10981 }
10982
10983 public int getDischargeAmountScreenOnSinceCharge() {
10984 synchronized(this) {
10985 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010986 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010987 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10988 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10989 }
10990 return val;
10991 }
10992 }
10993
10994 public int getDischargeAmountScreenOff() {
10995 synchronized(this) {
10996 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010997 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010998 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10999 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11000 }
11001 return val;
11002 }
11003 }
11004
11005 public int getDischargeAmountScreenOffSinceCharge() {
11006 synchronized(this) {
11007 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070011008 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011009 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11010 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11011 }
11012 return val;
11013 }
11014 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011016 /**
11017 * Retrieve the statistics object for a particular uid, creating if needed.
11018 */
11019 public Uid getUidStatsLocked(int uid) {
11020 Uid u = mUidStats.get(uid);
11021 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080011022 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023 mUidStats.put(uid, u);
11024 }
11025 return u;
11026 }
11027
11028 /**
11029 * Remove the statistics object for a particular uid.
11030 */
11031 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070011032 mKernelUidCpuTimeReader.removeUid(uid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070011033 mKernelUidCpuFreqTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011034 mUidStats.remove(uid);
11035 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070011036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 /**
11038 * Retrieve the statistics object for a particular process, creating
11039 * if needed.
11040 */
11041 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011042 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011043 Uid u = getUidStatsLocked(uid);
11044 return u.getProcessStatsLocked(name);
11045 }
11046
11047 /**
11048 * Retrieve the statistics object for a particular process, creating
11049 * if needed.
11050 */
11051 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011052 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011053 Uid u = getUidStatsLocked(uid);
11054 return u.getPackageStatsLocked(pkg);
11055 }
11056
11057 /**
11058 * Retrieve the statistics object for a particular service, creating
11059 * if needed.
11060 */
11061 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011062 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063 Uid u = getUidStatsLocked(uid);
11064 return u.getServiceStatsLocked(pkg, name);
11065 }
11066
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011067 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011068 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011069 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011070 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011071 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011072
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011073 Parcel mPendingWrite = null;
11074 final ReentrantLock mWriteLock = new ReentrantLock();
11075
11076 public void writeAsyncLocked() {
11077 writeLocked(false);
11078 }
11079
11080 public void writeSyncLocked() {
11081 writeLocked(true);
11082 }
11083
11084 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011085 if (mFile == null) {
11086 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011087 return;
11088 }
11089
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011090 if (mShuttingDown) {
11091 return;
11092 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011093
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011094 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011095 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080011096 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011097
11098 if (mPendingWrite != null) {
11099 mPendingWrite.recycle();
11100 }
11101 mPendingWrite = out;
11102
11103 if (sync) {
11104 commitPendingDataToDisk();
11105 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011106 BackgroundThread.getHandler().post(new Runnable() {
11107 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011108 commitPendingDataToDisk();
11109 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011110 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011111 }
11112 }
11113
11114 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011115 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011116 synchronized (this) {
11117 next = mPendingWrite;
11118 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011119 if (next == null) {
11120 return;
11121 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011122 }
11123
Amith Yamasanid2450862017-02-07 15:58:24 -080011124 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011125 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011126 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011127 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070011129 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011130 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011131 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011133 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011134 mFile.rollback();
11135 } finally {
11136 next.recycle();
11137 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070011138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 }
11140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011141 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011142 if (mDailyFile != null) {
11143 readDailyStatsLocked();
11144 }
11145
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011146 if (mFile == null) {
11147 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011148 return;
11149 }
11150
11151 mUidStats.clear();
11152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011153 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011154 File file = mFile.chooseForRead();
11155 if (!file.exists()) {
11156 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011158 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011160 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 Parcel in = Parcel.obtain();
11162 in.unmarshall(raw, 0, raw.length);
11163 in.setDataPosition(0);
11164 stream.close();
11165
11166 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080011167 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011168 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011169 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011171
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011172 mEndPlatformVersion = Build.ID;
11173
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011174 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011175 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080011176 final long elapsedRealtime = mClocks.elapsedRealtime();
11177 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011178 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011179 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011180 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011181 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
11182 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011183 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011184
11185 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011186 }
11187
11188 public int describeContents() {
11189 return 0;
11190 }
11191
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011192 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070011193 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011194
11195 mHistoryBuffer.setDataSize(0);
11196 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011197 mHistoryTagPool.clear();
11198 mNextHistoryTagIdx = 0;
11199 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011200
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011201 int numTags = in.readInt();
11202 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080011203 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011204 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011205 if (str == null) {
11206 throw new ParcelFormatException("null history tag string");
11207 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011208 int uid = in.readInt();
11209 HistoryTag tag = new HistoryTag();
11210 tag.string = str;
11211 tag.uid = uid;
11212 tag.poolIdx = idx;
11213 mHistoryTagPool.put(tag, idx);
11214 if (idx >= mNextHistoryTagIdx) {
11215 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011216 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011217 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011218 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011219
11220 int bufSize = in.readInt();
11221 int curPos = in.dataPosition();
11222 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011223 throw new ParcelFormatException("File corrupt: history data buffer too large " +
11224 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011225 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011226 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
11227 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011228 } else {
11229 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
11230 + " bytes at " + curPos);
11231 mHistoryBuffer.appendFrom(in, curPos, bufSize);
11232 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011233 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011234
Dianne Hackbornae384452011-06-28 12:33:48 -070011235 if (andOldHistory) {
11236 readOldHistory(in);
11237 }
11238
11239 if (DEBUG_HISTORY) {
11240 StringBuilder sb = new StringBuilder(128);
11241 sb.append("****************** OLD mHistoryBaseTime: ");
11242 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11243 Slog.i(TAG, sb.toString());
11244 }
11245 mHistoryBaseTime = historyBaseTime;
11246 if (DEBUG_HISTORY) {
11247 StringBuilder sb = new StringBuilder(128);
11248 sb.append("****************** NEW mHistoryBaseTime: ");
11249 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11250 Slog.i(TAG, sb.toString());
11251 }
11252
11253 // We are just arbitrarily going to insert 1 minute from the sample of
11254 // the last run until samples in this run.
11255 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080011256 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011257 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070011258 if (DEBUG_HISTORY) {
11259 StringBuilder sb = new StringBuilder(128);
11260 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
11261 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11262 Slog.i(TAG, sb.toString());
11263 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070011264 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011265 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011266
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011267 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011268 if (!USE_OLD_HISTORY) {
11269 return;
11270 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011271 mHistory = mHistoryEnd = mHistoryCache = null;
11272 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070011273 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011274 HistoryItem rec = new HistoryItem(time, in);
11275 addHistoryRecordLocked(rec);
11276 }
11277 }
11278
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011279 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070011280 if (DEBUG_HISTORY) {
11281 StringBuilder sb = new StringBuilder(128);
11282 sb.append("****************** WRITING mHistoryBaseTime: ");
11283 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070011284 sb.append(" mLastHistoryElapsedRealtime: ");
11285 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070011286 Slog.i(TAG, sb.toString());
11287 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011288 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011289 if (!inclData) {
11290 out.writeInt(0);
11291 out.writeInt(0);
11292 return;
11293 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011294 out.writeInt(mHistoryTagPool.size());
11295 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
11296 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080011297 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011298 out.writeString(tag.string);
11299 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080011300 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011301 out.writeInt(mHistoryBuffer.dataSize());
11302 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
11303 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
11304 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070011305
11306 if (andOldHistory) {
11307 writeOldHistory(out);
11308 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011309 }
11310
11311 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011312 if (!USE_OLD_HISTORY) {
11313 return;
11314 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011315 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011316 while (rec != null) {
11317 if (rec.time >= 0) rec.writeToParcel(out, 0);
11318 rec = rec.next;
11319 }
11320 out.writeLong(-1);
11321 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011322
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011323 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011324 final int version = in.readInt();
11325 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011326 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011327 + ", expected " + VERSION + "; erasing old stats");
11328 return;
11329 }
11330
Dianne Hackbornae384452011-06-28 12:33:48 -070011331 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011333 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011334 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011335 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011336 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011337 mStartPlatformVersion = in.readString();
11338 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011339 mOnBatteryTimeBase.readSummaryFromParcel(in);
11340 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011341 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011342 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011343 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011344 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011345 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070011346 mMinLearnedBatteryCapacity = in.readInt();
11347 mMaxLearnedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011348 mLowDischargeAmountSinceCharge = in.readInt();
11349 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011350 mDischargeAmountScreenOnSinceCharge = in.readInt();
11351 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011352 mDischargeStepTracker.readFromParcel(in);
11353 mChargeStepTracker.readFromParcel(in);
11354 mDailyDischargeStepTracker.readFromParcel(in);
11355 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011356 mDischargeCounter.readSummaryFromParcelLocked(in);
11357 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011358 int NPKG = in.readInt();
11359 if (NPKG > 0) {
11360 mDailyPackageChanges = new ArrayList<>(NPKG);
11361 while (NPKG > 0) {
11362 NPKG--;
11363 PackageChange pc = new PackageChange();
11364 pc.mPackageName = in.readString();
11365 pc.mUpdate = in.readInt() != 0;
11366 pc.mVersionCode = in.readInt();
11367 mDailyPackageChanges.add(pc);
11368 }
11369 } else {
11370 mDailyPackageChanges = null;
11371 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011372 mDailyStartTime = in.readLong();
11373 mNextMinDailyDeadline = in.readLong();
11374 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011377
Jeff Browne95c3cd2014-05-02 16:59:26 -070011378 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011379 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011380 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11381 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
11382 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011383 mInteractive = false;
11384 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011385 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011386 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011387 mLongestLightIdleTime = in.readLong();
11388 mLongestFullIdleTime = in.readLong();
11389 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
11390 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
11391 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011392 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080011394 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070011395 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11396 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011397 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011398 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11399 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
11400 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011401 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011402 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11403 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011404 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011405 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011406 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080011407 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011408 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011409 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
11410 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011411 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070011412 mWifiOn = false;
11413 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011414 mGlobalWifiRunning = false;
11415 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011416 for (int i=0; i<NUM_WIFI_STATES; i++) {
11417 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
11418 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011419 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11420 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
11421 }
11422 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11423 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11424 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011425 mWifiActivity.readSummaryFromParcel(in);
11426 mBluetoothActivity.readSummaryFromParcel(in);
11427 mModemActivity.readSummaryFromParcel(in);
11428 mHasWifiReporting = in.readInt() != 0;
11429 mHasBluetoothReporting = in.readInt() != 0;
11430 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070011431
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011432 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011433 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011434 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011435 mCameraOnNesting = 0;
11436 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011437 mBluetoothScanNesting = 0;
11438 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439
Evan Millarc64edde2009-04-18 12:26:32 -070011440 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011441 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011442 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011443 }
Evan Millarc64edde2009-04-18 12:26:32 -070011444 for (int ikw = 0; ikw < NKW; ikw++) {
11445 if (in.readInt() != 0) {
11446 String kwltName = in.readString();
11447 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
11448 }
11449 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011450
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011451 int NWR = in.readInt();
11452 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011453 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011454 }
11455 for (int iwr = 0; iwr < NWR; iwr++) {
11456 if (in.readInt() != 0) {
11457 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011458 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011459 }
11460 }
11461
James Carr3a226052016-07-01 14:49:52 -070011462 int NMS = in.readInt();
11463 for (int ims = 0; ims < NMS; ims++) {
11464 if (in.readInt() != 0) {
11465 long kmstName = in.readLong();
11466 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
11467 }
11468 }
11469
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011470 mCpuFreqs = in.createLongArray();
11471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011472 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011473 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011474 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011476 for (int iu = 0; iu < NU; iu++) {
11477 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011478 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479 mUidStats.put(uid, u);
11480
Bookatz867c0d72017-03-07 18:23:42 -080011481 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -070011482 u.mOnBatteryScreenOffBackgroundTimeBase.readSummaryFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -080011483
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011484 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011485 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011486 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011487 }
The Android Open Source Project10592532009-03-18 17:39:46 -070011488 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011489 if (in.readInt() != 0) {
11490 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
11491 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011492 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011493 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070011494 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011495 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011496 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
11497 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11498 if (in.readInt() != 0) {
11499 u.makeWifiBatchedScanBin(i, null);
11500 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
11501 }
11502 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011503 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011504 if (in.readInt() != 0) {
11505 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
11506 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011507 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011508 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011509 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011510 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011511 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11512 }
11513 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011514 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11515 }
11516 if (in.readInt() != 0) {
11517 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11518 }
11519 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011520 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
11521 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011522 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -070011523 u.createAggregatedPartialWakelockTimerLocked().readSummaryFromParcelLocked(in);
11524 }
11525 if (in.readInt() != 0) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011526 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
11527 }
Bookatz956f36bf2017-04-28 09:48:17 -070011528 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -070011529 u.createBluetoothUnoptimizedScanTimerLocked().readSummaryFromParcelLocked(in);
11530 }
11531 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -070011532 u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
11533 }
Bookatzb1f04f32017-05-19 13:57:32 -070011534 if (in.readInt() != 0) {
11535 u.createBluetoothScanResultBgCounterLocked().readSummaryFromParcelLocked(in);
11536 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080011537 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070011538 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11539 if (in.readInt() != 0) {
11540 u.makeProcessState(i, null);
11541 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
11542 }
11543 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011544 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011545 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011546 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011547
Dianne Hackborn617f8772009-03-31 15:04:46 -070011548 if (in.readInt() != 0) {
11549 if (u.mUserActivityCounters == null) {
11550 u.initUserActivityLocked();
11551 }
11552 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11553 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
11554 }
11555 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011556
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011557 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011558 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011559 u.initNetworkActivityLocked();
11560 }
11561 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011562 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11563 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011564 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011565 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
11566 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011567 }
11568
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011569 u.mUserCpuTime.readSummaryFromParcelLocked(in);
11570 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
11571
Adam Lesinski6832f392015-09-05 18:05:40 -070011572 if (in.readInt() != 0) {
11573 final int numClusters = in.readInt();
11574 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
11575 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011576 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011577
11578 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
11579 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070011580 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070011581 final int NSB = in.readInt();
11582 if (mPowerProfile != null &&
11583 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
11584 throw new ParcelFormatException("File corrupt: too many speed bins " +
11585 NSB);
11586 }
11587
Adam Lesinski6832f392015-09-05 18:05:40 -070011588 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
11589 for (int speed = 0; speed < NSB; speed++) {
11590 if (in.readInt() != 0) {
11591 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
11592 mOnBatteryTimeBase);
11593 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
11594 }
11595 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070011596 } else {
11597 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070011598 }
11599 }
11600 } else {
11601 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011602 }
11603
Sudheer Shanka59f5c002017-05-15 10:57:15 -070011604 u.mCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
11605 in, mOnBatteryTimeBase);
11606 u.mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
11607 in, mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011608
11609 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -070011610 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11611 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
11612 } else {
11613 u.mMobileRadioApWakeupCount = null;
11614 }
11615
11616 if (in.readInt() != 0) {
11617 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11618 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
11619 } else {
11620 u.mWifiRadioApWakeupCount = null;
11621 }
11622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011623 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011624 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011625 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011627 for (int iw = 0; iw < NW; iw++) {
11628 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011629 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011630 }
11631
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011632 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011633 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011634 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011635 }
11636 for (int is = 0; is < NS; is++) {
11637 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011638 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011639 }
11640
11641 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011642 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011643 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011644 }
11645 for (int ij = 0; ij < NJ; ij++) {
11646 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011647 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011648 }
11649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011651 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011652 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011654 for (int is = 0; is < NP; is++) {
11655 int seNumber = in.readInt();
11656 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080011657 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011658 }
11659 }
11660
11661 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011662 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011663 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011665 for (int ip = 0; ip < NP; ip++) {
11666 String procName = in.readString();
11667 Uid.Proc p = u.getProcessStatsLocked(procName);
11668 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011670 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011671 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011672 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
11673 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011674 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011675 }
11676
11677 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011678 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011679 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011681 for (int ip = 0; ip < NP; ip++) {
11682 String pkgName = in.readString();
11683 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011684 final int NWA = in.readInt();
11685 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011686 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011687 }
11688 p.mWakeupAlarms.clear();
11689 for (int iwa=0; iwa<NWA; iwa++) {
11690 String tag = in.readString();
11691 Counter c = new Counter(mOnBatteryTimeBase);
11692 c.readSummaryFromParcelLocked(in);
11693 p.mWakeupAlarms.put(tag, c);
11694 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011695 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011696 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011697 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011699 for (int is = 0; is < NS; is++) {
11700 String servName = in.readString();
11701 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
11702 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011703 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011704 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011705 }
11706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011707 }
11708 }
11709
11710 /**
11711 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
11712 * disk. This format does not allow a lossless round-trip.
11713 *
11714 * @param out the Parcel to be written to.
11715 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011716 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011717 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011718
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011719 // Pull the clock time. This may update the time and make a new history entry
11720 // if we had originally pulled a time before the RTC was set.
11721 long startClockTime = getStartClockTime();
11722
Joe Onoratoabded112016-02-08 16:49:39 -080011723 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
11724 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011725
11726 out.writeInt(VERSION);
11727
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011728 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011730 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011731 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011732 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011733 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011734 out.writeString(mStartPlatformVersion);
11735 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011736 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11737 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011738 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011739 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011740 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011741 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011742 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070011743 out.writeInt(mMinLearnedBatteryCapacity);
11744 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011745 out.writeInt(getLowDischargeAmountSinceCharge());
11746 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011747 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11748 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011749 mDischargeStepTracker.writeToParcel(out);
11750 mChargeStepTracker.writeToParcel(out);
11751 mDailyDischargeStepTracker.writeToParcel(out);
11752 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011753 mDischargeCounter.writeSummaryFromParcelLocked(out);
11754 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011755 if (mDailyPackageChanges != null) {
11756 final int NPKG = mDailyPackageChanges.size();
11757 out.writeInt(NPKG);
11758 for (int i=0; i<NPKG; i++) {
11759 PackageChange pc = mDailyPackageChanges.get(i);
11760 out.writeString(pc.mPackageName);
11761 out.writeInt(pc.mUpdate ? 1 : 0);
11762 out.writeInt(pc.mVersionCode);
11763 }
11764 } else {
11765 out.writeInt(0);
11766 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011767 out.writeLong(mDailyStartTime);
11768 out.writeLong(mNextMinDailyDeadline);
11769 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011770
11771 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011772 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011773 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011774 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011775 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011776 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011777 out.writeLong(mLongestLightIdleTime);
11778 out.writeLong(mLongestFullIdleTime);
11779 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11780 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11781 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011782 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011783 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011784 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011785 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011786 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011787 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011788 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011789 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011790 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011791 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011792 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11793 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011794 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011795 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11796 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011797 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011798 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11799 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011800 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11801 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011802 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011803 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011804 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011805 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11806 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11807 }
11808 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11809 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11810 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011811 mWifiActivity.writeSummaryToParcel(out);
11812 mBluetoothActivity.writeSummaryToParcel(out);
11813 mModemActivity.writeSummaryToParcel(out);
11814 out.writeInt(mHasWifiReporting ? 1 : 0);
11815 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11816 out.writeInt(mHasModemReporting ? 1 : 0);
11817
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011818 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011819 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011820 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011821 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011822
Evan Millarc64edde2009-04-18 12:26:32 -070011823 out.writeInt(mKernelWakelockStats.size());
11824 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11825 Timer kwlt = ent.getValue();
11826 if (kwlt != null) {
11827 out.writeInt(1);
11828 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011829 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11830 } else {
11831 out.writeInt(0);
11832 }
11833 }
11834
11835 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011836 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11837 SamplingTimer timer = ent.getValue();
11838 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011839 out.writeInt(1);
11840 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011841 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070011842 } else {
11843 out.writeInt(0);
11844 }
11845 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011846
James Carr3a226052016-07-01 14:49:52 -070011847 out.writeInt(mKernelMemoryStats.size());
11848 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11849 Timer kmt = mKernelMemoryStats.valueAt(i);
11850 if (kmt != null) {
11851 out.writeInt(1);
11852 out.writeLong(mKernelMemoryStats.keyAt(i));
11853 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11854 } else {
11855 out.writeInt(0);
11856 }
11857 }
11858
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011859 out.writeLongArray(mCpuFreqs);
11860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011861 final int NU = mUidStats.size();
11862 out.writeInt(NU);
11863 for (int iu = 0; iu < NU; iu++) {
11864 out.writeInt(mUidStats.keyAt(iu));
11865 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011866
Bookatz867c0d72017-03-07 18:23:42 -080011867 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatzc8c44962017-05-11 12:12:54 -070011868 u.mOnBatteryScreenOffBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatz867c0d72017-03-07 18:23:42 -080011869
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011870 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011871 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011872 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011873 } else {
11874 out.writeInt(0);
11875 }
11876 if (u.mFullWifiLockTimer != null) {
11877 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011878 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011879 } else {
11880 out.writeInt(0);
11881 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011882 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011883 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011884 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011885 } else {
11886 out.writeInt(0);
11887 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011888 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11889 if (u.mWifiBatchedScanTimer[i] != null) {
11890 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011891 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070011892 } else {
11893 out.writeInt(0);
11894 }
11895 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011896 if (u.mWifiMulticastTimer != null) {
11897 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011898 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011899 } else {
11900 out.writeInt(0);
11901 }
11902 if (u.mAudioTurnedOnTimer != null) {
11903 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011904 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011905 } else {
11906 out.writeInt(0);
11907 }
11908 if (u.mVideoTurnedOnTimer != null) {
11909 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011910 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011911 } else {
11912 out.writeInt(0);
11913 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011914 if (u.mFlashlightTurnedOnTimer != null) {
11915 out.writeInt(1);
11916 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11917 } else {
11918 out.writeInt(0);
11919 }
11920 if (u.mCameraTurnedOnTimer != null) {
11921 out.writeInt(1);
11922 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11923 } else {
11924 out.writeInt(0);
11925 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011926 if (u.mForegroundActivityTimer != null) {
11927 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011928 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011929 } else {
11930 out.writeInt(0);
11931 }
Bookatzc8c44962017-05-11 12:12:54 -070011932 if (u.mAggregatedPartialWakelockTimer != null) {
11933 out.writeInt(1);
11934 u.mAggregatedPartialWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11935 } else {
11936 out.writeInt(0);
11937 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011938 if (u.mBluetoothScanTimer != null) {
11939 out.writeInt(1);
11940 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11941 } else {
11942 out.writeInt(0);
11943 }
Bookatzb1f04f32017-05-19 13:57:32 -070011944 if (u.mBluetoothUnoptimizedScanTimer != null) {
11945 out.writeInt(1);
11946 u.mBluetoothUnoptimizedScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11947 } else {
11948 out.writeInt(0);
11949 }
Bookatz956f36bf2017-04-28 09:48:17 -070011950 if (u.mBluetoothScanResultCounter != null) {
11951 out.writeInt(1);
11952 u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
11953 } else {
11954 out.writeInt(0);
11955 }
Bookatzb1f04f32017-05-19 13:57:32 -070011956 if (u.mBluetoothScanResultBgCounter != null) {
11957 out.writeInt(1);
11958 u.mBluetoothScanResultBgCounter.writeSummaryFromParcelLocked(out);
11959 } else {
11960 out.writeInt(0);
11961 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070011962 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11963 if (u.mProcessStateTimer[i] != null) {
11964 out.writeInt(1);
11965 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11966 } else {
11967 out.writeInt(0);
11968 }
11969 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011970 if (u.mVibratorOnTimer != null) {
11971 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011972 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011973 } else {
11974 out.writeInt(0);
11975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976
Dianne Hackborn617f8772009-03-31 15:04:46 -070011977 if (u.mUserActivityCounters == null) {
11978 out.writeInt(0);
11979 } else {
11980 out.writeInt(1);
11981 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11982 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
11983 }
11984 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011985
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011986 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011987 out.writeInt(0);
11988 } else {
11989 out.writeInt(1);
11990 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011991 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11992 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011993 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011994 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
11995 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011996 }
11997
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011998 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
11999 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
12000
Adam Lesinski6832f392015-09-05 18:05:40 -070012001 if (u.mCpuClusterSpeed != null) {
12002 out.writeInt(1);
12003 out.writeInt(u.mCpuClusterSpeed.length);
12004 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
12005 if (cpuSpeeds != null) {
12006 out.writeInt(1);
12007 out.writeInt(cpuSpeeds.length);
12008 for (LongSamplingCounter c : cpuSpeeds) {
12009 if (c != null) {
12010 out.writeInt(1);
12011 c.writeSummaryFromParcelLocked(out);
12012 } else {
12013 out.writeInt(0);
12014 }
12015 }
12016 } else {
12017 out.writeInt(0);
12018 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012019 }
Adam Lesinski6832f392015-09-05 18:05:40 -070012020 } else {
12021 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012022 }
12023
Sudheer Shanka59f5c002017-05-15 10:57:15 -070012024 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mCpuFreqTimeMs);
12025 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012026
Adam Lesinski5f056f62016-07-14 16:56:08 -070012027 if (u.mMobileRadioApWakeupCount != null) {
12028 out.writeInt(1);
12029 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12030 } else {
12031 out.writeInt(0);
12032 }
12033
12034 if (u.mWifiRadioApWakeupCount != null) {
12035 out.writeInt(1);
12036 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12037 } else {
12038 out.writeInt(0);
12039 }
12040
Dianne Hackbornd953c532014-08-16 18:17:38 -070012041 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
12042 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012043 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012044 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012045 out.writeString(wakeStats.keyAt(iw));
12046 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012047 if (wl.mTimerFull != null) {
12048 out.writeInt(1);
12049 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12050 } else {
12051 out.writeInt(0);
12052 }
12053 if (wl.mTimerPartial != null) {
12054 out.writeInt(1);
12055 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12056 } else {
12057 out.writeInt(0);
12058 }
12059 if (wl.mTimerWindow != null) {
12060 out.writeInt(1);
12061 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12062 } else {
12063 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012064 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012065 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070012066 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012067 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070012068 } else {
12069 out.writeInt(0);
12070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012071 }
12072
Bookatz2bffb5b2017-04-13 11:59:33 -070012073 final ArrayMap<String, DualTimer> syncStats = u.mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012074 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012075 out.writeInt(NS);
12076 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012077 out.writeString(syncStats.keyAt(is));
12078 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012079 }
12080
Bookatzaa4594a2017-03-24 12:39:56 -070012081 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012082 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012083 out.writeInt(NJ);
12084 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012085 out.writeString(jobStats.keyAt(ij));
12086 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012087 }
12088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012089 int NSE = u.mSensorStats.size();
12090 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012091 for (int ise=0; ise<NSE; ise++) {
12092 out.writeInt(u.mSensorStats.keyAt(ise));
12093 Uid.Sensor se = u.mSensorStats.valueAt(ise);
12094 if (se.mTimer != null) {
12095 out.writeInt(1);
12096 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12097 } else {
12098 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012099 }
12100 }
12101
12102 int NP = u.mProcessStats.size();
12103 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012104 for (int ip=0; ip<NP; ip++) {
12105 out.writeString(u.mProcessStats.keyAt(ip));
12106 Uid.Proc ps = u.mProcessStats.valueAt(ip);
12107 out.writeLong(ps.mUserTime);
12108 out.writeLong(ps.mSystemTime);
12109 out.writeLong(ps.mForegroundTime);
12110 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012111 out.writeInt(ps.mNumCrashes);
12112 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012113 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012114 }
12115
12116 NP = u.mPackageStats.size();
12117 out.writeInt(NP);
12118 if (NP > 0) {
12119 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
12120 : u.mPackageStats.entrySet()) {
12121 out.writeString(ent.getKey());
12122 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012123 final int NWA = ps.mWakeupAlarms.size();
12124 out.writeInt(NWA);
12125 for (int iwa=0; iwa<NWA; iwa++) {
12126 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
12127 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
12128 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012129 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012130 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012131 for (int is=0; is<NS; is++) {
12132 out.writeString(ps.mServiceStats.keyAt(is));
12133 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
12134 long time = ss.getStartTimeToNowLocked(
12135 mOnBatteryTimeBase.getUptime(NOW_SYS));
12136 out.writeLong(time);
12137 out.writeInt(ss.mStarts);
12138 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012139 }
12140 }
12141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012142 }
12143 }
12144
12145 public void readFromParcel(Parcel in) {
12146 readFromParcelLocked(in);
12147 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012149 void readFromParcelLocked(Parcel in) {
12150 int magic = in.readInt();
12151 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012152 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012153 }
12154
Dianne Hackbornae384452011-06-28 12:33:48 -070012155 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012158 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012159 mStartPlatformVersion = in.readString();
12160 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012161 mUptime = in.readLong();
12162 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012163 mRealtime = in.readLong();
12164 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012165 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012166 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070012167 mMinLearnedBatteryCapacity = in.readInt();
12168 mMaxLearnedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012169 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012170 mOnBatteryTimeBase.readFromParcel(in);
12171 mOnBatteryScreenOffTimeBase.readFromParcel(in);
12172
Jeff Browne95c3cd2014-05-02 16:59:26 -070012173 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080012174 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012175 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012176 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
12177 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012178 }
Dianne Hackborn29325132014-05-21 15:01:03 -070012179 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012180 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012181 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012182 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
12183 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012184 mLongestLightIdleTime = in.readLong();
12185 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080012186 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
12187 mOnBatteryTimeBase, in);
12188 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
12189 mOnBatteryTimeBase, in);
12190 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
12191 mOnBatteryTimeBase, in);
12192 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
12193 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012194 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012195 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012196 null, mOnBatteryTimeBase, in);
12197 }
Joe Onoratoabded112016-02-08 16:49:39 -080012198 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
12199 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012200 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012201 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012202 null, mOnBatteryTimeBase, in);
12203 }
12204 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12205 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12206 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12207 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012208 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080012209 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
12210 mOnBatteryTimeBase, in);
12211 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
12212 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012213 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012214 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
12215 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012216 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012217 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012218 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012219 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012220 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
12221 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012222 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012223 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012224 null, mOnBatteryTimeBase, in);
12225 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012226 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012227 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012228 null, mOnBatteryTimeBase, in);
12229 }
12230 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012231 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012232 null, mOnBatteryTimeBase, in);
12233 }
Adam Lesinski33dac552015-03-09 15:24:48 -070012234
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012235 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12236 NUM_WIFI_TX_LEVELS, in);
12237 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12238 NUM_BT_TX_LEVELS, in);
12239 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12240 ModemActivityInfo.TX_POWER_LEVELS, in);
12241 mHasWifiReporting = in.readInt() != 0;
12242 mHasBluetoothReporting = in.readInt() != 0;
12243 mHasModemReporting = in.readInt() != 0;
12244
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012245 mNumConnectivityChange = in.readInt();
12246 mLoadedNumConnectivityChange = in.readInt();
12247 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012248 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012249 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012250 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012251 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012252 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012253 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012254 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012255 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012256 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012257 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012258 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012259 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070012260 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012261 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012262 mLowDischargeAmountSinceCharge = in.readInt();
12263 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012264 mDischargeAmountScreenOn = in.readInt();
12265 mDischargeAmountScreenOnSinceCharge = in.readInt();
12266 mDischargeAmountScreenOff = in.readInt();
12267 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012268 mDischargeStepTracker.readFromParcel(in);
12269 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012270 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
12271 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 mLastWriteTime = in.readLong();
12273
Evan Millarc64edde2009-04-18 12:26:32 -070012274 mKernelWakelockStats.clear();
12275 int NKW = in.readInt();
12276 for (int ikw = 0; ikw < NKW; ikw++) {
12277 if (in.readInt() != 0) {
12278 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012279 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070012280 mKernelWakelockStats.put(wakelockName, kwlt);
12281 }
12282 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012283
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012284 mWakeupReasonStats.clear();
12285 int NWR = in.readInt();
12286 for (int iwr = 0; iwr < NWR; iwr++) {
12287 if (in.readInt() != 0) {
12288 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012289 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012290 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012291 }
12292 }
12293
James Carr3a226052016-07-01 14:49:52 -070012294 mKernelMemoryStats.clear();
12295 int nmt = in.readInt();
12296 for (int imt = 0; imt < nmt; imt++) {
12297 if (in.readInt() != 0) {
12298 Long bucket = in.readLong();
12299 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
12300 mKernelMemoryStats.put(bucket, kmt);
12301 }
12302 }
12303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304 mPartialTimers.clear();
12305 mFullTimers.clear();
12306 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012307 mWifiRunningTimers.clear();
12308 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070012309 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070012310 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012311 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012312 mAudioTurnedOnTimers.clear();
12313 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012314 mFlashlightTurnedOnTimers.clear();
12315 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012316
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012317 mCpuFreqs = in.createLongArray();
12318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012319 int numUids = in.readInt();
12320 mUidStats.clear();
12321 for (int i = 0; i < numUids; i++) {
12322 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080012323 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012324 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012325 mUidStats.append(uid, u);
12326 }
12327 }
12328
12329 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012330 writeToParcelLocked(out, true, flags);
12331 }
12332
12333 public void writeToParcelWithoutUids(Parcel out, int flags) {
12334 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012335 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012336
12337 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012338 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012339 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012340 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012341
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012342 // Pull the clock time. This may update the time and make a new history entry
12343 // if we had originally pulled a time before the RTC was set.
12344 long startClockTime = getStartClockTime();
12345
Joe Onoratoabded112016-02-08 16:49:39 -080012346 final long uSecUptime = mClocks.uptimeMillis() * 1000;
12347 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012348 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
12349 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012351 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012352
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012353 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012355 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012356 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012357 out.writeString(mStartPlatformVersion);
12358 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012359 out.writeLong(mUptime);
12360 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012361 out.writeLong(mRealtime);
12362 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012363 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012364 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070012365 out.writeInt(mMinLearnedBatteryCapacity);
12366 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012367 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12368 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12369
12370 mScreenOnTimer.writeToParcel(out, uSecRealtime);
12371 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
12372 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
12373 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012374 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012375 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012376 out.writeLong(mLongestLightIdleTime);
12377 out.writeLong(mLongestFullIdleTime);
12378 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
12379 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
12380 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012381 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012382 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
12383 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
12384 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
12385 }
12386 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
12387 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
12388 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
12389 }
12390 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12391 mNetworkByteActivityCounters[i].writeToParcel(out);
12392 mNetworkPacketActivityCounters[i].writeToParcel(out);
12393 }
12394 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
12395 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012396 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012397 mMobileRadioActiveUnknownTime.writeToParcel(out);
12398 mMobileRadioActiveUnknownCount.writeToParcel(out);
12399 mWifiOnTimer.writeToParcel(out, uSecRealtime);
12400 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
12401 for (int i=0; i<NUM_WIFI_STATES; i++) {
12402 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
12403 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012404 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12405 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
12406 }
12407 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12408 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
12409 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012410 mWifiActivity.writeToParcel(out, 0);
12411 mBluetoothActivity.writeToParcel(out, 0);
12412 mModemActivity.writeToParcel(out, 0);
12413 out.writeInt(mHasWifiReporting ? 1 : 0);
12414 out.writeInt(mHasBluetoothReporting ? 1 : 0);
12415 out.writeInt(mHasModemReporting ? 1 : 0);
12416
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012417 out.writeInt(mNumConnectivityChange);
12418 out.writeInt(mLoadedNumConnectivityChange);
12419 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012420 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012421 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012422 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012423 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012424 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070012425 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012426 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012427 out.writeInt(mLowDischargeAmountSinceCharge);
12428 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012429 out.writeInt(mDischargeAmountScreenOn);
12430 out.writeInt(mDischargeAmountScreenOnSinceCharge);
12431 out.writeInt(mDischargeAmountScreenOff);
12432 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012433 mDischargeStepTracker.writeToParcel(out);
12434 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012435 mDischargeCounter.writeToParcel(out);
12436 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012437 out.writeLong(mLastWriteTime);
12438
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012439 if (inclUids) {
12440 out.writeInt(mKernelWakelockStats.size());
12441 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
12442 SamplingTimer kwlt = ent.getValue();
12443 if (kwlt != null) {
12444 out.writeInt(1);
12445 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012446 kwlt.writeToParcel(out, uSecRealtime);
12447 } else {
12448 out.writeInt(0);
12449 }
12450 }
12451 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012452 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
12453 SamplingTimer timer = ent.getValue();
12454 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012455 out.writeInt(1);
12456 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012457 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012458 } else {
12459 out.writeInt(0);
12460 }
Evan Millarc64edde2009-04-18 12:26:32 -070012461 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012462 } else {
12463 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070012464 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070012465
James Carr3a226052016-07-01 14:49:52 -070012466 out.writeInt(mKernelMemoryStats.size());
12467 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
12468 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
12469 if (kmt != null) {
12470 out.writeInt(1);
12471 out.writeLong(mKernelMemoryStats.keyAt(i));
12472 kmt.writeToParcel(out, uSecRealtime);
12473 } else {
12474 out.writeInt(0);
12475 }
12476 }
12477
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012478 out.writeLongArray(mCpuFreqs);
12479
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012480 if (inclUids) {
12481 int size = mUidStats.size();
12482 out.writeInt(size);
12483 for (int i = 0; i < size; i++) {
12484 out.writeInt(mUidStats.keyAt(i));
12485 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012486
Bookatz867c0d72017-03-07 18:23:42 -080012487 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012488 }
12489 } else {
12490 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012491 }
12492 }
12493
12494 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
12495 new Parcelable.Creator<BatteryStatsImpl>() {
12496 public BatteryStatsImpl createFromParcel(Parcel in) {
12497 return new BatteryStatsImpl(in);
12498 }
12499
12500 public BatteryStatsImpl[] newArray(int size) {
12501 return new BatteryStatsImpl[size];
12502 }
12503 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012504
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012505 public void prepareForDumpLocked() {
12506 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012507 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012508
12509 // Pull the clock time. This may update the time and make a new history entry
12510 // if we had originally pulled a time before the RTC was set.
12511 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012512 }
12513
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012514 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012515 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012516 pw.println("mOnBatteryTimeBase:");
12517 mOnBatteryTimeBase.dump(pw, " ");
12518 pw.println("mOnBatteryScreenOffTimeBase:");
12519 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012520 Printer pr = new PrintWriterPrinter(pw);
12521 pr.println("*** Screen timer:");
12522 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070012523 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012524 pr.println("*** Screen brightness #" + i + ":");
12525 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070012526 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012527 pr.println("*** Interactive timer:");
12528 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012529 pr.println("*** Power save mode timer:");
12530 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012531 pr.println("*** Device idle mode light timer:");
12532 mDeviceIdleModeLightTimer.logState(pr, " ");
12533 pr.println("*** Device idle mode full timer:");
12534 mDeviceIdleModeFullTimer.logState(pr, " ");
12535 pr.println("*** Device light idling timer:");
12536 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012537 pr.println("*** Device idling timer:");
12538 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012539 pr.println("*** Phone timer:");
12540 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080012541 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070012542 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012543 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012544 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070012545 pr.println("*** Signal scanning :");
12546 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012547 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012548 pr.println("*** Data connection type #" + i + ":");
12549 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012550 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012551 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080012552 pr.println("*** Mobile network active timer:");
12553 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012554 pr.println("*** Mobile network active adjusted timer:");
12555 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012556 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012557 pr.println("*** Wifi timer:");
12558 mWifiOnTimer.logState(pr, " ");
12559 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012560 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012561 for (int i=0; i<NUM_WIFI_STATES; i++) {
12562 pr.println("*** Wifi state #" + i + ":");
12563 mWifiStateTimer[i].logState(pr, " ");
12564 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012565 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12566 pr.println("*** Wifi suppl state #" + i + ":");
12567 mWifiSupplStateTimer[i].logState(pr, " ");
12568 }
12569 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12570 pr.println("*** Wifi signal strength #" + i + ":");
12571 mWifiSignalStrengthsTimer[i].logState(pr, " ");
12572 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012573 pr.println("*** Flashlight timer:");
12574 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012575 pr.println("*** Camera timer:");
12576 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012577 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012578 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012580}