blob: 4b582bfd07111fd7446c5a3c722136d0462a1185 [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();
174 }
175
176 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
177
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700178 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800179 public MyHandler(Looper looper) {
180 super(looper, null, true);
181 }
182
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700183 @Override
184 public void handleMessage(Message msg) {
185 BatteryCallback cb = mCallback;
186 switch (msg.what) {
187 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700188 synchronized (BatteryStatsImpl.this) {
Sudheer Shanka671985f2017-05-19 11:33:42 -0700189 updateCpuTimeLocked(false /* updateCpuFreqData */);
Adam Lesinski72478f02015-06-17 15:39:43 -0700190 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700191 if (cb != null) {
192 cb.batteryNeedsCpuUpdate();
193 }
194 break;
195 case MSG_REPORT_POWER_CHANGE:
196 if (cb != null) {
197 cb.batteryPowerChanged(msg.arg1 != 0);
198 }
199 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700200 case MSG_REPORT_CHARGING:
201 if (cb != null) {
202 final String action;
203 synchronized (BatteryStatsImpl.this) {
204 action = mCharging ? BatteryManager.ACTION_CHARGING
205 : BatteryManager.ACTION_DISCHARGING;
206 }
207 Intent intent = new Intent(action);
208 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
209 cb.batterySendBroadcast(intent);
210 }
211 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700212 }
213 }
214 }
215
Joe Onoratoabded112016-02-08 16:49:39 -0800216 public interface Clocks {
217 public long elapsedRealtime();
218 public long uptimeMillis();
219 }
220
221 public static class SystemClocks implements Clocks {
222 public long elapsedRealtime() {
223 return SystemClock.elapsedRealtime();
224 }
225
226 public long uptimeMillis() {
227 return SystemClock.uptimeMillis();
228 }
229 }
230
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700231 public interface ExternalStatsSync {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700232 int UPDATE_CPU = 0x01;
233 int UPDATE_WIFI = 0x02;
234 int UPDATE_RADIO = 0x04;
235 int UPDATE_BT = 0x08;
236 int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800237
238 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700239 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700240 }
241
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700242 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700243 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700244
245 private BatteryCallback mCallback;
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800248 * Mapping isolated uids to the actual owning app uid.
249 */
250 final SparseIntArray mIsolatedUids = new SparseIntArray();
251
252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * The statistics we have collected organized by uids.
254 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800255 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 // A set of pools of currently active timers. When a timer is queried, we will divide the
258 // elapsed time by the number of active timers to arrive at that timer's share of the time.
259 // In order to do this, we must refresh each timer whenever the number of active timers
260 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700261 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
262 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
263 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700264 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700265 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
266 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
267 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
268 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
269 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
270 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
271 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
272 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700273 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
274 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800275 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700277 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700278 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 // These are the objects that will want to do something when the device
281 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800282 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800283
284 // These are the objects that will want to do something when the device
285 // is unplugged from power *and* the screen is off.
286 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
287
288 // Set to true when we want to distribute CPU across wakelocks for the next
289 // CPU update, even if we aren't currently running wake locks.
290 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700291
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700292 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700293
Dianne Hackborn37de0982014-05-09 09:32:18 -0700294 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800295
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700296 long mHistoryBaseTime;
297 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700298 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700299 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700300
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700301 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
302 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700303 final Parcel mHistoryBuffer = Parcel.obtain();
304 final HistoryItem mHistoryLastWritten = new HistoryItem();
305 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700306 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700307 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700308 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800309 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800310 int[] mReadHistoryUids;
311 int mReadHistoryChars;
312 int mNextHistoryTagIdx = 0;
313 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700314 int mHistoryBufferLastPos = -1;
315 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700316 int mActiveHistoryStates = 0xffffffff;
317 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700318 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700319 long mTrackRunningHistoryElapsedRealtime = 0;
320 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700321
322 final HistoryItem mHistoryCur = new HistoryItem();
323
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700324 HistoryItem mHistory;
325 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700326 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700327 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700328
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800329 // Used by computeHistoryStepDetails
330 HistoryStepDetails mLastHistoryStepDetails = null;
331 byte mLastHistoryStepLevel = 0;
332 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
333 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
334 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700335
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800336 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700337 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800338 */
339 long mLastStepCpuUserTime;
340 long mCurStepCpuUserTime;
341 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700342 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800343 */
344 long mLastStepCpuSystemTime;
345 long mCurStepCpuSystemTime;
346 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700347 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800348 */
349 long mLastStepStatUserTime;
350 long mLastStepStatSystemTime;
351 long mLastStepStatIOWaitTime;
352 long mLastStepStatIrqTime;
353 long mLastStepStatSoftIrqTime;
354 long mLastStepStatIdleTime;
355 long mCurStepStatUserTime;
356 long mCurStepStatSystemTime;
357 long mCurStepStatIOWaitTime;
358 long mCurStepStatIrqTime;
359 long mCurStepStatSoftIrqTime;
360 long mCurStepStatIdleTime;
361
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700362 private HistoryItem mHistoryIterator;
363 private boolean mReadOverflow;
364 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 int mStartCount;
367
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800368 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700369 String mStartPlatformVersion;
370 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 long mUptime;
373 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 long mRealtime;
375 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700376
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800377 int mWakeLockNesting;
378 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700379 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700380 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800381
Jeff Browne95c3cd2014-05-02 16:59:26 -0700382 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700383 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700384
Dianne Hackborn617f8772009-03-31 15:04:46 -0700385 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700386 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700387
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800388 boolean mPretendScreenOff;
389
Jeff Browne95c3cd2014-05-02 16:59:26 -0700390 boolean mInteractive;
391 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700392
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700393 boolean mPowerSaveModeEnabled;
394 StopwatchTimer mPowerSaveModeEnabledTimer;
395
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700396 boolean mDeviceIdling;
397 StopwatchTimer mDeviceIdlingTimer;
398
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700399 boolean mDeviceLightIdling;
400 StopwatchTimer mDeviceLightIdlingTimer;
401
402 int mDeviceIdleMode;
403 long mLastIdleTimeStart;
404 long mLongestLightIdleTime;
405 long mLongestFullIdleTime;
406 StopwatchTimer mDeviceIdleModeLightTimer;
407 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700410 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700411
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700412 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700413 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700414
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700415 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700416 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700417
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700418 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700419 StopwatchTimer mFlashlightOnTimer;
420
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700421 int mCameraOnNesting;
422 StopwatchTimer mCameraOnTimer;
423
Dianne Hackborn627bba72009-03-24 22:32:56 -0700424 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800425 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700426 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800427 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700428
429 StopwatchTimer mPhoneSignalScanningTimer;
430
Dianne Hackborn627bba72009-03-24 22:32:56 -0700431 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700432 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700433 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700434
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800435 final LongSamplingCounter[] mNetworkByteActivityCounters =
436 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
437 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700438 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
439
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800440 /**
441 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
442 */
443 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700444
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800445 /**
446 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
447 */
448 ControllerActivityCounterImpl mBluetoothActivity;
449
450 /**
451 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
452 */
453 ControllerActivityCounterImpl mModemActivity;
454
455 /**
456 * Whether the device supports WiFi controller energy reporting. This is set to true on
457 * the first WiFi energy report. See {@link #mWifiActivity}.
458 */
459 boolean mHasWifiReporting = false;
460
461 /**
462 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
463 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
464 */
465 boolean mHasBluetoothReporting = false;
466
467 /**
468 * Whether the device supports Modem controller energy reporting. This is set to true on
469 * the first Modem energy report. See {@link #mModemActivity}.
470 */
471 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700472
The Android Open Source Project10592532009-03-18 17:39:46 -0700473 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700474 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700475
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700476 boolean mGlobalWifiRunning;
477 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700478
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800479 int mWifiState = -1;
480 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
481
Dianne Hackborn3251b902014-06-20 14:40:53 -0700482 int mWifiSupplState = -1;
483 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
484
485 int mWifiSignalStrengthBin = -1;
486 final StopwatchTimer[] mWifiSignalStrengthsTimer =
487 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
488
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800489 int mBluetoothScanNesting;
Bookatz867c0d72017-03-07 18:23:42 -0800490 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
491 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800492
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700493 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700494 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800495 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800496 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700497 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800498 LongSamplingCounter mMobileRadioActiveUnknownTime;
499 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800500
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700501 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 /**
504 * These provide time bases that discount the time the device is plugged
505 * in to power.
506 */
507 boolean mOnBattery;
508 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700509
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700510 /**
511 * External reporting of whether the device is actually charging.
512 */
513 boolean mCharging = true;
514 int mLastChargingStateLevel;
515
The Android Open Source Project10592532009-03-18 17:39:46 -0700516 /*
517 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
518 */
Evan Millar633a1742009-04-02 16:36:33 -0700519 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700520 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700521 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700522 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700523 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700524 int mLowDischargeAmountSinceCharge;
525 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800526 int mDischargeScreenOnUnplugLevel;
527 int mDischargeScreenOffUnplugLevel;
528 int mDischargeAmountScreenOn;
529 int mDischargeAmountScreenOnSinceCharge;
530 int mDischargeAmountScreenOff;
531 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700532
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700533 private LongSamplingCounter mDischargeScreenOffCounter;
534 private LongSamplingCounter mDischargeCounter;
535
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700536 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700537
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700538 int mInitStepMode = 0;
539 int mCurStepMode = 0;
540 int mModStepMode = 0;
541
Dianne Hackborn260c5022014-04-29 11:23:16 -0700542 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700543 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800544 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
545 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700546 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700547
548 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700549 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800550 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
551 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
552
553 static final int MAX_DAILY_ITEMS = 10;
554
555 long mDailyStartTime = 0;
556 long mNextMinDailyDeadline = 0;
557 long mNextMaxDailyDeadline = 0;
558
559 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700562
Amith Yamasanif37447b2009-10-08 18:28:01 -0700563 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800564 private int mPhoneServiceStateRaw = -1;
565 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700566
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800567 private int mNumConnectivityChange;
568 private int mLoadedNumConnectivityChange;
569 private int mUnpluggedNumConnectivityChange;
570
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700571 private int mEstimatedBatteryCapacity = -1;
572
Jocelyn Dangc627d102017-04-14 13:15:14 -0700573 private int mMinLearnedBatteryCapacity = -1;
574 private int mMaxLearnedBatteryCapacity = -1;
Adam Lesinski041d9172016-12-12 12:03:56 -0800575
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700576 private long[] mCpuFreqs;
577
Adam Lesinskie08af192015-03-25 16:42:59 -0700578 private PowerProfile mPowerProfile;
579
Evan Millarc64edde2009-04-18 12:26:32 -0700580 /*
581 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
582 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700583 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700584
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700585 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700586 return mKernelWakelockStats;
587 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700588
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700589 String mLastWakeupReason = null;
590 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700591 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700592
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700593 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700594 return mWakeupReasonStats;
595 }
596
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700597 @Override
598 public LongCounter getDischargeScreenOffCoulombCounter() {
599 return mDischargeScreenOffCounter;
600 }
601
602 @Override
603 public LongCounter getDischargeCoulombCounter() {
604 return mDischargeCounter;
605 }
606
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700607 @Override
608 public int getEstimatedBatteryCapacity() {
609 return mEstimatedBatteryCapacity;
610 }
611
Jocelyn Dangc627d102017-04-14 13:15:14 -0700612 @Override
613 public int getMinLearnedBatteryCapacity() {
614 return mMinLearnedBatteryCapacity;
615 }
616
617 @Override
618 public int getMaxLearnedBatteryCapacity() {
619 return mMaxLearnedBatteryCapacity;
620 }
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800623 this(new SystemClocks());
624 }
625
626 public BatteryStatsImpl(Clocks clocks) {
627 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700628 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700629 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800630 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700631 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700632 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700633 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700634 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636
Joe Onoratoabded112016-02-08 16:49:39 -0800637 private void init(Clocks clocks) {
638 mClocks = clocks;
Joe Onoratoabded112016-02-08 16:49:39 -0800639 }
640
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700641 public interface TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800642 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
643 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
644 }
645
Joe Onoratoabded112016-02-08 16:49:39 -0800646 // methods are protected not private to be VisibleForTesting
647 public static class TimeBase {
648 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800649
Joe Onoratoabded112016-02-08 16:49:39 -0800650 protected long mUptime;
651 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800652
Joe Onoratoabded112016-02-08 16:49:39 -0800653 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800654
Joe Onoratoabded112016-02-08 16:49:39 -0800655 protected long mPastUptime;
656 protected long mUptimeStart;
657 protected long mPastRealtime;
658 protected long mRealtimeStart;
659 protected long mUnpluggedUptime;
660 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800661
662 public void dump(PrintWriter pw, String prefix) {
663 StringBuilder sb = new StringBuilder(128);
664 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
665 sb.setLength(0);
666 sb.append(prefix);
667 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700668 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800669 pw.println(sb.toString());
670 sb.setLength(0);
671 sb.append(prefix);
672 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700673 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800674 pw.println(sb.toString());
675 sb.setLength(0);
676 sb.append(prefix);
677 sb.append("mPastUptime=");
678 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
679 formatTimeMs(sb, mUptimeStart / 1000);
680 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
681 pw.println(sb.toString());
682 sb.setLength(0);
683 sb.append(prefix);
684 sb.append("mPastRealtime=");
685 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
686 formatTimeMs(sb, mRealtimeStart / 1000);
687 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
688 pw.println(sb.toString());
689 }
690
691 public void add(TimeBaseObs observer) {
692 mObservers.add(observer);
693 }
694
695 public void remove(TimeBaseObs observer) {
696 if (!mObservers.remove(observer)) {
697 Slog.wtf(TAG, "Removed unknown observer: " + observer);
698 }
699 }
700
Joe Onoratoabded112016-02-08 16:49:39 -0800701 public boolean hasObserver(TimeBaseObs observer) {
702 return mObservers.contains(observer);
703 }
704
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800705 public void init(long uptime, long realtime) {
706 mRealtime = 0;
707 mUptime = 0;
708 mPastUptime = 0;
709 mPastRealtime = 0;
710 mUptimeStart = uptime;
711 mRealtimeStart = realtime;
712 mUnpluggedUptime = getUptime(mUptimeStart);
713 mUnpluggedRealtime = getRealtime(mRealtimeStart);
714 }
715
716 public void reset(long uptime, long realtime) {
717 if (!mRunning) {
718 mPastUptime = 0;
719 mPastRealtime = 0;
720 } else {
721 mUptimeStart = uptime;
722 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800723 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
724 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800725 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800726 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800727 mUnpluggedRealtime = getRealtime(realtime);
728 }
729 }
730
731 public long computeUptime(long curTime, int which) {
732 switch (which) {
733 case STATS_SINCE_CHARGED:
734 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800735 case STATS_CURRENT:
736 return getUptime(curTime);
737 case STATS_SINCE_UNPLUGGED:
738 return getUptime(curTime) - mUnpluggedUptime;
739 }
740 return 0;
741 }
742
743 public long computeRealtime(long curTime, int which) {
744 switch (which) {
745 case STATS_SINCE_CHARGED:
746 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800747 case STATS_CURRENT:
748 return getRealtime(curTime);
749 case STATS_SINCE_UNPLUGGED:
750 return getRealtime(curTime) - mUnpluggedRealtime;
751 }
752 return 0;
753 }
754
755 public long getUptime(long curTime) {
756 long time = mPastUptime;
757 if (mRunning) {
758 time += curTime - mUptimeStart;
759 }
760 return time;
761 }
762
763 public long getRealtime(long curTime) {
764 long time = mPastRealtime;
765 if (mRunning) {
766 time += curTime - mRealtimeStart;
767 }
768 return time;
769 }
770
771 public long getUptimeStart() {
772 return mUptimeStart;
773 }
774
775 public long getRealtimeStart() {
776 return mRealtimeStart;
777 }
778
779 public boolean isRunning() {
780 return mRunning;
781 }
782
783 public boolean setRunning(boolean running, long uptime, long realtime) {
784 if (mRunning != running) {
785 mRunning = running;
786 if (running) {
787 mUptimeStart = uptime;
788 mRealtimeStart = realtime;
789 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
790 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
791
792 for (int i = mObservers.size() - 1; i >= 0; i--) {
793 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
794 }
795 } else {
796 mPastUptime += uptime - mUptimeStart;
797 mPastRealtime += realtime - mRealtimeStart;
798
799 long batteryUptime = getUptime(uptime);
800 long batteryRealtime = getRealtime(realtime);
801
802 for (int i = mObservers.size() - 1; i >= 0; i--) {
803 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
804 }
805 }
806 return true;
807 }
808 return false;
809 }
810
811 public void readSummaryFromParcel(Parcel in) {
812 mUptime = in.readLong();
813 mRealtime = in.readLong();
814 }
815
816 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
817 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
818 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
819 }
820
821 public void readFromParcel(Parcel in) {
822 mRunning = false;
823 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800824 mPastUptime = in.readLong();
825 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700826 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800827 mPastRealtime = in.readLong();
828 mRealtimeStart = in.readLong();
829 mUnpluggedUptime = in.readLong();
830 mUnpluggedRealtime = in.readLong();
831 }
832
833 public void writeToParcel(Parcel out, long uptime, long realtime) {
834 final long runningUptime = getUptime(uptime);
835 final long runningRealtime = getRealtime(realtime);
836 out.writeLong(mUptime);
837 out.writeLong(runningUptime);
838 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700839 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800840 out.writeLong(runningRealtime);
841 out.writeLong(mRealtimeStart);
842 out.writeLong(mUnpluggedUptime);
843 out.writeLong(mUnpluggedRealtime);
844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700848 * State for keeping track of counting information.
849 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800850 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700851 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800852 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700853 int mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700854 int mUnpluggedCount;
855 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700856
Bookatz8c6f3c52017-05-24 12:00:17 -0700857 public Counter(TimeBase timeBase, Parcel in) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800858 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700859 mPluggedCount = in.readInt();
860 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700861 mLoadedCount = in.readInt();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700862 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800863 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700864 }
865
Bookatz8c6f3c52017-05-24 12:00:17 -0700866 public Counter(TimeBase timeBase) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800867 mTimeBase = timeBase;
868 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700869 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700870
Dianne Hackborn617f8772009-03-31 15:04:46 -0700871 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700872 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700873 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700874 out.writeInt(mUnpluggedCount);
875 }
876
Bookatz8c6f3c52017-05-24 12:00:17 -0700877 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800878 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700879 mUnpluggedCount = mPluggedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700880 }
881
Bookatz8c6f3c52017-05-24 12:00:17 -0700882 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800883 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700884 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700885 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700886
Dianne Hackborn617f8772009-03-31 15:04:46 -0700887 /**
888 * Writes a possibly null Counter to a Parcel.
889 *
890 * @param out the Parcel to be written to.
891 * @param counter a Counter, or null.
892 */
893 public static void writeCounterToParcel(Parcel out, Counter counter) {
894 if (counter == null) {
895 out.writeInt(0); // indicates null
896 return;
897 }
898 out.writeInt(1); // indicates non-null
899
900 counter.writeToParcel(out);
901 }
902
903 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700904 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700905 int val = mCount.get();
906 if (which == STATS_SINCE_UNPLUGGED) {
907 val -= mUnpluggedCount;
908 } else if (which != STATS_SINCE_CHARGED) {
909 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700910 }
911
912 return val;
913 }
914
915 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700916 pw.println(prefix + "mCount=" + mCount.get()
Bookatz8c6f3c52017-05-24 12:00:17 -0700917 + " mLoadedCount=" + mLoadedCount
Dianne Hackborn617f8772009-03-31 15:04:46 -0700918 + " mUnpluggedCount=" + mUnpluggedCount
919 + " mPluggedCount=" + mPluggedCount);
920 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700921
Bookatz8c6f3c52017-05-24 12:00:17 -0700922 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
923 public void stepAtomic() {
924 if (mTimeBase.isRunning()) {
925 mCount.incrementAndGet();
926 }
Dianne Hackborn617f8772009-03-31 15:04:46 -0700927 }
928
Bookatz4ebc0642017-05-11 12:21:19 -0700929 void addAtomic(int delta) {
Bookatz8c6f3c52017-05-24 12:00:17 -0700930 if (mTimeBase.isRunning()) {
931 mCount.addAndGet(delta);
932 }
Bookatz4ebc0642017-05-11 12:21:19 -0700933 }
934
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700935 /**
936 * Clear state of this counter.
937 */
938 void reset(boolean detachIfReset) {
939 mCount.set(0);
Bookatz8c6f3c52017-05-24 12:00:17 -0700940 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700941 if (detachIfReset) {
942 detach();
943 }
944 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700945
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700946 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800947 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700948 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700949
Bookatz8c6f3c52017-05-24 12:00:17 -0700950 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
951 public void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700952 int count = mCount.get();
953 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700954 }
955
Bookatz8c6f3c52017-05-24 12:00:17 -0700956 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
957 public void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700958 mLoadedCount = in.readInt();
959 mCount.set(mLoadedCount);
Christopher Tate4cee7252010-03-19 14:50:40 -0700960 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700961 }
962 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700963
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700964 @VisibleForTesting
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700965 public static class LongSamplingCounterArray extends LongCounterArray implements TimeBaseObs {
966 final TimeBase mTimeBase;
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700967 public long[] mCounts;
968 public long[] mLoadedCounts;
969 public long[] mUnpluggedCounts;
970 public long[] mPluggedCounts;
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700971
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700972 private LongSamplingCounterArray(TimeBase timeBase, Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700973 mTimeBase = timeBase;
974 mPluggedCounts = in.createLongArray();
975 mCounts = copyArray(mPluggedCounts, mCounts);
976 mLoadedCounts = in.createLongArray();
977 mUnpluggedCounts = in.createLongArray();
978 timeBase.add(this);
979 }
980
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700981 public LongSamplingCounterArray(TimeBase timeBase) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700982 mTimeBase = timeBase;
983 timeBase.add(this);
984 }
985
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700986 private void writeToParcel(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700987 out.writeLongArray(mCounts);
988 out.writeLongArray(mLoadedCounts);
989 out.writeLongArray(mUnpluggedCounts);
990 }
991
992 @Override
993 public void onTimeStarted(long elapsedRealTime, long baseUptime, long baseRealtime) {
994 mUnpluggedCounts = copyArray(mPluggedCounts, mUnpluggedCounts);
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700995 }
996
997 @Override
998 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
999 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1000 }
1001
1002 @Override
1003 public long[] getCountsLocked(int which) {
1004 long[] val = copyArray(mTimeBase.isRunning() ? mCounts : mPluggedCounts, null);
1005 if (which == STATS_SINCE_UNPLUGGED) {
1006 subtract(val, mUnpluggedCounts);
1007 } else if (which != STATS_SINCE_CHARGED) {
1008 subtract(val, mLoadedCounts);
1009 }
1010 return val;
1011 }
1012
1013 @Override
1014 public void logState(Printer pw, String prefix) {
1015 pw.println(prefix + "mCounts=" + Arrays.toString(mCounts)
1016 + " mLoadedCounts=" + Arrays.toString(mLoadedCounts)
1017 + " mUnpluggedCounts=" + Arrays.toString(mUnpluggedCounts)
1018 + " mPluggedCounts=" + Arrays.toString(mPluggedCounts));
1019 }
1020
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001021 public void addCountLocked(long[] counts) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001022 if (counts == null) {
1023 return;
1024 }
Bookatz8c6f3c52017-05-24 12:00:17 -07001025 if (mTimeBase.isRunning()) {
1026 if (mCounts == null) {
1027 mCounts = new long[counts.length];
1028 }
1029 for (int i = 0; i < counts.length; ++i) {
1030 mCounts[i] += counts[i];
1031 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001032 }
1033 }
1034
1035 /**
1036 * Clear state of this counter.
1037 */
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001038 public void reset(boolean detachIfReset) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001039 fillArray(mCounts, 0);
1040 fillArray(mLoadedCounts, 0);
1041 fillArray(mPluggedCounts, 0);
1042 fillArray(mUnpluggedCounts, 0);
1043 if (detachIfReset) {
1044 detach();
1045 }
1046 }
1047
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001048 public void detach() {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001049 mTimeBase.remove(this);
1050 }
1051
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001052 private void writeSummaryToParcelLocked(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001053 out.writeLongArray(mCounts);
1054 }
1055
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001056 private void readSummaryFromParcelLocked(Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001057 mCounts = in.createLongArray();
1058 mLoadedCounts = copyArray(mCounts, mLoadedCounts);
1059 mUnpluggedCounts = copyArray(mCounts, mUnpluggedCounts);
1060 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1061 }
1062
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001063 public static void writeToParcel(Parcel out, LongSamplingCounterArray counterArray) {
1064 if (counterArray != null) {
1065 out.writeInt(1);
1066 counterArray.writeToParcel(out);
1067 } else {
1068 out.writeInt(0);
1069 }
1070 }
1071
1072 public static LongSamplingCounterArray readFromParcel(Parcel in, TimeBase timeBase) {
1073 if (in.readInt() != 0) {
1074 return new LongSamplingCounterArray(timeBase, in);
1075 } else {
1076 return null;
1077 }
1078 }
1079
1080 public static void writeSummaryToParcelLocked(Parcel out,
1081 LongSamplingCounterArray counterArray) {
1082 if (counterArray != null) {
1083 out.writeInt(1);
1084 counterArray.writeSummaryToParcelLocked(out);
1085 } else {
1086 out.writeInt(0);
1087 }
1088 }
1089
1090 public static LongSamplingCounterArray readSummaryFromParcelLocked(Parcel in,
1091 TimeBase timeBase) {
1092 if (in.readInt() != 0) {
1093 final LongSamplingCounterArray counterArray
1094 = new LongSamplingCounterArray(timeBase);
1095 counterArray.readSummaryFromParcelLocked(in);
1096 return counterArray;
1097 } else {
1098 return null;
1099 }
1100 }
1101
Bookatz8c6f3c52017-05-24 12:00:17 -07001102 private static void fillArray(long[] a, long val) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001103 if (a != null) {
1104 Arrays.fill(a, val);
1105 }
1106 }
1107
Bookatz8c6f3c52017-05-24 12:00:17 -07001108 private static void subtract(@NonNull long[] val, long[] toSubtract) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001109 if (toSubtract == null) {
1110 return;
1111 }
1112 for (int i = 0; i < val.length; i++) {
1113 val[i] -= toSubtract[i];
1114 }
1115 }
1116
Bookatz8c6f3c52017-05-24 12:00:17 -07001117 private static long[] copyArray(long[] src, long[] dest) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001118 if (src == null) {
1119 return null;
1120 } else {
1121 if (dest == null) {
1122 dest = new long[src.length];
1123 }
1124 System.arraycopy(src, 0, dest, 0, src.length);
1125 return dest;
1126 }
1127 }
1128 }
1129
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001130 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001131 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001132 long mCount;
1133 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001134 long mUnpluggedCount;
1135 long mPluggedCount;
1136
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001137 LongSamplingCounter(TimeBase timeBase, Parcel in) {
1138 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001139 mPluggedCount = in.readLong();
1140 mCount = mPluggedCount;
1141 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001142 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001143 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001144 }
1145
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 LongSamplingCounter(TimeBase timeBase) {
1147 mTimeBase = timeBase;
1148 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001149 }
1150
1151 public void writeToParcel(Parcel out) {
1152 out.writeLong(mCount);
1153 out.writeLong(mLoadedCount);
1154 out.writeLong(mUnpluggedCount);
1155 }
1156
1157 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001158 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001159 mUnpluggedCount = mPluggedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001160 }
1161
1162 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001163 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001164 mPluggedCount = mCount;
1165 }
1166
1167 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001168 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -07001169 if (which == STATS_SINCE_UNPLUGGED) {
1170 val -= mUnpluggedCount;
1171 } else if (which != STATS_SINCE_CHARGED) {
1172 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001173 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001174 return val;
1175 }
1176
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001177 @Override
1178 public void logState(Printer pw, String prefix) {
1179 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001180 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001181 + " mUnpluggedCount=" + mUnpluggedCount
1182 + " mPluggedCount=" + mPluggedCount);
1183 }
1184
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001185 void addCountLocked(long count) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001186 if (mTimeBase.isRunning()) {
1187 mCount += count;
1188 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001189 }
1190
1191 /**
1192 * Clear state of this counter.
1193 */
1194 void reset(boolean detachIfReset) {
1195 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001196 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001197 if (detachIfReset) {
1198 detach();
1199 }
1200 }
1201
1202 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001203 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001204 }
1205
1206 void writeSummaryFromParcelLocked(Parcel out) {
1207 out.writeLong(mCount);
1208 }
1209
1210 void readSummaryFromParcelLocked(Parcel in) {
1211 mLoadedCount = in.readLong();
1212 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001213 mUnpluggedCount = mPluggedCount = mLoadedCount;
1214 }
1215 }
1216
Dianne Hackborn617f8772009-03-31 15:04:46 -07001217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 * State for keeping track of timing information.
1219 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001220 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001221 protected final Clocks mClocks;
1222 protected final int mType;
1223 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001224
Joe Onoratoabded112016-02-08 16:49:39 -08001225 protected int mCount;
1226 protected int mLoadedCount;
1227 protected int mLastCount;
1228 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 // Times are in microseconds for better accuracy when dividing by the
1231 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 /**
1234 * The total time we have accumulated since the start of the original
1235 * boot, to the last time something interesting happened in the
1236 * current run.
1237 */
Joe Onoratoabded112016-02-08 16:49:39 -08001238 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 /**
1241 * The total time we loaded for the previous runs. Subtract this from
1242 * mTotalTime to find the time for the current run of the system.
1243 */
Joe Onoratoabded112016-02-08 16:49:39 -08001244 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * The run time of the last run of the system, as loaded from the
1248 * saved data.
1249 */
Joe Onoratoabded112016-02-08 16:49:39 -08001250 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 /**
1253 * The value of mTotalTime when unplug() was last called. Subtract
1254 * this from mTotalTime to find the time since the last unplug from
1255 * power.
1256 */
Joe Onoratoabded112016-02-08 16:49:39 -08001257 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001258
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001259 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001260 * The total time this timer has been running until the latest mark has been set.
1261 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1262 */
Joe Onoratoabded112016-02-08 16:49:39 -08001263 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001264
1265 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001266 * Constructs from a parcel.
1267 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001268 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001269 * @param in
1270 */
Joe Onoratoabded112016-02-08 16:49:39 -08001271 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1272 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001274 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 mCount = in.readInt();
1277 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001278 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 mUnpluggedCount = in.readInt();
1280 mTotalTime = in.readLong();
1281 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001282 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001284 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001285 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001286 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
1288
Joe Onoratoabded112016-02-08 16:49:39 -08001289 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1290 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001292 mTimeBase = timeBase;
1293 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
Evan Millarc64edde2009-04-18 12:26:32 -07001295
1296 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001297
Evan Millarc64edde2009-04-18 12:26:32 -07001298 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001299
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001300 /**
1301 * Clear state of this timer. Returns true if the timer is inactive
1302 * so can be completely dropped.
1303 */
Joe Onoratoabded112016-02-08 16:49:39 -08001304 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001305 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001306 mCount = mLoadedCount = mLastCount = 0;
1307 if (detachIfReset) {
1308 detach();
1309 }
1310 return true;
1311 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001312
Joe Onoratoabded112016-02-08 16:49:39 -08001313 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001314 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001315 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001316
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001317 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001318 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1319 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001320 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001323 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001326 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
1328
Adam Lesinskie08af192015-03-25 16:42:59 -07001329 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001330 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001332 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 + " old mUnpluggedTime=" + mUnpluggedTime
1334 + " old mUnpluggedCount=" + mUnpluggedCount);
1335 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001336 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001337 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 if (DEBUG && mType < 0) {
1339 Log.v(TAG, "unplug #" + mType
1340 + ": new mUnpluggedTime=" + mUnpluggedTime
1341 + " new mUnpluggedCount=" + mUnpluggedCount);
1342 }
1343 }
1344
Adam Lesinskie08af192015-03-25 16:42:59 -07001345 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001346 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001347 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001348 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001349 + " old mTotalTime=" + mTotalTime);
1350 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001351 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001352 mCount = computeCurrentCountLocked();
1353 if (DEBUG && mType < 0) {
1354 Log.v(TAG, "plug #" + mType
1355 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
1357 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 /**
1360 * Writes a possibly null Timer to a Parcel.
1361 *
1362 * @param out the Parcel to be written to.
1363 * @param timer a Timer, or null.
1364 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001365 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 if (timer == null) {
1367 out.writeInt(0); // indicates null
1368 return;
1369 }
1370 out.writeInt(1); // indicates non-null
1371
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001372 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374
1375 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001376 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001377 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1378 if (which == STATS_SINCE_UNPLUGGED) {
1379 val -= mUnpluggedTime;
1380 } else if (which != STATS_SINCE_CHARGED) {
1381 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383
1384 return val;
1385 }
1386
1387 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001388 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001389 int val = computeCurrentCountLocked();
1390 if (which == STATS_SINCE_UNPLUGGED) {
1391 val -= mUnpluggedCount;
1392 } else if (which != STATS_SINCE_CHARGED) {
1393 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395
1396 return val;
1397 }
1398
Adam Lesinskie08af192015-03-25 16:42:59 -07001399 @Override
1400 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1401 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1402 return val - mTimeBeforeMark;
1403 }
1404
1405 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001406 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001407 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1409 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001410 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001412 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001414 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001415
1416
Joe Onoratoabded112016-02-08 16:49:39 -08001417 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001418 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1419 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001420 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001421 }
1422
Joe Onoratoabded112016-02-08 16:49:39 -08001423 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001424 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001425 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001426 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001427 mUnpluggedTime = mTotalTime;
1428 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001429 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001430 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001431
1432 // When reading the summary, we set the mark to be the latest information.
1433 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001434 }
1435 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001436
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001437 /**
1438 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1439 * method. The state of the timer according to its {@link TimeBase} will determine how much
1440 * of the value is recorded.
1441 *
1442 * If the value being recorded resets, {@link #endSample()} can be called in order to
1443 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1444 * between calls, the {@link #endSample()} is automatically called and the new value is
1445 * expected to increase monotonically from that point on.
1446 */
Joe Onoratoabded112016-02-08 16:49:39 -08001447 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001448
Evan Millarc64edde2009-04-18 12:26:32 -07001449 /**
1450 * The most recent reported count from /proc/wakelocks.
1451 */
1452 int mCurrentReportedCount;
1453
1454 /**
1455 * The reported count from /proc/wakelocks when unplug() was last
1456 * called.
1457 */
1458 int mUnpluggedReportedCount;
1459
1460 /**
1461 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001462 */
Evan Millarc64edde2009-04-18 12:26:32 -07001463 long mCurrentReportedTotalTime;
1464
1465
1466 /**
1467 * The reported total_time from /proc/wakelocks when unplug() was last
1468 * called.
1469 */
1470 long mUnpluggedReportedTotalTime;
1471
1472 /**
1473 * Whether we are currently in a discharge cycle.
1474 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001475 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001476
1477 /**
1478 * Whether we are currently recording reported values.
1479 */
1480 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001481
Evan Millarc64edde2009-04-18 12:26:32 -07001482 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001483 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001484 */
1485 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001486
Adam Lesinski98f0d462016-04-19 16:46:20 -07001487 @VisibleForTesting
1488 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001489 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001490 mCurrentReportedCount = in.readInt();
1491 mUnpluggedReportedCount = in.readInt();
1492 mCurrentReportedTotalTime = in.readLong();
1493 mUnpluggedReportedTotalTime = in.readLong();
1494 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001495 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001496 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001497
Adam Lesinski98f0d462016-04-19 16:46:20 -07001498 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001499 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001500 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001501 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001502 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001503 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001504
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001505 /**
1506 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1507 * be less than the values used for a previous invocation.
1508 */
1509 public void endSample() {
1510 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1511 mCount = computeCurrentCountLocked();
1512 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1513 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001514 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001515
Evan Millarc64edde2009-04-18 12:26:32 -07001516 public void setUpdateVersion(int version) {
1517 mUpdateVersion = version;
1518 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001519
Evan Millarc64edde2009-04-18 12:26:32 -07001520 public int getUpdateVersion() {
1521 return mUpdateVersion;
1522 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001523
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001524 /**
1525 * Updates the current recorded values. These are meant to be monotonically increasing
1526 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1527 *
1528 * If the values being recorded have been reset, the monotonically increasing requirement
1529 * will be broken. In this case, {@link #endSample()} is automatically called and
1530 * the total value of totalTime and count are recorded, starting a new monotonically
1531 * increasing sample.
1532 *
1533 * @param totalTime total time of sample in microseconds.
1534 * @param count total number of times the event being sampled occurred.
1535 */
1536 public void update(long totalTime, int count) {
1537 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001538 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001539 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001540 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001541 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001542
1543 mTrackingReportedValues = true;
1544
1545 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1546 endSample();
1547 }
1548
1549 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001550 mCurrentReportedCount = count;
1551 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001552
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001553 /**
1554 * Adds deltaTime and deltaCount to the current sample.
1555 *
1556 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1557 * @param deltaCount additional number of times the event being sampled occurred.
1558 */
1559 public void add(long deltaTime, int deltaCount) {
1560 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001561 }
1562
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001563 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001564 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1565 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001566 if (mTrackingReportedValues) {
1567 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1568 mUnpluggedReportedCount = mCurrentReportedCount;
1569 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001570 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001571 }
1572
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001573 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001574 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1575 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1576 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001577 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001578
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001579 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001580 public void logState(Printer pw, String prefix) {
1581 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001582 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001583 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1584 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1585 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1586 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001587
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001588 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001589 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001590 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001591 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1592 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001593
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001594 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001595 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001596 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001597 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1598 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001599
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001600 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001601 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1602 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001603 out.writeInt(mCurrentReportedCount);
1604 out.writeInt(mUnpluggedReportedCount);
1605 out.writeLong(mCurrentReportedTotalTime);
1606 out.writeLong(mUnpluggedReportedTotalTime);
1607 out.writeInt(mTrackingReportedValues ? 1 : 0);
1608 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001609
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001610 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001611 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001612 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001613 mTrackingReportedValues = false;
1614 mUnpluggedReportedTotalTime = 0;
1615 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001616 return true;
1617 }
Evan Millarc64edde2009-04-18 12:26:32 -07001618 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001619
Evan Millarc64edde2009-04-18 12:26:32 -07001620 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001621 * A timer that increments in batches. It does not run for durations, but just jumps
1622 * for a pre-determined amount.
1623 */
Joe Onoratoabded112016-02-08 16:49:39 -08001624 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001625 final Uid mUid;
1626
1627 /**
1628 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1629 */
1630 long mLastAddedTime;
1631
1632 /**
1633 * The last duration that we added to the timer. This is in microseconds.
1634 */
1635 long mLastAddedDuration;
1636
1637 /**
1638 * Whether we are currently in a discharge cycle.
1639 */
1640 boolean mInDischarge;
1641
Joe Onoratoabded112016-02-08 16:49:39 -08001642 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1643 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001644 mUid = uid;
1645 mLastAddedTime = in.readLong();
1646 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001647 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001648 }
1649
Joe Onoratoabded112016-02-08 16:49:39 -08001650 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1651 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001652 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001653 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001654 }
1655
1656 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001657 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1658 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001659 out.writeLong(mLastAddedTime);
1660 out.writeLong(mLastAddedDuration);
1661 }
1662
1663 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001664 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001665 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001666 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001667 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001668 }
1669
1670 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001671 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001672 recomputeLastDuration(elapsedRealtime, false);
1673 mInDischarge = true;
1674 // If we are still within the last added duration, then re-added whatever remains.
1675 if (mLastAddedTime == elapsedRealtime) {
1676 mTotalTime += mLastAddedDuration;
1677 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001678 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001679 }
1680
1681 @Override
1682 public void logState(Printer pw, String prefix) {
1683 super.logState(pw, prefix);
1684 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1685 + " mLastAddedDuration=" + mLastAddedDuration);
1686 }
1687
1688 private long computeOverage(long curTime) {
1689 if (mLastAddedTime > 0) {
1690 return mLastTime + mLastAddedDuration - curTime;
1691 }
1692 return 0;
1693 }
1694
1695 private void recomputeLastDuration(long curTime, boolean abort) {
1696 final long overage = computeOverage(curTime);
1697 if (overage > 0) {
1698 // Aborting before the duration ran out -- roll back the remaining
1699 // duration. Only do this if currently discharging; otherwise we didn't
1700 // actually add the time.
1701 if (mInDischarge) {
1702 mTotalTime -= overage;
1703 }
1704 if (abort) {
1705 mLastAddedTime = 0;
1706 } else {
1707 mLastAddedTime = curTime;
1708 mLastAddedDuration -= overage;
1709 }
1710 }
1711 }
1712
1713 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001714 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001715 recomputeLastDuration(now, true);
1716 mLastAddedTime = now;
1717 mLastAddedDuration = durationMillis * 1000;
1718 if (mInDischarge) {
1719 mTotalTime += mLastAddedDuration;
1720 mCount++;
1721 }
1722 }
1723
1724 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001725 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001726 recomputeLastDuration(now, true);
1727 }
1728
1729 @Override
1730 protected int computeCurrentCountLocked() {
1731 return mCount;
1732 }
1733
1734 @Override
1735 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001736 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001737 if (overage > 0) {
1738 return mTotalTime = overage;
1739 }
1740 return mTotalTime;
1741 }
1742
1743 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001744 public boolean reset(boolean detachIfReset) {
1745 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001746 recomputeLastDuration(now, true);
1747 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001748 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001749 return !stillActive;
1750 }
1751 }
1752
Joe Onorato92fd23f2016-07-25 11:18:42 -07001753
1754 /**
1755 * A StopwatchTimer that also tracks the total and max individual
1756 * time spent active according to the given timebase. Whereas
1757 * StopwatchTimer apportions the time amongst all in the pool,
1758 * the total and max durations are not apportioned.
1759 */
1760 public static class DurationTimer extends StopwatchTimer {
1761 /**
1762 * The time (in ms) that the timer was last acquired or the time base
1763 * last (re-)started. Increasing the nesting depth does not reset this time.
1764 *
1765 * -1 if the timer is currently not running or the time base is not running.
1766 *
1767 * If written to a parcel, the start time is reset, as is mNesting in the base class
1768 * StopwatchTimer.
1769 */
1770 long mStartTimeMs = -1;
1771
1772 /**
Bookatz867c0d72017-03-07 18:23:42 -08001773 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001774 */
1775 long mMaxDurationMs;
1776
1777 /**
Bookatz867c0d72017-03-07 18:23:42 -08001778 * The time (in ms) that that the timer has been active since most recent
1779 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001780 */
1781 long mCurrentDurationMs;
1782
Bookatz867c0d72017-03-07 18:23:42 -08001783 /**
1784 * The total time (in ms) that that the timer has been active since most recent reset()
1785 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1786 * (but not including the present currentDuration) since reset. Not pooled.
1787 */
1788 long mTotalDurationMs;
1789
Joe Onorato92fd23f2016-07-25 11:18:42 -07001790 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1791 TimeBase timeBase, Parcel in) {
1792 super(clocks, uid, type, timerPool, timeBase, in);
1793 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001794 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001795 }
1796
1797 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1798 TimeBase timeBase) {
1799 super(clocks, uid, type, timerPool, timeBase);
1800 }
1801
1802 @Override
1803 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1804 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001805 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001806 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001807 }
1808
1809 /**
1810 * Write the summary to the parcel.
1811 *
1812 * Since the time base is probably meaningless after we come back, reading
1813 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001814 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001815 */
1816 @Override
1817 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1818 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001819 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001820 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001821 }
1822
1823 /**
1824 * Read the summary parcel.
1825 *
1826 * Has the side effect of stopping the timer.
1827 */
1828 @Override
1829 public void readSummaryFromParcelLocked(Parcel in) {
1830 super.readSummaryFromParcelLocked(in);
1831 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001832 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001833 mStartTimeMs = -1;
1834 mCurrentDurationMs = 0;
1835 }
1836
1837 /**
1838 * The TimeBase time started (again).
1839 *
1840 * If the timer is also running, store the start time.
1841 */
1842 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1843 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1844 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001845 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001846 }
1847 }
1848
1849 /**
1850 * The TimeBase stopped running.
1851 *
1852 * If the timer is running, add the duration into mCurrentDurationMs.
1853 */
1854 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001855 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1856 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001857 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001858 // baseRealtimeUs has already been converted to the timebase's realtime.
1859 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001860 }
1861 mStartTimeMs = -1;
1862 }
1863
1864 @Override
1865 public void logState(Printer pw, String prefix) {
1866 super.logState(pw, prefix);
1867 }
1868
1869 @Override
1870 public void startRunningLocked(long elapsedRealtimeMs) {
1871 super.startRunningLocked(elapsedRealtimeMs);
1872 if (mNesting == 1 && mTimeBase.isRunning()) {
1873 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001874 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001875 }
1876 }
1877
1878 /**
1879 * Decrements the mNesting ref-count on this timer.
1880 *
1881 * If it actually stopped (mNesting went to 0), then possibly update
1882 * mMaxDuration if the current duration was the longest ever.
1883 */
1884 @Override
1885 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001886 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001887 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001888 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001889 if (durationMs > mMaxDurationMs) {
1890 mMaxDurationMs = durationMs;
1891 }
1892 mStartTimeMs = -1;
1893 mCurrentDurationMs = 0;
1894 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001895 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1896 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1897 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001898 }
1899
1900 @Override
1901 public boolean reset(boolean detachIfReset) {
1902 boolean result = super.reset(detachIfReset);
1903 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08001904 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001905 mCurrentDurationMs = 0;
1906 if (mNesting > 0) {
1907 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
1908 } else {
1909 mStartTimeMs = -1;
1910 }
1911 return result;
1912 }
1913
1914 /**
1915 * Returns the max duration that this timer has ever seen.
1916 *
1917 * Note that this time is NOT split between the timers in the timer group that
1918 * this timer is attached to. It is the TOTAL time.
1919 */
1920 @Override
1921 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
1922 if (mNesting > 0) {
1923 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1924 if (durationMs > mMaxDurationMs) {
1925 return durationMs;
1926 }
1927 }
1928 return mMaxDurationMs;
1929 }
1930
1931 /**
1932 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08001933 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001934 *
1935 * Note that this time is NOT split between the timers in the timer group that
1936 * this timer is attached to. It is the TOTAL time.
1937 */
1938 @Override
1939 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
1940 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08001941 if (mNesting > 0 && mTimeBase.isRunning()) {
1942 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
1943 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001944 }
1945 return durationMs;
1946 }
Bookatz867c0d72017-03-07 18:23:42 -08001947
1948 /**
1949 * Returns the total cumulative duration that this timer has been on since reset().
1950 * If mTimerPool == null, this should be the same
1951 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
1952 *
1953 * Note that this time is NOT split between the timers in the timer group that
1954 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
1955 * the result will not be equivalent to getTotalTimeLocked.
1956 */
1957 @Override
1958 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
1959 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
1960 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07001961 }
1962
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001963 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001964 * State for keeping track of timing information.
1965 */
Joe Onoratoabded112016-02-08 16:49:39 -08001966 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001967 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001968 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001969
Evan Millarc64edde2009-04-18 12:26:32 -07001970 int mNesting;
1971
Evan Millarc64edde2009-04-18 12:26:32 -07001972 /**
1973 * The last time at which we updated the timer. If mNesting is > 0,
1974 * subtract this from the current battery time to find the amount of
1975 * time we have been running since we last computed an update.
1976 */
1977 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001978
Evan Millarc64edde2009-04-18 12:26:32 -07001979 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001980 * The total time at which the timer was acquired, to determine if it
Bookatzceebafe2017-04-06 11:59:13 -07001981 * was actually held for an interesting duration. If time base was not running when timer
1982 * was acquired, will be -1.
Evan Millarc64edde2009-04-18 12:26:32 -07001983 */
Bookatzceebafe2017-04-06 11:59:13 -07001984 long mAcquireTime = -1;
Evan Millarc64edde2009-04-18 12:26:32 -07001985
Amith Yamasanif37447b2009-10-08 18:28:01 -07001986 long mTimeout;
1987
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001988 /**
1989 * For partial wake locks, keep track of whether we are in the list
1990 * to consume CPU cycles.
1991 */
1992 boolean mInList;
1993
Joe Onoratoabded112016-02-08 16:49:39 -08001994 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001995 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001996 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001997 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001998 mTimerPool = timerPool;
1999 mUpdateTime = in.readLong();
2000 }
2001
Joe Onoratoabded112016-02-08 16:49:39 -08002002 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002003 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08002004 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002005 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002006 mTimerPool = timerPool;
2007 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002008
Joe Onoratoabded112016-02-08 16:49:39 -08002009 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002010 mTimeout = timeout;
2011 }
2012
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002013 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2014 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07002015 out.writeLong(mUpdateTime);
2016 }
2017
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002018 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07002019 if (mNesting > 0) {
2020 if (DEBUG && mType < 0) {
2021 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
2022 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002023 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
2024 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07002025 if (DEBUG && mType < 0) {
2026 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
2027 }
2028 }
2029 }
2030
2031 public void logState(Printer pw, String prefix) {
2032 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002033 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 + " mAcquireTime=" + mAcquireTime);
2035 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002036
Joe Onoratoabded112016-02-08 16:49:39 -08002037 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002039 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002040 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 if (mTimerPool != null) {
2042 // Accumulate time to all currently active timers before adding
2043 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002044 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 // Add this timer to the active pool
2046 mTimerPool.add(this);
2047 }
Bookatzceebafe2017-04-06 11:59:13 -07002048 if (mTimeBase.isRunning()) {
2049 // Increment the count
2050 mCount++;
2051 mAcquireTime = mTotalTime;
2052 } else {
2053 mAcquireTime = -1;
2054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 if (DEBUG && mType < 0) {
2056 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
2057 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2058 + " mAcquireTime=" + mAcquireTime);
2059 }
2060 }
2061 }
2062
Joe Onoratoabded112016-02-08 16:49:39 -08002063 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07002064 return mNesting > 0;
2065 }
2066
Joe Onoratoabded112016-02-08 16:49:39 -08002067 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 // Ignore attempt to stop a timer that isn't running
2069 if (mNesting == 0) {
2070 return;
2071 }
2072 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002073 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 if (mTimerPool != null) {
2075 // Accumulate time to all active counters, scaled by the total
2076 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002077 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 // Remove this timer from the active pool
2079 mTimerPool.remove(this);
2080 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 mNesting = 1;
2082 mTotalTime = computeRunTimeLocked(batteryRealtime);
2083 mNesting = 0;
2084 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 if (DEBUG && mType < 0) {
2087 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
2088 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2089 + " mAcquireTime=" + mAcquireTime);
2090 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002091
Bookatzceebafe2017-04-06 11:59:13 -07002092 if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 // If there was no change in the time, then discard this
2094 // count. A somewhat cheezy strategy, but hey.
2095 mCount--;
2096 }
2097 }
2098 }
2099
Joe Onoratoabded112016-02-08 16:49:39 -08002100 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07002101 if (mNesting > 0) {
2102 mNesting = 1;
2103 stopRunningLocked(elapsedRealtimeMs);
2104 }
2105 }
2106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 // Update the total time for all other running Timers with the same type as this Timer
2108 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002109 private static long refreshTimersLocked(long batteryRealtime,
2110 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002111 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 final int N = pool.size();
2113 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07002114 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 long heldTime = batteryRealtime - t.mUpdateTime;
2116 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002117 final long myTime = heldTime / N;
2118 if (t == self) {
2119 selfTime = myTime;
2120 }
2121 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123 t.mUpdateTime = batteryRealtime;
2124 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002125 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127
Evan Millarc64edde2009-04-18 12:26:32 -07002128 @Override
2129 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002130 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
2131 curBatteryRealtime = mUpdateTime + mTimeout;
2132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 return mTotalTime + (mNesting > 0
2134 ? (curBatteryRealtime - mUpdateTime)
2135 / (mTimerPool != null ? mTimerPool.size() : 1)
2136 : 0);
2137 }
2138
Evan Millarc64edde2009-04-18 12:26:32 -07002139 @Override
2140 protected int computeCurrentCountLocked() {
2141 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 }
2143
Adam Lesinskie08af192015-03-25 16:42:59 -07002144 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002145 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002146 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002147 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002148 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08002149 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002150 }
Bookatzceebafe2017-04-06 11:59:13 -07002151 mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002152 return canDetach;
2153 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002154
Adam Lesinskie08af192015-03-25 16:42:59 -07002155 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002156 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002157 super.detach();
2158 if (mTimerPool != null) {
2159 mTimerPool.remove(this);
2160 }
2161 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002162
Adam Lesinskie08af192015-03-25 16:42:59 -07002163 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002164 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07002165 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 mNesting = 0;
2167 }
Adam Lesinskie08af192015-03-25 16:42:59 -07002168
2169 /**
2170 * Set the mark so that we can query later for the total time the timer has
2171 * accumulated since this point. The timer can be running or not.
2172 *
2173 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
2174 */
2175 public void setMark(long elapsedRealtimeMs) {
2176 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
2177 if (mNesting > 0) {
2178 // We are running.
2179 if (mTimerPool != null) {
2180 refreshTimersLocked(batteryRealtime, mTimerPool, this);
2181 } else {
2182 mTotalTime += batteryRealtime - mUpdateTime;
2183 mUpdateTime = batteryRealtime;
2184 }
2185 }
2186 mTimeBeforeMark = mTotalTime;
2187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002189
Bookatz867c0d72017-03-07 18:23:42 -08002190 /**
2191 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2192 * TimeBase is effectively a subset of the other.
2193 */
Bookatzaa4594a2017-03-24 12:39:56 -07002194 public static class DualTimer extends DurationTimer {
2195 // This class both is a DurationTimer and also holds a second DurationTimer.
2196 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2197 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2198 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002199 // mSubTimer typically tracks only part of the total time, such as background time, as
2200 // determined by a subTimeBase. It is NOT pooled.
2201 private final DurationTimer mSubTimer;
2202
2203 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002204 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2205 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002206 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002207 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002208 */
2209 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2210 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002211 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002212 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2213 }
2214
2215 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002216 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2217 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002218 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002219 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002220 */
2221 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2222 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002223 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002224 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2225 }
2226
Bookatz867c0d72017-03-07 18:23:42 -08002227 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002228 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002229 public DurationTimer getSubTimer() {
2230 return mSubTimer;
2231 }
2232
Bookatzaa4594a2017-03-24 12:39:56 -07002233 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002234 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002235 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002236 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2237 }
2238
Bookatzaa4594a2017-03-24 12:39:56 -07002239 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002240 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002241 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002242 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2243 }
2244
Bookatzaa4594a2017-03-24 12:39:56 -07002245 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002246 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002247 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002248 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2249 }
2250
Bookatzaa4594a2017-03-24 12:39:56 -07002251 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002252 public boolean reset(boolean detachIfReset) {
2253 boolean active = false;
Bookatz4a3eda92017-04-10 13:10:46 -07002254 // Do not detach the subTimer explicitly since that'll be done by DualTimer.detach().
2255 active |= !mSubTimer.reset(false);
Bookatzaa4594a2017-03-24 12:39:56 -07002256 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002257 return !active;
2258 }
2259
Bookatzaa4594a2017-03-24 12:39:56 -07002260 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002261 public void detach() {
Bookatz867c0d72017-03-07 18:23:42 -08002262 mSubTimer.detach();
Bookatz4a3eda92017-04-10 13:10:46 -07002263 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002264 }
2265
Bookatzaa4594a2017-03-24 12:39:56 -07002266 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002267 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002268 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002269 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2270 }
2271
Bookatzaa4594a2017-03-24 12:39:56 -07002272 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002273 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002274 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002275 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2276 }
2277
Bookatzaa4594a2017-03-24 12:39:56 -07002278 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002279 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002280 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002281 mSubTimer.readSummaryFromParcelLocked(in);
2282 }
2283 }
2284
2285
Dianne Hackbornd953c532014-08-16 18:17:38 -07002286 public abstract class OverflowArrayMap<T> {
2287 private static final String OVERFLOW_NAME = "*overflow*";
2288
Dianne Hackborn657153b2016-07-29 14:54:14 -07002289 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002290 final ArrayMap<String, T> mMap = new ArrayMap<>();
2291 T mCurOverflow;
2292 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002293 long mLastOverflowTime;
2294 long mLastOverflowFinishTime;
2295 long mLastClearTime;
2296 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002297
Dianne Hackborn657153b2016-07-29 14:54:14 -07002298 public OverflowArrayMap(int uid) {
2299 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002300 }
2301
2302 public ArrayMap<String, T> getMap() {
2303 return mMap;
2304 }
2305
2306 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002307 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002308 mMap.clear();
2309 mCurOverflow = null;
2310 mActiveOverflow = null;
2311 }
2312
2313 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002314 if (name == null) {
2315 name = "";
2316 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002317 mMap.put(name, obj);
2318 if (OVERFLOW_NAME.equals(name)) {
2319 mCurOverflow = obj;
2320 }
2321 }
2322
2323 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002324 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002325 if (mActiveOverflow != null) {
2326 if (mActiveOverflow.size() == 0) {
2327 mActiveOverflow = null;
2328 }
2329 }
2330 if (mActiveOverflow == null) {
2331 // There is no currently active overflow, so we should no longer have
2332 // an overflow entry.
2333 if (mMap.containsKey(OVERFLOW_NAME)) {
2334 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2335 + mMap.get(OVERFLOW_NAME));
2336 mMap.remove(OVERFLOW_NAME);
2337 }
2338 mCurOverflow = null;
2339 } else {
2340 // There is currently active overflow, so we should still have an overflow entry.
2341 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2342 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2343 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2344 }
2345 }
2346 }
2347
2348 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002349 if (name == null) {
2350 name = "";
2351 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002352 T obj = mMap.get(name);
2353 if (obj != null) {
2354 return obj;
2355 }
2356
2357 // No object exists for the given name, but do we currently have it
2358 // running as part of the overflow?
2359 if (mActiveOverflow != null) {
2360 MutableInt over = mActiveOverflow.get(name);
2361 if (over != null) {
2362 // We are already actively counting this name in the overflow object.
2363 obj = mCurOverflow;
2364 if (obj == null) {
2365 // Shouldn't be here, but we'll try to recover.
2366 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2367 obj = mCurOverflow = instantiateObject();
2368 mMap.put(OVERFLOW_NAME, obj);
2369 }
2370 over.value++;
2371 return obj;
2372 }
2373 }
2374
2375 // No object exists for given name nor in the overflow; we need to make
2376 // a new one.
2377 final int N = mMap.size();
2378 if (N >= MAX_WAKELOCKS_PER_UID) {
2379 // Went over the limit on number of objects to track; this one goes
2380 // in to the overflow.
2381 obj = mCurOverflow;
2382 if (obj == null) {
2383 // Need to start overflow now...
2384 obj = mCurOverflow = instantiateObject();
2385 mMap.put(OVERFLOW_NAME, obj);
2386 }
2387 if (mActiveOverflow == null) {
2388 mActiveOverflow = new ArrayMap<>();
2389 }
2390 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002391 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002392 return obj;
2393 }
2394
2395 // Normal case where we just need to make a new object.
2396 obj = instantiateObject();
2397 mMap.put(name, obj);
2398 return obj;
2399 }
2400
2401 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002402 if (name == null) {
2403 name = "";
2404 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002405 T obj = mMap.get(name);
2406 if (obj != null) {
2407 return obj;
2408 }
2409
2410 // No object exists for the given name, but do we currently have it
2411 // running as part of the overflow?
2412 if (mActiveOverflow != null) {
2413 MutableInt over = mActiveOverflow.get(name);
2414 if (over != null) {
2415 // We are already actively counting this name in the overflow object.
2416 obj = mCurOverflow;
2417 if (obj != null) {
2418 over.value--;
2419 if (over.value <= 0) {
2420 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002421 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002422 }
2423 return obj;
2424 }
2425 }
2426 }
2427
2428 // Huh, they are stopping an active operation but we can't find one!
2429 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002430 StringBuilder sb = new StringBuilder();
2431 sb.append("Unable to find object for ");
2432 sb.append(name);
2433 sb.append(" in uid ");
2434 sb.append(mUid);
2435 sb.append(" mapsize=");
2436 sb.append(mMap.size());
2437 sb.append(" activeoverflow=");
2438 sb.append(mActiveOverflow);
2439 sb.append(" curoverflow=");
2440 sb.append(mCurOverflow);
2441 long now = SystemClock.elapsedRealtime();
2442 if (mLastOverflowTime != 0) {
2443 sb.append(" lastOverflowTime=");
2444 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2445 }
2446 if (mLastOverflowFinishTime != 0) {
2447 sb.append(" lastOverflowFinishTime=");
2448 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2449 }
2450 if (mLastClearTime != 0) {
2451 sb.append(" lastClearTime=");
2452 TimeUtils.formatDuration(mLastClearTime-now, sb);
2453 }
2454 if (mLastCleanupTime != 0) {
2455 sb.append(" lastCleanupTime=");
2456 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2457 }
2458 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002459 return null;
2460 }
2461
2462 public abstract T instantiateObject();
2463 }
2464
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002465 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2466 implements Parcelable {
2467 private final LongSamplingCounter mIdleTimeMillis;
2468 private final LongSamplingCounter mRxTimeMillis;
2469 private final LongSamplingCounter[] mTxTimeMillis;
2470 private final LongSamplingCounter mPowerDrainMaMs;
2471
2472 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2473 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2474 mRxTimeMillis = new LongSamplingCounter(timeBase);
2475 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2476 for (int i = 0; i < numTxStates; i++) {
2477 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2478 }
2479 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2480 }
2481
2482 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2483 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2484 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2485 final int recordedTxStates = in.readInt();
2486 if (recordedTxStates != numTxStates) {
2487 throw new ParcelFormatException("inconsistent tx state lengths");
2488 }
2489
2490 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2491 for (int i = 0; i < numTxStates; i++) {
2492 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2493 }
2494 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2495 }
2496
2497 public void readSummaryFromParcel(Parcel in) {
2498 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2499 mRxTimeMillis.readSummaryFromParcelLocked(in);
2500 final int recordedTxStates = in.readInt();
2501 if (recordedTxStates != mTxTimeMillis.length) {
2502 throw new ParcelFormatException("inconsistent tx state lengths");
2503 }
2504 for (LongSamplingCounter counter : mTxTimeMillis) {
2505 counter.readSummaryFromParcelLocked(in);
2506 }
2507 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2508 }
2509
2510 @Override
2511 public int describeContents() {
2512 return 0;
2513 }
2514
2515 public void writeSummaryToParcel(Parcel dest) {
2516 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2517 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2518 dest.writeInt(mTxTimeMillis.length);
2519 for (LongSamplingCounter counter : mTxTimeMillis) {
2520 counter.writeSummaryFromParcelLocked(dest);
2521 }
2522 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2523 }
2524
2525 @Override
2526 public void writeToParcel(Parcel dest, int flags) {
2527 mIdleTimeMillis.writeToParcel(dest);
2528 mRxTimeMillis.writeToParcel(dest);
2529 dest.writeInt(mTxTimeMillis.length);
2530 for (LongSamplingCounter counter : mTxTimeMillis) {
2531 counter.writeToParcel(dest);
2532 }
2533 mPowerDrainMaMs.writeToParcel(dest);
2534 }
2535
2536 public void reset(boolean detachIfReset) {
2537 mIdleTimeMillis.reset(detachIfReset);
2538 mRxTimeMillis.reset(detachIfReset);
2539 for (LongSamplingCounter counter : mTxTimeMillis) {
2540 counter.reset(detachIfReset);
2541 }
2542 mPowerDrainMaMs.reset(detachIfReset);
2543 }
2544
2545 public void detach() {
2546 mIdleTimeMillis.detach();
2547 mRxTimeMillis.detach();
2548 for (LongSamplingCounter counter : mTxTimeMillis) {
2549 counter.detach();
2550 }
2551 mPowerDrainMaMs.detach();
2552 }
2553
2554 /**
2555 * @return a LongSamplingCounter, measuring time spent in the idle state in
2556 * milliseconds.
2557 */
2558 @Override
2559 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002560 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002561 }
2562
2563 /**
2564 * @return a LongSamplingCounter, measuring time spent in the receive state in
2565 * milliseconds.
2566 */
2567 @Override
2568 public LongSamplingCounter getRxTimeCounter() {
2569 return mRxTimeMillis;
2570 }
2571
2572 /**
2573 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2574 * milliseconds.
2575 */
2576 @Override
2577 public LongSamplingCounter[] getTxTimeCounters() {
2578 return mTxTimeMillis;
2579 }
2580
2581 /**
2582 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2583 */
2584 @Override
2585 public LongSamplingCounter getPowerCounter() {
2586 return mPowerDrainMaMs;
2587 }
2588 }
2589
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002590 /*
2591 * Get the wakeup reason counter, and create a new one if one
2592 * doesn't already exist.
2593 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002594 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2595 SamplingTimer timer = mWakeupReasonStats.get(name);
2596 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002597 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002598 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002599 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002600 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002601 }
2602
Evan Millarc64edde2009-04-18 12:26:32 -07002603 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002604 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002605 * doesn't already exist.
2606 */
2607 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2608 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2609 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002610 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002611 mKernelWakelockStats.put(name, kwlt);
2612 }
2613 return kwlt;
2614 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002615
James Carr3a226052016-07-01 14:49:52 -07002616 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2617 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2618 if (kmt == null) {
2619 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2620 mKernelMemoryStats.put(bucket, kmt);
2621 }
2622 return kmt;
2623 }
2624
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002625 private int writeHistoryTag(HistoryTag tag) {
2626 Integer idxObj = mHistoryTagPool.get(tag);
2627 int idx;
2628 if (idxObj != null) {
2629 idx = idxObj;
2630 } else {
2631 idx = mNextHistoryTagIdx;
2632 HistoryTag key = new HistoryTag();
2633 key.setTo(tag);
2634 tag.poolIdx = idx;
2635 mHistoryTagPool.put(key, idx);
2636 mNextHistoryTagIdx++;
2637 mNumHistoryTagChars += key.string.length() + 1;
2638 }
2639 return idx;
2640 }
2641
2642 private void readHistoryTag(int index, HistoryTag tag) {
2643 tag.string = mReadHistoryStrings[index];
2644 tag.uid = mReadHistoryUids[index];
2645 tag.poolIdx = index;
2646 }
2647
Adam Lesinski926969b2016-04-28 17:31:12 -07002648 /*
2649 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2650
2651 There is always the first token, which may contain the delta time, or an indicator of
2652 the length of the time (int or long) following this token.
2653
2654 First token: always present,
2655 31 23 15 7 0
2656 â–ˆ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â–ˆ
2657
2658 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2659 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2660 delta time.
2661 A: battery level changed and an int follows with battery data.
2662 B: state changed and an int follows with state change data.
2663 C: state2 has changed and an int follows with state2 change data.
2664 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2665 E: event data has changed and an event struct follows.
2666 F: battery charge in coulombs has changed and an int with the charge follows.
2667 G: state flag denoting that the mobile radio was active.
2668 H: state flag denoting that the wifi radio was active.
2669 I: state flag denoting that a wifi scan occurred.
2670 J: state flag denoting that a wifi full lock was held.
2671 K: state flag denoting that the gps was on.
2672 L: state flag denoting that a wakelock was held.
2673 M: state flag denoting that the cpu was running.
2674
2675 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2676 with the time delta.
2677
2678 Battery level int: if A in the first token is set,
2679 31 23 15 7 0
2680 â–ˆ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â–ˆ
2681
2682 D: indicates that extra history details follow.
2683 V: the battery voltage.
2684 T: the battery temperature.
2685 L: the battery level (out of 100).
2686
2687 State change int: if B in the first token is set,
2688 31 23 15 7 0
2689 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2690
2691 A: wifi multicast was on.
2692 B: battery was plugged in.
2693 C: screen was on.
2694 D: phone was scanning for signal.
2695 E: audio was on.
2696 F: a sensor was active.
2697
2698 State2 change int: if C in the first token is set,
2699 31 23 15 7 0
2700 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2701
2702 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2703 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2704 C: a bluetooth scan was active.
2705 D: the camera was active.
2706 E: bluetooth was on.
2707 F: a phone call was active.
2708 G: the device was charging.
2709 H: 2 bits indicating the device-idle (doze) state: off, light, full
2710 I: the flashlight was on.
2711 J: wifi was on.
2712 K: wifi was running.
2713 L: video was playing.
2714 M: power save mode was on.
2715
2716 Wakelock/wakereason struct: if D in the first token is set,
2717 TODO(adamlesinski): describe wakelock/wakereason struct.
2718
2719 Event struct: if E in the first token is set,
2720 TODO(adamlesinski): describe the event struct.
2721
2722 History step details struct: if D in the battery level int is set,
2723 TODO(adamlesinski): describe the history step details struct.
2724
2725 Battery charge int: if F in the first token is set, an int representing the battery charge
2726 in coulombs follows.
2727 */
2728
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002729 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002730 static final int DELTA_TIME_MASK = 0x7ffff;
2731 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2732 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2733 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002734 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002735 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002736 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002737 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002738 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002739 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002740 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002741 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002742 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002743 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002744 // Flag in delta int: contains the battery charge count in uAh.
2745 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002746 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002747 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002748
2749 // These are the pieces of battery state that are packed in to the upper bits of
2750 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002751 // in STATE_BATTERY_MASK.
2752 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002753 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2754 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2755 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2756 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2757 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2758 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002759
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002760 // We use the low bit of the battery state int to indicate that we have full details
2761 // from a battery level change.
2762 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2763
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002764 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002765 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002766 dest.writeInt(DELTA_TIME_ABS);
2767 cur.writeToParcel(dest, 0);
2768 return;
2769 }
2770
2771 final long deltaTime = cur.time - last.time;
2772 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2773 final int lastStateInt = buildStateInt(last);
2774
2775 int deltaTimeToken;
2776 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2777 deltaTimeToken = DELTA_TIME_LONG;
2778 } else if (deltaTime >= DELTA_TIME_ABS) {
2779 deltaTimeToken = DELTA_TIME_INT;
2780 } else {
2781 deltaTimeToken = (int)deltaTime;
2782 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002783 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002784 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2785 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2786 final boolean computeStepDetails = includeStepDetails != 0
2787 || mLastHistoryStepDetails == null;
2788 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002789 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2790 if (batteryLevelIntChanged) {
2791 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2792 }
2793 final int stateInt = buildStateInt(cur);
2794 final boolean stateIntChanged = stateInt != lastStateInt;
2795 if (stateIntChanged) {
2796 firstToken |= DELTA_STATE_FLAG;
2797 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002798 final boolean state2IntChanged = cur.states2 != last.states2;
2799 if (state2IntChanged) {
2800 firstToken |= DELTA_STATE2_FLAG;
2801 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002802 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002803 firstToken |= DELTA_WAKELOCK_FLAG;
2804 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002805 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2806 firstToken |= DELTA_EVENT_FLAG;
2807 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002808
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002809 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2810 if (batteryChargeChanged) {
2811 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002812 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002813 dest.writeInt(firstToken);
2814 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2815 + " deltaTime=" + deltaTime);
2816
2817 if (deltaTimeToken >= DELTA_TIME_INT) {
2818 if (deltaTimeToken == DELTA_TIME_INT) {
2819 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2820 dest.writeInt((int)deltaTime);
2821 } else {
2822 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2823 dest.writeLong(deltaTime);
2824 }
2825 }
2826 if (batteryLevelIntChanged) {
2827 dest.writeInt(batteryLevelInt);
2828 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2829 + Integer.toHexString(batteryLevelInt)
2830 + " batteryLevel=" + cur.batteryLevel
2831 + " batteryTemp=" + cur.batteryTemperature
2832 + " batteryVolt=" + (int)cur.batteryVoltage);
2833 }
2834 if (stateIntChanged) {
2835 dest.writeInt(stateInt);
2836 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2837 + Integer.toHexString(stateInt)
2838 + " batteryStatus=" + cur.batteryStatus
2839 + " batteryHealth=" + cur.batteryHealth
2840 + " batteryPlugType=" + cur.batteryPlugType
2841 + " states=0x" + Integer.toHexString(cur.states));
2842 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002843 if (state2IntChanged) {
2844 dest.writeInt(cur.states2);
2845 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2846 + Integer.toHexString(cur.states2));
2847 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002848 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2849 int wakeLockIndex;
2850 int wakeReasonIndex;
2851 if (cur.wakelockTag != null) {
2852 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2853 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2854 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2855 } else {
2856 wakeLockIndex = 0xffff;
2857 }
2858 if (cur.wakeReasonTag != null) {
2859 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2860 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2861 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2862 } else {
2863 wakeReasonIndex = 0xffff;
2864 }
2865 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002866 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002867 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002868 int index = writeHistoryTag(cur.eventTag);
2869 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002870 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002871 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2872 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2873 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002874 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002875 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002876 if (mPlatformIdleStateCallback != null) {
2877 mCurHistoryStepDetails.statPlatformIdleState =
2878 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2879 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2880 mCurHistoryStepDetails.statPlatformIdleState);
2881 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002882 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2883 if (includeStepDetails != 0) {
2884 mCurHistoryStepDetails.writeToParcel(dest);
2885 }
2886 cur.stepDetails = mCurHistoryStepDetails;
2887 mLastHistoryStepDetails = mCurHistoryStepDetails;
2888 } else {
2889 cur.stepDetails = null;
2890 }
2891 if (mLastHistoryStepLevel < cur.batteryLevel) {
2892 mLastHistoryStepDetails = null;
2893 }
2894 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002895
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002896 if (batteryChargeChanged) {
2897 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2898 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002899 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002900 }
2901
2902 private int buildBatteryLevelInt(HistoryItem h) {
2903 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002904 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2905 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2906 }
2907
2908 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2909 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2910 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2911 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002912 }
2913
2914 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002915 int plugType = 0;
2916 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2917 plugType = 1;
2918 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2919 plugType = 2;
2920 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2921 plugType = 3;
2922 }
2923 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2924 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2925 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002926 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002927 }
2928
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002929 private void computeHistoryStepDetails(final HistoryStepDetails out,
2930 final HistoryStepDetails last) {
2931 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2932
2933 // Perform a CPU update right after we do this collection, so we have started
2934 // collecting good data for the next step.
2935 requestImmediateCpuUpdate();
2936
2937 if (last == null) {
2938 // We are not generating a delta, so all we need to do is reset the stats
2939 // we will later be doing a delta from.
2940 final int NU = mUidStats.size();
2941 for (int i=0; i<NU; i++) {
2942 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2943 uid.mLastStepUserTime = uid.mCurStepUserTime;
2944 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2945 }
2946 mLastStepCpuUserTime = mCurStepCpuUserTime;
2947 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2948 mLastStepStatUserTime = mCurStepStatUserTime;
2949 mLastStepStatSystemTime = mCurStepStatSystemTime;
2950 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2951 mLastStepStatIrqTime = mCurStepStatIrqTime;
2952 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2953 mLastStepStatIdleTime = mCurStepStatIdleTime;
2954 tmp.clear();
2955 return;
2956 }
2957 if (DEBUG) {
2958 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2959 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2960 + " irq=" + mLastStepStatIrqTime + " sirq="
2961 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2962 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2963 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2964 + " irq=" + mCurStepStatIrqTime + " sirq="
2965 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2966 }
2967 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2968 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2969 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2970 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2971 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2972 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2973 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2974 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2975 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2976 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2977 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2978 final int NU = mUidStats.size();
2979 for (int i=0; i<NU; i++) {
2980 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2981 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2982 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2983 final int totalTime = totalUTime + totalSTime;
2984 uid.mLastStepUserTime = uid.mCurStepUserTime;
2985 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2986 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2987 continue;
2988 }
2989 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2990 out.appCpuUid3 = uid.mUid;
2991 out.appCpuUTime3 = totalUTime;
2992 out.appCpuSTime3 = totalSTime;
2993 } else {
2994 out.appCpuUid3 = out.appCpuUid2;
2995 out.appCpuUTime3 = out.appCpuUTime2;
2996 out.appCpuSTime3 = out.appCpuSTime2;
2997 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2998 out.appCpuUid2 = uid.mUid;
2999 out.appCpuUTime2 = totalUTime;
3000 out.appCpuSTime2 = totalSTime;
3001 } else {
3002 out.appCpuUid2 = out.appCpuUid1;
3003 out.appCpuUTime2 = out.appCpuUTime1;
3004 out.appCpuSTime2 = out.appCpuSTime1;
3005 out.appCpuUid1 = uid.mUid;
3006 out.appCpuUTime1 = totalUTime;
3007 out.appCpuSTime1 = totalSTime;
3008 }
3009 }
3010 }
3011 mLastStepCpuUserTime = mCurStepCpuUserTime;
3012 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3013 mLastStepStatUserTime = mCurStepStatUserTime;
3014 mLastStepStatSystemTime = mCurStepStatSystemTime;
3015 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3016 mLastStepStatIrqTime = mCurStepStatIrqTime;
3017 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3018 mLastStepStatIdleTime = mCurStepStatIdleTime;
3019 }
3020
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003021 public void readHistoryDelta(Parcel src, HistoryItem cur) {
3022 int firstToken = src.readInt();
3023 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003024 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003025 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003026 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
3027 + " deltaTimeToken=" + deltaTimeToken);
3028
3029 if (deltaTimeToken < DELTA_TIME_ABS) {
3030 cur.time += deltaTimeToken;
3031 } else if (deltaTimeToken == DELTA_TIME_ABS) {
3032 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003033 cur.numReadInts += 2;
3034 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003035 cur.readFromParcel(src);
3036 return;
3037 } else if (deltaTimeToken == DELTA_TIME_INT) {
3038 int delta = src.readInt();
3039 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003040 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003041 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3042 } else {
3043 long delta = src.readLong();
3044 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3045 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003046 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003047 }
3048
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003049 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003050 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003051 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08003052 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003053 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003054 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
3055 + Integer.toHexString(batteryLevelInt)
3056 + " batteryLevel=" + cur.batteryLevel
3057 + " batteryTemp=" + cur.batteryTemperature
3058 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003059 } else {
3060 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003061 }
3062
3063 if ((firstToken&DELTA_STATE_FLAG) != 0) {
3064 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003065 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003066 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
3067 & STATE_BATTERY_STATUS_MASK);
3068 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
3069 & STATE_BATTERY_HEALTH_MASK);
3070 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
3071 & STATE_BATTERY_PLUG_MASK);
3072 switch (cur.batteryPlugType) {
3073 case 1:
3074 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
3075 break;
3076 case 2:
3077 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
3078 break;
3079 case 3:
3080 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
3081 break;
3082 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003083 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003084 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
3085 + Integer.toHexString(stateInt)
3086 + " batteryStatus=" + cur.batteryStatus
3087 + " batteryHealth=" + cur.batteryHealth
3088 + " batteryPlugType=" + cur.batteryPlugType
3089 + " states=0x" + Integer.toHexString(cur.states));
3090 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07003091 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003092 }
3093
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003094 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
3095 cur.states2 = src.readInt();
3096 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
3097 + Integer.toHexString(cur.states2));
3098 }
3099
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003100 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003101 int indexes = src.readInt();
3102 int wakeLockIndex = indexes&0xffff;
3103 int wakeReasonIndex = (indexes>>16)&0xffff;
3104 if (wakeLockIndex != 0xffff) {
3105 cur.wakelockTag = cur.localWakelockTag;
3106 readHistoryTag(wakeLockIndex, cur.wakelockTag);
3107 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
3108 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
3109 } else {
3110 cur.wakelockTag = null;
3111 }
3112 if (wakeReasonIndex != 0xffff) {
3113 cur.wakeReasonTag = cur.localWakeReasonTag;
3114 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
3115 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
3116 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
3117 } else {
3118 cur.wakeReasonTag = null;
3119 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003120 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003121 } else {
3122 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003123 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003124 }
3125
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003126 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003127 cur.eventTag = cur.localEventTag;
3128 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003129 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003130 final int index = ((codeAndIndex>>16)&0xffff);
3131 readHistoryTag(index, cur.eventTag);
3132 cur.numReadInts += 1;
3133 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
3134 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3135 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003136 } else {
3137 cur.eventCode = HistoryItem.EVENT_NONE;
3138 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003139
3140 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
3141 cur.stepDetails = mReadHistoryStepDetails;
3142 cur.stepDetails.readFromParcel(src);
3143 } else {
3144 cur.stepDetails = null;
3145 }
Adam Lesinski926969b2016-04-28 17:31:12 -07003146
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003147 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
3148 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003149 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003150 }
3151
Dianne Hackbornfc064132014-06-02 12:42:12 -07003152 @Override
3153 public void commitCurrentHistoryBatchLocked() {
3154 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
3155 }
3156
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003157 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003158 if (!mHaveBatteryLevel || !mRecordingHistory) {
3159 return;
3160 }
3161
Dianne Hackborn40c87252014-03-19 16:55:40 -07003162 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003163 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
3164 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003165 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003166 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003167 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
3168 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003169 + Integer.toHexString(lastDiffStates) + " diff2="
3170 + Integer.toHexString(diffStates2) + " lastDiff2="
3171 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003172 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003173 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003174 && (diffStates2&lastDiffStates2) == 0
3175 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3176 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003177 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003178 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003179 || cur.eventCode == HistoryItem.EVENT_NONE)
3180 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3181 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3182 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3183 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3184 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3185 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003186 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003187 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003188 // as long as no bit has changed both between now and the last entry, as
3189 // well as the last entry and the one before it (so we capture any toggles).
3190 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003191 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3192 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3193 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003194 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003195 // If the last written history had a wakelock tag, we need to retain it.
3196 // Note that the condition above made sure that we aren't in a case where
3197 // both it and the current history item have a wakelock tag.
3198 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003199 cur.wakelockTag = cur.localWakelockTag;
3200 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003201 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003202 // If the last written history had a wake reason 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.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003206 cur.wakeReasonTag = cur.localWakeReasonTag;
3207 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003208 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003209 // If the last written history had an event, 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 an event.
3212 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003213 cur.eventCode = mHistoryLastWritten.eventCode;
3214 cur.eventTag = cur.localEventTag;
3215 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003216 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003217 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003218 }
3219
Adam Lesinski45489782016-12-15 23:45:17 -08003220 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003221 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003222 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3223 // Clients can't deal with history buffers this large. This only
3224 // really happens when the device is on charger and interacted with
3225 // for long periods of time, like in retail mode. Since the device is
3226 // most likely charged, when unplugged, stats would have reset anyways.
3227 // Reset the stats and mark that we overflowed.
3228 // b/32540341
3229 resetAllStatsLocked();
3230
3231 // Mark that we want to set *OVERFLOW* event and the RESET:START
3232 // events.
3233 recordResetDueToOverflow = true;
3234
3235 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003236 if (!mHistoryOverflow) {
3237 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003238 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3239 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003240 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003241 }
3242
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003243 // After overflow, we allow various bit-wise states to settle to 0.
3244 boolean writeAnyway = false;
3245 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3246 & mActiveHistoryStates;
3247 if (mHistoryLastWritten.states != curStates) {
3248 // mActiveHistoryStates keeps track of which bits in .states are now being
3249 // forced to 0.
3250 int old = mActiveHistoryStates;
3251 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3252 writeAnyway |= old != mActiveHistoryStates;
3253 }
3254 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3255 & mActiveHistoryStates2;
3256 if (mHistoryLastWritten.states2 != curStates2) {
3257 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3258 // forced to 0.
3259 int old = mActiveHistoryStates2;
3260 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3261 writeAnyway |= old != mActiveHistoryStates2;
3262 }
3263
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003264 // Once we've reached the maximum number of items, we only
3265 // record changes to the battery level and the most interesting states.
3266 // Once we've reached the maximum maximum number of items, we only
3267 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003268 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003269 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003270 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003271 & HistoryItem.MOST_INTERESTING_STATES) == 0
3272 || ((mHistoryLastWritten.states2^cur.states2)
3273 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003274 return;
3275 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003276
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003277 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003278 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003279 }
3280
Adam Lesinski45489782016-12-15 23:45:17 -08003281 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003282 // The history is currently empty; we need it to start with a time stamp.
3283 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003284 if (recordResetDueToOverflow) {
3285 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3286 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003287 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3288 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003289 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003290 }
3291
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003292 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3293 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003294 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003295 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003296 }
3297 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3298 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003299 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003300 mHistoryLastWritten.states &= mActiveHistoryStates;
3301 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003302 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003303 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003304 cur.wakelockTag = null;
3305 cur.wakeReasonTag = null;
3306 cur.eventCode = HistoryItem.EVENT_NONE;
3307 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003308 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3309 + " now " + mHistoryBuffer.dataPosition()
3310 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003311 }
3312
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003313 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003314 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003315
Dianne Hackborn40c87252014-03-19 16:55:40 -07003316 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003317 if (mTrackRunningHistoryElapsedRealtime != 0) {
3318 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3319 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3320 if (diffUptime < (diffElapsed-20)) {
3321 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3322 mHistoryAddTmp.setTo(mHistoryLastWritten);
3323 mHistoryAddTmp.wakelockTag = null;
3324 mHistoryAddTmp.wakeReasonTag = null;
3325 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3326 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3327 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3328 }
3329 }
3330 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3331 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3332 mTrackRunningHistoryUptime = uptimeMs;
3333 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3334 }
3335
3336 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3337 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003338
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003339 if (!USE_OLD_HISTORY) {
3340 return;
3341 }
3342
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003343 if (!mHaveBatteryLevel || !mRecordingHistory) {
3344 return;
3345 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003346
3347 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003348 // and no states have since the last recorded entry changed and
3349 // are now resetting back to their original value, then just collapse
3350 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003351 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003352 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003353 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3354 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003355 // If the current is the same as the one before, then we no
3356 // longer need the entry.
3357 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003358 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003359 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003360 mHistoryLastEnd.next = null;
3361 mHistoryEnd.next = mHistoryCache;
3362 mHistoryCache = mHistoryEnd;
3363 mHistoryEnd = mHistoryLastEnd;
3364 mHistoryLastEnd = null;
3365 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003366 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3367 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003368 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003369 }
3370 return;
3371 }
3372
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003373 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003374 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003375
3376 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3377 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003378 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003379 }
3380
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003381 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3382 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003383 // record changes to the battery level and the most interesting states.
3384 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003385 // record changes to the battery level.
3386 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003387 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003388 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003389 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003390 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003391 return;
3392 }
3393 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003394
Dianne Hackborn40c87252014-03-19 16:55:40 -07003395 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003396 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003397
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003398 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003399 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003400 mHistoryCur.eventCode = code;
3401 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3402 mHistoryCur.eventTag.string = name;
3403 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003404 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003405 }
3406
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003407 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003408 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003409 if (rec != null) {
3410 mHistoryCache = rec.next;
3411 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003412 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003413 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003414 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003415
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003416 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003417 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003418
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003419 void addHistoryRecordLocked(HistoryItem rec) {
3420 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003421 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003422 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003423 if (mHistoryEnd != null) {
3424 mHistoryEnd.next = rec;
3425 mHistoryEnd = rec;
3426 } else {
3427 mHistory = mHistoryEnd = rec;
3428 }
3429 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003430
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003431 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003432 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003433 if (USE_OLD_HISTORY) {
3434 if (mHistory != null) {
3435 mHistoryEnd.next = mHistoryCache;
3436 mHistoryCache = mHistory;
3437 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3438 }
3439 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003440 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003441
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003442 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003443 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003444 mTrackRunningHistoryElapsedRealtime = 0;
3445 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003446
3447 mHistoryBuffer.setDataSize(0);
3448 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003449 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003450 mHistoryLastLastWritten.clear();
3451 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003452 mHistoryTagPool.clear();
3453 mNextHistoryTagIdx = 0;
3454 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003455 mHistoryBufferLastPos = -1;
3456 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003457 mActiveHistoryStates = 0xffffffff;
3458 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003459 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003460
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003461 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
3462 long realtime) {
Bookatz867c0d72017-03-07 18:23:42 -08003463 boolean batteryStatusChanged = mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
3464
3465 if (batteryStatusChanged) {
Bookatzc8c44962017-05-11 12:12:54 -07003466 for (int i = 0; i < mUidStats.size(); i++) {
3467 mUidStats.valueAt(i).updateOnBatteryBgTimeBase(uptime, realtime);
Bookatz867c0d72017-03-07 18:23:42 -08003468 }
3469 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003470
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003471 boolean unpluggedScreenOff = unplugged && screenOff;
3472 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
3473 updateKernelWakelocksLocked();
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003474 updateBatteryPropertiesLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07003475 if (DEBUG_ENERGY_CPU) {
3476 Slog.d(TAG, "Updating cpu time because screen is now " +
3477 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003478 }
Sudheer Shanka671985f2017-05-19 11:33:42 -07003479 updateCpuTimeLocked(true /* updateCpuFreqData */);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003480 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
Bookatzc8c44962017-05-11 12:12:54 -07003481 for (int i = 0; i < mUidStats.size(); i++) {
3482 mUidStats.valueAt(i).updateOnBatteryScreenOffBgTimeBase(uptime, realtime);
3483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 }
3485 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003486
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003487 private void updateBatteryPropertiesLocked() {
3488 try {
3489 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3490 ServiceManager.getService("batteryproperties"));
3491 registrar.scheduleUpdate();
3492 } catch (RemoteException e) {
3493 // Ignore.
3494 }
3495 }
3496
Dianne Hackborn099bc622014-01-22 13:39:16 -08003497 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3498 mIsolatedUids.put(isolatedUid, appUid);
3499 }
3500
Adam Lesinski61db88f2015-07-01 15:05:07 -07003501 /**
3502 * Schedules a read of the latest cpu times before removing the isolated UID.
3503 * @see #removeIsolatedUidLocked(int)
3504 */
3505 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003506 int curUid = mIsolatedUids.get(isolatedUid, -1);
3507 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003508 if (mExternalSync != null) {
3509 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3510 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003511 }
3512 }
3513
Adam Lesinski61db88f2015-07-01 15:05:07 -07003514 /**
3515 * This should only be called after the cpu times have been read.
3516 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3517 */
3518 public void removeIsolatedUidLocked(int isolatedUid) {
3519 mIsolatedUids.delete(isolatedUid);
3520 mKernelUidCpuTimeReader.removeUid(isolatedUid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -07003521 mKernelUidCpuFreqTimeReader.removeUid(isolatedUid);
Adam Lesinski61db88f2015-07-01 15:05:07 -07003522 }
3523
Dianne Hackborn099bc622014-01-22 13:39:16 -08003524 public int mapUid(int uid) {
3525 int isolated = mIsolatedUids.get(uid, -1);
3526 return isolated > 0 ? isolated : uid;
3527 }
3528
3529 public void noteEventLocked(int code, String name, int uid) {
3530 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003531 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3532 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003533 }
Joe Onoratoabded112016-02-08 16:49:39 -08003534 final long elapsedRealtime = mClocks.elapsedRealtime();
3535 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003536 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003537 }
3538
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003539 boolean ensureStartClockTime(final long currentTime) {
3540 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3541 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3542 // If the start clock time has changed by more than a year, then presumably
3543 // the previous time was completely bogus. So we are going to figure out a
3544 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003545 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003546 return true;
3547 }
3548 return false;
3549 }
3550
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003551 public void noteCurrentTimeChangedLocked() {
3552 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003553 final long elapsedRealtime = mClocks.elapsedRealtime();
3554 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003555 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003556 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003557 }
3558
Dianne Hackborn61659e52014-07-09 16:13:01 -07003559 public void noteProcessStartLocked(String name, int uid) {
3560 uid = mapUid(uid);
3561 if (isOnBattery()) {
3562 Uid u = getUidStatsLocked(uid);
3563 u.getProcessStatsLocked(name).incStartsLocked();
3564 }
3565 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3566 return;
3567 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003568 if (!mRecordAllHistory) {
3569 return;
3570 }
Joe Onoratoabded112016-02-08 16:49:39 -08003571 final long elapsedRealtime = mClocks.elapsedRealtime();
3572 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003573 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3574 }
3575
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003576 public void noteProcessCrashLocked(String name, int uid) {
3577 uid = mapUid(uid);
3578 if (isOnBattery()) {
3579 Uid u = getUidStatsLocked(uid);
3580 u.getProcessStatsLocked(name).incNumCrashesLocked();
3581 }
3582 }
3583
3584 public void noteProcessAnrLocked(String name, int uid) {
3585 uid = mapUid(uid);
3586 if (isOnBattery()) {
3587 Uid u = getUidStatsLocked(uid);
3588 u.getProcessStatsLocked(name).incNumAnrsLocked();
3589 }
3590 }
3591
Dianne Hackborna8d10942015-11-19 17:55:19 -08003592 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003593 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003594 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003595 }
3596
3597 public void noteProcessFinishLocked(String name, int uid) {
3598 uid = mapUid(uid);
3599 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3600 return;
3601 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003602 if (!mRecordAllHistory) {
3603 return;
3604 }
Joe Onoratoabded112016-02-08 16:49:39 -08003605 final long elapsedRealtime = mClocks.elapsedRealtime();
3606 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003607 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003608 }
3609
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003610 public void noteSyncStartLocked(String name, int uid) {
3611 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003612 final long elapsedRealtime = mClocks.elapsedRealtime();
3613 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003614 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3615 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3616 return;
3617 }
3618 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3619 }
3620
3621 public void noteSyncFinishLocked(String name, int uid) {
3622 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003623 final long elapsedRealtime = mClocks.elapsedRealtime();
3624 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003625 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3626 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3627 return;
3628 }
3629 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3630 }
3631
3632 public void noteJobStartLocked(String name, int uid) {
3633 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003634 final long elapsedRealtime = mClocks.elapsedRealtime();
3635 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003636 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3637 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3638 return;
3639 }
3640 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3641 }
3642
3643 public void noteJobFinishLocked(String name, int uid) {
3644 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003645 final long elapsedRealtime = mClocks.elapsedRealtime();
3646 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003647 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3648 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3649 return;
3650 }
3651 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3652 }
3653
Dianne Hackborn1e383822015-04-10 14:02:33 -07003654 public void noteAlarmStartLocked(String name, int uid) {
3655 if (!mRecordAllHistory) {
3656 return;
3657 }
3658 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003659 final long elapsedRealtime = mClocks.elapsedRealtime();
3660 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003661 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3662 return;
3663 }
3664 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3665 }
3666
3667 public void noteAlarmFinishLocked(String name, int uid) {
3668 if (!mRecordAllHistory) {
3669 return;
3670 }
3671 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003672 final long elapsedRealtime = mClocks.elapsedRealtime();
3673 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003674 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3675 return;
3676 }
3677 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3678 }
3679
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003680 private void requestWakelockCpuUpdate() {
3681 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3682 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3683 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3684 }
3685 }
3686
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003687 private void requestImmediateCpuUpdate() {
3688 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3689 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3690 }
3691
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003692 public void setRecordAllHistoryLocked(boolean enabled) {
3693 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003694 if (!enabled) {
3695 // Clear out any existing state.
3696 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003697 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003698 // Record the currently running processes as stopping, now that we are no
3699 // longer tracking them.
3700 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3701 HistoryItem.EVENT_PROC);
3702 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003703 long mSecRealtime = mClocks.elapsedRealtime();
3704 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003705 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3706 SparseIntArray uids = ent.getValue();
3707 for (int j=0; j<uids.size(); j++) {
3708 addHistoryEventLocked(mSecRealtime, mSecUptime,
3709 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3710 }
3711 }
3712 }
3713 } else {
3714 // Record the currently running processes as starting, now that we are tracking them.
3715 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3716 HistoryItem.EVENT_PROC);
3717 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003718 long mSecRealtime = mClocks.elapsedRealtime();
3719 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003720 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3721 SparseIntArray uids = ent.getValue();
3722 for (int j=0; j<uids.size(); j++) {
3723 addHistoryEventLocked(mSecRealtime, mSecUptime,
3724 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3725 }
3726 }
3727 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003728 }
3729 }
3730
Dianne Hackborn9a755432014-05-15 17:05:22 -07003731 public void setNoAutoReset(boolean enabled) {
3732 mNoAutoReset = enabled;
3733 }
3734
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003735 public void setPretendScreenOff(boolean pretendScreenOff) {
3736 mPretendScreenOff = pretendScreenOff;
3737 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3738 }
3739
Dianne Hackborn9a755432014-05-15 17:05:22 -07003740 private String mInitialAcquireWakeName;
3741 private int mInitialAcquireWakeUid = -1;
3742
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003743 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003744 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003745 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003746 if (type == WAKE_TYPE_PARTIAL) {
3747 // Only care about partial wake locks, since full wake locks
3748 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003749 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003750 if (historyName == null) {
3751 historyName = name;
3752 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003753 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003754 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3755 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003756 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003757 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003758 }
3759 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003760 if (mWakeLockNesting == 0) {
3761 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3762 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3763 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003764 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003765 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003766 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003767 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003768 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003769 } else if (!mWakeLockImportant && !unimportantForLogging
3770 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003771 if (mHistoryLastWritten.wakelockTag != null) {
3772 // We'll try to update the last tag.
3773 mHistoryLastWritten.wakelockTag = null;
3774 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003775 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003776 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003777 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003778 }
3779 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003780 }
3781 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003782 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003783 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003784 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3785 // We only update the cpu time when a wake lock is acquired if the screen is off.
3786 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3787 if (DEBUG_ENERGY_CPU) {
3788 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3789 }
3790 requestWakelockCpuUpdate();
3791 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003792 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003793 }
3794 }
3795
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003796 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3797 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003798 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003799 if (type == WAKE_TYPE_PARTIAL) {
3800 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003801 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003802 if (historyName == null) {
3803 historyName = name;
3804 }
3805 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3806 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003807 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003808 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003809 }
3810 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003811 if (mWakeLockNesting == 0) {
3812 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3813 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3814 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003815 mInitialAcquireWakeName = null;
3816 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003817 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003818 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003819 }
3820 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003821 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3822 if (DEBUG_ENERGY_CPU) {
3823 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3824 }
3825 requestWakelockCpuUpdate();
3826 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003827 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003828 }
3829 }
3830
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003831 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3832 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003833 final long elapsedRealtime = mClocks.elapsedRealtime();
3834 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003835 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003836 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003837 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003838 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003839 }
3840 }
3841
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003842 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3843 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003844 String newHistoryName, int newType, boolean newUnimportantForLogging) {
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 // For correct semantics, we start the need worksources first, so that we won't
3848 // make inappropriate history items as if all wake locks went away and new ones
3849 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003850 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003851 for (int i=0; i<NN; i++) {
3852 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003853 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003854 }
3855 final int NO = ws.size();
3856 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003857 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003858 }
3859 }
3860
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003861 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3862 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003863 final long elapsedRealtime = mClocks.elapsedRealtime();
3864 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003865 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003866 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003867 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003868 }
3869 }
3870
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003871 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3872 uid = mapUid(uid);
3873 final long elapsedRealtime = mClocks.elapsedRealtime();
3874 final long uptime = mClocks.uptimeMillis();
3875 if (historyName == null) {
3876 historyName = name;
3877 }
3878 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3879 0)) {
3880 return;
3881 }
3882 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3883 historyName, uid);
3884 }
3885
3886 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3887 uid = mapUid(uid);
3888 final long elapsedRealtime = mClocks.elapsedRealtime();
3889 final long uptime = mClocks.uptimeMillis();
3890 if (historyName == null) {
3891 historyName = name;
3892 }
3893 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3894 0)) {
3895 return;
3896 }
3897 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3898 historyName, uid);
3899 }
3900
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003901 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3902 if (mLastWakeupReason != null) {
3903 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003904 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003905 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003906 mLastWakeupReason = null;
3907 }
3908 }
3909
3910 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003911 final long elapsedRealtime = mClocks.elapsedRealtime();
3912 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003913 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003914 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003915 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003916 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3917 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003918 mHistoryCur.wakeReasonTag.uid = 0;
3919 mLastWakeupReason = reason;
3920 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003921 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003922 }
3923
Adam Lesinski72478f02015-06-17 15:39:43 -07003924 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003925 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003926 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003927 }
3928
Adam Lesinski72478f02015-06-17 15:39:43 -07003929 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3930 int statSystemTime, int statIOWaitTime, int statIrqTime,
3931 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003932 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3933 + " user=" + statUserTime + " sys=" + statSystemTime
3934 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3935 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3936 mCurStepCpuUserTime += totalUTime;
3937 mCurStepCpuSystemTime += totalSTime;
3938 mCurStepStatUserTime += statUserTime;
3939 mCurStepStatSystemTime += statSystemTime;
3940 mCurStepStatIOWaitTime += statIOWaitTime;
3941 mCurStepStatIrqTime += statIrqTime;
3942 mCurStepStatSoftIrqTime += statSoftIrqTime;
3943 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003944 }
3945
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003946 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003947 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003948 Uid u = mUidStats.get(uid);
3949 if (u != null) {
3950 u.mPids.remove(pid);
3951 }
3952 }
3953
3954 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003955 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003956 Uid u = mUidStats.get(uid);
3957 if (u != null) {
3958 Uid.Pid p = u.mPids.get(pid);
3959 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003960 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003961 }
3962 }
3963 return 0;
3964 }
3965
3966 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
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 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3971 }
3972 }
3973
Dianne Hackborn287952c2010-09-22 22:34:31 -07003974 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003975 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003976 Uid u = mUidStats.get(uid);
3977 if (u != null) {
3978 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3979 }
3980 }
3981
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003982 int mSensorNesting;
3983
3984 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003985 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003986 final long elapsedRealtime = mClocks.elapsedRealtime();
3987 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003988 if (mSensorNesting == 0) {
3989 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3990 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3991 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003992 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003993 }
3994 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003995 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003996 }
3997
3998 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003999 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004000 final long elapsedRealtime = mClocks.elapsedRealtime();
4001 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004002 mSensorNesting--;
4003 if (mSensorNesting == 0) {
4004 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
4005 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
4006 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004007 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004008 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004009 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004010 }
4011
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004012 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004013
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004014 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004015 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004016 final long elapsedRealtime = mClocks.elapsedRealtime();
4017 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004018 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004019 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004020 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
4021 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004022 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004023 }
4024 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004025 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004027
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004028 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004029 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004030 final long elapsedRealtime = mClocks.elapsedRealtime();
4031 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004032 mGpsNesting--;
4033 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004034 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004035 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
4036 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004037 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004038 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004039 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004041
Jeff Browne95c3cd2014-05-02 16:59:26 -07004042 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004043 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08004044
4045 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
4046 // original 4 are mapped to one of the originals.
4047 if (state > MAX_TRACKED_SCREEN_STATE) {
4048 switch (state) {
4049 case Display.STATE_VR:
4050 state = Display.STATE_ON;
4051 break;
4052 default:
4053 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
4054 break;
4055 }
4056 }
4057
Jeff Browne95c3cd2014-05-02 16:59:26 -07004058 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004059 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004060 final int oldState = mScreenState;
4061 mScreenState = state;
4062 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
4063 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004064
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004065 if (state != Display.STATE_UNKNOWN) {
4066 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08004067 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
4068 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
4069 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004070 } else {
4071 Slog.wtf(TAG, "Unexpected screen state: " + state);
4072 }
4073 }
4074
Jeff Browne95c3cd2014-05-02 16:59:26 -07004075 if (state == Display.STATE_ON) {
4076 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08004077 final long elapsedRealtime = mClocks.elapsedRealtime();
4078 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004079 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
4080 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
4081 + Integer.toHexString(mHistoryCur.states));
4082 addHistoryRecordLocked(elapsedRealtime, uptime);
4083 mScreenOnTimer.startRunningLocked(elapsedRealtime);
4084 if (mScreenBrightnessBin >= 0) {
4085 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
4086 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004087
Jeff Browne95c3cd2014-05-02 16:59:26 -07004088 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08004089 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004090
Jeff Browne95c3cd2014-05-02 16:59:26 -07004091 // Fake a wake lock, so we consider the device waked as long
4092 // as the screen is on.
4093 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
4094 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004095
Jeff Browne95c3cd2014-05-02 16:59:26 -07004096 // Update discharge amounts.
4097 if (mOnBatteryInternal) {
4098 updateDischargeScreenLevelsLocked(false, true);
4099 }
4100 } else if (oldState == Display.STATE_ON) {
4101 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08004102 final long elapsedRealtime = mClocks.elapsedRealtime();
4103 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004104 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
4105 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
4106 + Integer.toHexString(mHistoryCur.states));
4107 addHistoryRecordLocked(elapsedRealtime, uptime);
4108 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
4109 if (mScreenBrightnessBin >= 0) {
4110 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
4111 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004112
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004113 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07004114 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004115
Jeff Browne95c3cd2014-05-02 16:59:26 -07004116 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08004117 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004118
Jeff Browne95c3cd2014-05-02 16:59:26 -07004119 // Update discharge amounts.
4120 if (mOnBatteryInternal) {
4121 updateDischargeScreenLevelsLocked(true, false);
4122 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004123 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004124 }
4125 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004126
Dianne Hackborn617f8772009-03-31 15:04:46 -07004127 public void noteScreenBrightnessLocked(int brightness) {
4128 // Bin the brightness.
4129 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
4130 if (bin < 0) bin = 0;
4131 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
4132 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004133 final long elapsedRealtime = mClocks.elapsedRealtime();
4134 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004135 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
4136 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004137 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
4138 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004139 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004140 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004141 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004142 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004143 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004144 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004145 }
4146 mScreenBrightnessBin = bin;
4147 }
4148 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004149
Dianne Hackborn617f8772009-03-31 15:04:46 -07004150 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004151 if (mOnBatteryInternal) {
4152 uid = mapUid(uid);
4153 getUidStatsLocked(uid).noteUserActivityLocked(event);
4154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004156
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004157 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004158 final long elapsedRealtime = mClocks.elapsedRealtime();
4159 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004160 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
4161 reason, reasonUid);
4162 }
4163
Jeff Browne95c3cd2014-05-02 16:59:26 -07004164 public void noteInteractiveLocked(boolean interactive) {
4165 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08004166 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004167 mInteractive = interactive;
4168 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
4169 if (interactive) {
4170 mInteractiveTimer.startRunningLocked(elapsedRealtime);
4171 } else {
4172 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
4173 }
4174 }
4175 }
4176
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004177 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08004178 final long elapsedRealtime = mClocks.elapsedRealtime();
4179 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004180 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
4181 extra, type);
4182 mNumConnectivityChange++;
4183 }
4184
Adam Lesinski5f056f62016-07-14 16:56:08 -07004185 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
4186 final long uptimeMillis, int uid) {
4187 uid = mapUid(uid);
4188 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4189 uid);
4190 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4191 }
4192
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004193 /**
4194 * Updates the radio power state and returns true if an external stats collection should occur.
4195 */
4196 public boolean noteMobileRadioPowerStateLocked(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004197 final long elapsedRealtime = mClocks.elapsedRealtime();
4198 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004199 if (mMobileRadioPowerState != powerState) {
4200 long realElapsedRealtimeMs;
4201 final boolean active =
4202 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4203 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4204 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004205 if (uid > 0) {
4206 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4207 }
4208
Adam Lesinski9acfd812016-04-19 18:29:50 -07004209 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004210 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4211 } else {
4212 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004213 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004214 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4215 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4216 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004217 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004218 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4219 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4220 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004221 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004222 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4223 }
4224 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4225 + Integer.toHexString(mHistoryCur.states));
4226 addHistoryRecordLocked(elapsedRealtime, uptime);
4227 mMobileRadioPowerState = powerState;
4228 if (active) {
4229 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4230 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4231 } else {
4232 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004233 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004234 // Tell the caller to collect radio network/power stats.
4235 return true;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004236 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004237 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004238 return false;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004239 }
4240
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004241 public void notePowerSaveModeLocked(boolean enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004242 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004243 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4244 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4245 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004246 final long elapsedRealtime = mClocks.elapsedRealtime();
4247 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004248 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004249 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004250 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4251 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004252 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004253 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004254 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004255 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4256 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004257 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004258 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004259 }
4260 addHistoryRecordLocked(elapsedRealtime, uptime);
4261 }
4262 }
4263
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004264 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004265 final long elapsedRealtime = mClocks.elapsedRealtime();
4266 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004267 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004268 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004269 // We don't go out of general idling mode until explicitly taken out of
4270 // device idle through going active or significant motion.
4271 nowIdling = true;
4272 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004273 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4274 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4275 // We don't go out of general light idling mode until explicitly taken out of
4276 // device idle through going active or significant motion.
4277 nowLightIdling = true;
4278 }
4279 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4280 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4281 activeReason, activeUid);
4282 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004283 if (mDeviceIdling != nowIdling) {
4284 mDeviceIdling = nowIdling;
4285 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4286 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4287 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004288 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004289 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4290 } else {
4291 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4292 }
4293 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004294 if (mDeviceLightIdling != nowLightIdling) {
4295 mDeviceLightIdling = nowLightIdling;
4296 if (nowLightIdling) {
4297 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004298 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004299 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004300 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004301 }
4302 if (mDeviceIdleMode != mode) {
4303 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4304 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4305 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4306 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004307 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004308 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4309 mLastIdleTimeStart = elapsedRealtime;
4310 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4311 if (lastDuration > mLongestLightIdleTime) {
4312 mLongestLightIdleTime = lastDuration;
4313 }
4314 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004315 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004316 if (lastDuration > mLongestFullIdleTime) {
4317 mLongestFullIdleTime = lastDuration;
4318 }
4319 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4320 }
4321 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4322 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004323 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004324 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4325 }
4326 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004327 }
4328 }
4329
4330 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004331 final long elapsedRealtime = mClocks.elapsedRealtime();
4332 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004333 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4334 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004335 PackageChange pc = new PackageChange();
4336 pc.mPackageName = pkgName;
4337 pc.mUpdate = true;
4338 pc.mVersionCode = versionCode;
4339 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004340 }
4341
4342 public void notePackageUninstalledLocked(String pkgName) {
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_UNINSTALLED,
4346 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004347 PackageChange pc = new PackageChange();
4348 pc.mPackageName = pkgName;
4349 pc.mUpdate = true;
4350 addPackageChange(pc);
4351 }
4352
4353 private void addPackageChange(PackageChange pc) {
4354 if (mDailyPackageChanges == null) {
4355 mDailyPackageChanges = new ArrayList<>();
4356 }
4357 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004358 }
4359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 public void notePhoneOnLocked() {
4361 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004362 final long elapsedRealtime = mClocks.elapsedRealtime();
4363 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004364 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004365 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4366 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004367 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004369 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 }
4371 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 public void notePhoneOffLocked() {
4374 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004375 final long elapsedRealtime = mClocks.elapsedRealtime();
4376 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004377 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004378 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4379 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004380 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004382 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 }
4384 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004385
Dianne Hackborn3251b902014-06-20 14:40:53 -07004386 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004387 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004388 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004389 if (i == except) {
4390 continue;
4391 }
4392 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004393 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004394 }
4395 }
4396 }
4397
Dianne Hackborne4a59512010-12-07 11:08:07 -08004398 private int fixPhoneServiceState(int state, int signalBin) {
4399 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4400 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4401 // to infer that we are scanning from other data.
4402 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004403 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004404 state = ServiceState.STATE_IN_SERVICE;
4405 }
4406 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004407
Dianne Hackborne4a59512010-12-07 11:08:07 -08004408 return state;
4409 }
4410
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004411 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004412 boolean scanning = false;
4413 boolean newHistory = false;
4414
4415 mPhoneServiceStateRaw = state;
4416 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004417 mPhoneSignalStrengthBinRaw = strengthBin;
4418
Joe Onoratoabded112016-02-08 16:49:39 -08004419 final long elapsedRealtime = mClocks.elapsedRealtime();
4420 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004421
4422 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4423 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4424 // to infer that we are scanning from other data.
4425 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004426 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004427 state = ServiceState.STATE_IN_SERVICE;
4428 }
4429 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004430
4431 // If the phone is powered off, stop all timers.
4432 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004433 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004434
Dianne Hackborne4a59512010-12-07 11:08:07 -08004435 // If we are in service, make sure the correct signal string timer is running.
4436 } else if (state == ServiceState.STATE_IN_SERVICE) {
4437 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004438
4439 // If we're out of service, we are in the lowest signal strength
4440 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004441 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004442 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004443 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004444 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004445 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004446 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004447 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4448 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004449 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004450 }
4451 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004452
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004453 if (!scanning) {
4454 // If we are no longer scanning, then stop the scanning timer.
4455 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4456 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4457 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4458 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004459 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004460 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004461 }
4462 }
4463
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004464 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004465 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4466 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004467 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004468 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004469 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004470 mPhoneServiceState = state;
4471 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004472
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004473 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004474 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004475 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004476 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004477 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004478 if (strengthBin >= 0) {
4479 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004480 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004481 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004482 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4483 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004484 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004485 + Integer.toHexString(mHistoryCur.states));
4486 newHistory = true;
4487 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004488 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004489 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004490 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004491 }
4492
4493 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004494 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004495 }
4496 }
4497
4498 /**
4499 * Telephony stack updates the phone state.
4500 * @param state phone state from ServiceState.getState()
4501 */
4502 public void notePhoneStateLocked(int state, int simState) {
4503 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004504 }
4505
Wink Savillee9b06d72009-05-18 21:47:50 -07004506 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004507 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004508 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004509 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004510 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004511
Dianne Hackborn627bba72009-03-24 22:32:56 -07004512 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4513 int bin = DATA_CONNECTION_NONE;
4514 if (hasData) {
4515 switch (dataType) {
4516 case TelephonyManager.NETWORK_TYPE_EDGE:
4517 bin = DATA_CONNECTION_EDGE;
4518 break;
4519 case TelephonyManager.NETWORK_TYPE_GPRS:
4520 bin = DATA_CONNECTION_GPRS;
4521 break;
4522 case TelephonyManager.NETWORK_TYPE_UMTS:
4523 bin = DATA_CONNECTION_UMTS;
4524 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004525 case TelephonyManager.NETWORK_TYPE_CDMA:
4526 bin = DATA_CONNECTION_CDMA;
4527 break;
4528 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4529 bin = DATA_CONNECTION_EVDO_0;
4530 break;
4531 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4532 bin = DATA_CONNECTION_EVDO_A;
4533 break;
4534 case TelephonyManager.NETWORK_TYPE_1xRTT:
4535 bin = DATA_CONNECTION_1xRTT;
4536 break;
4537 case TelephonyManager.NETWORK_TYPE_HSDPA:
4538 bin = DATA_CONNECTION_HSDPA;
4539 break;
4540 case TelephonyManager.NETWORK_TYPE_HSUPA:
4541 bin = DATA_CONNECTION_HSUPA;
4542 break;
4543 case TelephonyManager.NETWORK_TYPE_HSPA:
4544 bin = DATA_CONNECTION_HSPA;
4545 break;
4546 case TelephonyManager.NETWORK_TYPE_IDEN:
4547 bin = DATA_CONNECTION_IDEN;
4548 break;
4549 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4550 bin = DATA_CONNECTION_EVDO_B;
4551 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004552 case TelephonyManager.NETWORK_TYPE_LTE:
4553 bin = DATA_CONNECTION_LTE;
4554 break;
4555 case TelephonyManager.NETWORK_TYPE_EHRPD:
4556 bin = DATA_CONNECTION_EHRPD;
4557 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004558 case TelephonyManager.NETWORK_TYPE_HSPAP:
4559 bin = DATA_CONNECTION_HSPAP;
4560 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004561 default:
4562 bin = DATA_CONNECTION_OTHER;
4563 break;
4564 }
4565 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004566 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004567 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004568 final long elapsedRealtime = mClocks.elapsedRealtime();
4569 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004570 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4571 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004572 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4573 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004574 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004575 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004576 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004577 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004578 }
4579 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004580 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004581 }
4582 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004583
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004584 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004585 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004586 final long elapsedRealtime = mClocks.elapsedRealtime();
4587 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004588 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004589 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4590 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004591 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004592 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004593 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004594 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004595 }
4596 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004597
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004598 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004599 final long elapsedRealtime = mClocks.elapsedRealtime();
4600 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004601 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004602 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004603 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4604 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004605 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004606 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004607 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004608 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004609 }
4610 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004611
4612 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004613 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004614 final long elapsedRealtime = mClocks.elapsedRealtime();
4615 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004616 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004617 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004618 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4619 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004620 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004621 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004622 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004623 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004624 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004625 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004626
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004627 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004628 if (mAudioOnNesting == 0) {
4629 return;
4630 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004631 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004632 final long elapsedRealtime = mClocks.elapsedRealtime();
4633 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004634 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004635 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004636 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4637 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004638 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004639 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004640 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004641 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004642 }
4643
4644 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004645 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004646 final long elapsedRealtime = mClocks.elapsedRealtime();
4647 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004648 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004649 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004650 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4651 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004652 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004653 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004654 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004655 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004656 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004657 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004658
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004659 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004660 if (mVideoOnNesting == 0) {
4661 return;
4662 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004663 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004664 final long elapsedRealtime = mClocks.elapsedRealtime();
4665 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004666 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004667 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004668 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4669 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004670 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004671 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004672 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004673 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004674 }
4675
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004676 public void noteResetAudioLocked() {
4677 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004678 final long elapsedRealtime = mClocks.elapsedRealtime();
4679 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004680 mAudioOnNesting = 0;
4681 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4682 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4683 + Integer.toHexString(mHistoryCur.states));
4684 addHistoryRecordLocked(elapsedRealtime, uptime);
4685 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4686 for (int i=0; i<mUidStats.size(); i++) {
4687 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4688 uid.noteResetAudioLocked(elapsedRealtime);
4689 }
4690 }
4691 }
4692
4693 public void noteResetVideoLocked() {
4694 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004695 final long elapsedRealtime = mClocks.elapsedRealtime();
4696 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004697 mAudioOnNesting = 0;
4698 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4699 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4700 + Integer.toHexString(mHistoryCur.states));
4701 addHistoryRecordLocked(elapsedRealtime, uptime);
4702 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4703 for (int i=0; i<mUidStats.size(); i++) {
4704 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4705 uid.noteResetVideoLocked(elapsedRealtime);
4706 }
4707 }
4708 }
4709
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004710 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004711 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004712 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004713 }
4714
4715 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004716 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004717 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004718 }
4719
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004720 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004721 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004722 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4723 }
4724
4725 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004726 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004727 getUidStatsLocked(uid).noteVibratorOffLocked();
4728 }
4729
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004730 public void noteFlashlightOnLocked(int uid) {
4731 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004732 final long elapsedRealtime = mClocks.elapsedRealtime();
4733 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004734 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004735 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4736 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004737 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004738 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004739 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4740 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004741 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4742 }
4743
4744 public void noteFlashlightOffLocked(int uid) {
4745 if (mFlashlightOnNesting == 0) {
4746 return;
4747 }
4748 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004749 final long elapsedRealtime = mClocks.elapsedRealtime();
4750 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004751 if (--mFlashlightOnNesting == 0) {
4752 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4753 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4754 + Integer.toHexString(mHistoryCur.states2));
4755 addHistoryRecordLocked(elapsedRealtime, uptime);
4756 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4757 }
4758 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4759 }
4760
4761 public void noteCameraOnLocked(int uid) {
4762 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004763 final long elapsedRealtime = mClocks.elapsedRealtime();
4764 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004765 if (mCameraOnNesting++ == 0) {
4766 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4767 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4768 + Integer.toHexString(mHistoryCur.states2));
4769 addHistoryRecordLocked(elapsedRealtime, uptime);
4770 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4771 }
4772 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4773 }
4774
4775 public void noteCameraOffLocked(int uid) {
4776 if (mCameraOnNesting == 0) {
4777 return;
4778 }
4779 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004780 final long elapsedRealtime = mClocks.elapsedRealtime();
4781 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004782 if (--mCameraOnNesting == 0) {
4783 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4784 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4785 + Integer.toHexString(mHistoryCur.states2));
4786 addHistoryRecordLocked(elapsedRealtime, uptime);
4787 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4788 }
4789 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4790 }
4791
4792 public void noteResetCameraLocked() {
4793 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004794 final long elapsedRealtime = mClocks.elapsedRealtime();
4795 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004796 mCameraOnNesting = 0;
4797 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4798 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4799 + Integer.toHexString(mHistoryCur.states2));
4800 addHistoryRecordLocked(elapsedRealtime, uptime);
4801 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4802 for (int i=0; i<mUidStats.size(); i++) {
4803 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4804 uid.noteResetCameraLocked(elapsedRealtime);
4805 }
4806 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004807 }
4808
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004809 public void noteResetFlashlightLocked() {
4810 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004811 final long elapsedRealtime = mClocks.elapsedRealtime();
4812 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004813 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004814 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4815 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004816 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004817 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004818 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4819 for (int i=0; i<mUidStats.size(); i++) {
4820 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4821 uid.noteResetFlashlightLocked(elapsedRealtime);
4822 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004823 }
4824 }
4825
Bookatzb1f04f32017-05-19 13:57:32 -07004826 private void noteBluetoothScanStartedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004827 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004828 final long elapsedRealtime = mClocks.elapsedRealtime();
4829 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004830 if (mBluetoothScanNesting == 0) {
4831 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4832 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4833 + Integer.toHexString(mHistoryCur.states2));
4834 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004835 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004836 }
4837 mBluetoothScanNesting++;
Bookatzb1f04f32017-05-19 13:57:32 -07004838 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004839 }
4840
Bookatzb1f04f32017-05-19 13:57:32 -07004841 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004842 final int N = ws.size();
4843 for (int i = 0; i < N; i++) {
Bookatzb1f04f32017-05-19 13:57:32 -07004844 noteBluetoothScanStartedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004845 }
4846 }
4847
Bookatz20e22f62017-07-11 16:49:17 -07004848 private void noteBluetoothScanStoppedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004849 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004850 final long elapsedRealtime = mClocks.elapsedRealtime();
4851 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004852 mBluetoothScanNesting--;
4853 if (mBluetoothScanNesting == 0) {
4854 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4855 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4856 + Integer.toHexString(mHistoryCur.states2));
4857 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004858 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004859 }
Bookatz20e22f62017-07-11 16:49:17 -07004860 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004861 }
4862
Bookatz20e22f62017-07-11 16:49:17 -07004863 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004864 final int N = ws.size();
4865 for (int i = 0; i < N; i++) {
Bookatz20e22f62017-07-11 16:49:17 -07004866 noteBluetoothScanStoppedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004867 }
4868 }
4869
4870 public void noteResetBluetoothScanLocked() {
4871 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004872 final long elapsedRealtime = mClocks.elapsedRealtime();
4873 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004874 mBluetoothScanNesting = 0;
4875 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4876 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4877 + Integer.toHexString(mHistoryCur.states2));
4878 addHistoryRecordLocked(elapsedRealtime, uptime);
4879 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4880 for (int i=0; i<mUidStats.size(); i++) {
4881 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4882 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4883 }
4884 }
4885 }
4886
Bookatz4ebc0642017-05-11 12:21:19 -07004887 public void noteBluetoothScanResultsFromSourceLocked(WorkSource ws, int numNewResults) {
Bookatz956f36bf2017-04-28 09:48:17 -07004888 final int N = ws.size();
4889 for (int i = 0; i < N; i++) {
4890 int uid = mapUid(ws.get(i));
Bookatz4ebc0642017-05-11 12:21:19 -07004891 getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07004892 }
4893 }
4894
Adam Lesinski5f056f62016-07-14 16:56:08 -07004895 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4896 final long uptimeMillis, int uid) {
4897 uid = mapUid(uid);
4898 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4899 uid);
4900 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4901 }
4902
4903 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004904 final long elapsedRealtime = mClocks.elapsedRealtime();
4905 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004906 if (mWifiRadioPowerState != powerState) {
4907 final boolean active =
4908 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4909 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4910 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004911 if (uid > 0) {
4912 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4913 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004914 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4915 } else {
4916 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4917 }
4918 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4919 + Integer.toHexString(mHistoryCur.states));
4920 addHistoryRecordLocked(elapsedRealtime, uptime);
4921 mWifiRadioPowerState = powerState;
4922 }
4923 }
4924
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004925 public void noteWifiRunningLocked(WorkSource ws) {
4926 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004927 final long elapsedRealtime = mClocks.elapsedRealtime();
4928 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004929 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004930 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4931 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004932 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004933 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004934 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004935 int N = ws.size();
4936 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004937 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004938 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004939 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004940 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004941 } else {
4942 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004943 }
4944 }
4945
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004946 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4947 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004948 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004949 int N = oldWs.size();
4950 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004951 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004952 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004953 }
4954 N = newWs.size();
4955 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004956 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004957 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004958 }
4959 } else {
4960 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4961 }
4962 }
4963
4964 public void noteWifiStoppedLocked(WorkSource ws) {
4965 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004966 final long elapsedRealtime = mClocks.elapsedRealtime();
4967 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004968 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004969 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4970 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004971 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004972 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004973 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004974 int N = ws.size();
4975 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004976 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004977 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004978 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004979 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004980 } else {
4981 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004982 }
4983 }
4984
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004985 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4986 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4987 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004988 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004989 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004990 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004991 }
4992 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004993 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004994 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004995 }
4996 }
4997
Dianne Hackborn3251b902014-06-20 14:40:53 -07004998 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4999 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
5000 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005001 final long elapsedRealtime = mClocks.elapsedRealtime();
5002 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005003 if (mWifiSupplState >= 0) {
5004 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
5005 }
5006 mWifiSupplState = supplState;
5007 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
5008 mHistoryCur.states2 =
5009 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
5010 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
5011 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
5012 + Integer.toHexString(mHistoryCur.states2));
5013 addHistoryRecordLocked(elapsedRealtime, uptime);
5014 }
5015 }
5016
5017 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08005018 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005019 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
5020 if (i == except) {
5021 continue;
5022 }
5023 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
5024 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
5025 }
5026 }
5027 }
5028
5029 public void noteWifiRssiChangedLocked(int newRssi) {
5030 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
5031 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
5032 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08005033 final long elapsedRealtime = mClocks.elapsedRealtime();
5034 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005035 if (mWifiSignalStrengthBin >= 0) {
5036 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
5037 elapsedRealtime);
5038 }
5039 if (strengthBin >= 0) {
5040 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
5041 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
5042 }
5043 mHistoryCur.states2 =
5044 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
5045 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
5046 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
5047 + Integer.toHexString(mHistoryCur.states2));
5048 addHistoryRecordLocked(elapsedRealtime, uptime);
5049 } else {
5050 stopAllWifiSignalStrengthTimersLocked(-1);
5051 }
5052 mWifiSignalStrengthBin = strengthBin;
5053 }
5054 }
5055
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005056 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005057
The Android Open Source Project10592532009-03-18 17:39:46 -07005058 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005059 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005060 final long elapsedRealtime = mClocks.elapsedRealtime();
5061 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005062 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005063 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005064 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
5065 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005066 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005067 }
5068 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005069 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005070 }
5071
5072 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005073 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005074 final long elapsedRealtime = mClocks.elapsedRealtime();
5075 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005076 mWifiFullLockNesting--;
5077 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005078 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005079 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
5080 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005081 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005082 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005083 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005084 }
5085
Nick Pelly6ccaa542012-06-15 15:22:47 -07005086 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005087
Nick Pelly6ccaa542012-06-15 15:22:47 -07005088 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005089 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005090 final long elapsedRealtime = mClocks.elapsedRealtime();
5091 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005092 if (mWifiScanNesting == 0) {
5093 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
5094 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005095 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005096 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005097 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005098 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005099 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005100 }
5101
Nick Pelly6ccaa542012-06-15 15:22:47 -07005102 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005103 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005104 final long elapsedRealtime = mClocks.elapsedRealtime();
5105 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005106 mWifiScanNesting--;
5107 if (mWifiScanNesting == 0) {
5108 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
5109 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005110 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005111 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005112 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005113 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005114 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005115
Robert Greenwalta029ea12013-09-25 16:38:12 -07005116 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005117 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005118 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005119 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005120 }
5121
5122 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005123 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005124 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005125 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005126 }
5127
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005128 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005129
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005130 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005131 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005132 final long elapsedRealtime = mClocks.elapsedRealtime();
5133 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005134 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005135 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005136 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
5137 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005138 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005139 }
5140 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005141 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005142 }
5143
5144 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005145 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005146 final long elapsedRealtime = mClocks.elapsedRealtime();
5147 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005148 mWifiMulticastNesting--;
5149 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005150 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005151 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
5152 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005153 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005154 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005155 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005156 }
5157
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005158 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
5159 int N = ws.size();
5160 for (int i=0; i<N; i++) {
5161 noteFullWifiLockAcquiredLocked(ws.get(i));
5162 }
5163 }
5164
5165 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
5166 int N = ws.size();
5167 for (int i=0; i<N; i++) {
5168 noteFullWifiLockReleasedLocked(ws.get(i));
5169 }
5170 }
5171
Nick Pelly6ccaa542012-06-15 15:22:47 -07005172 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005173 int N = ws.size();
5174 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005175 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005176 }
5177 }
5178
Nick Pelly6ccaa542012-06-15 15:22:47 -07005179 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005180 int N = ws.size();
5181 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005182 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005183 }
5184 }
5185
Robert Greenwalta029ea12013-09-25 16:38:12 -07005186 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
5187 int N = ws.size();
5188 for (int i=0; i<N; i++) {
5189 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
5190 }
5191 }
5192
5193 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
5194 int N = ws.size();
5195 for (int i=0; i<N; i++) {
5196 noteWifiBatchedScanStoppedLocked(ws.get(i));
5197 }
5198 }
5199
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005200 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
5201 int N = ws.size();
5202 for (int i=0; i<N; i++) {
5203 noteWifiMulticastEnabledLocked(ws.get(i));
5204 }
5205 }
5206
5207 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5208 int N = ws.size();
5209 for (int i=0; i<N; i++) {
5210 noteWifiMulticastDisabledLocked(ws.get(i));
5211 }
5212 }
5213
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005214 private static String[] includeInStringArray(String[] array, String str) {
5215 if (ArrayUtils.indexOf(array, str) >= 0) {
5216 return array;
5217 }
5218 String[] newArray = new String[array.length+1];
5219 System.arraycopy(array, 0, newArray, 0, array.length);
5220 newArray[array.length] = str;
5221 return newArray;
5222 }
5223
5224 private static String[] excludeFromStringArray(String[] array, String str) {
5225 int index = ArrayUtils.indexOf(array, str);
5226 if (index >= 0) {
5227 String[] newArray = new String[array.length-1];
5228 if (index > 0) {
5229 System.arraycopy(array, 0, newArray, 0, index);
5230 }
5231 if (index < array.length-1) {
5232 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5233 }
5234 return newArray;
5235 }
5236 return array;
5237 }
5238
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005239 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005240 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005241
Adam Lesinski14ae39a2017-05-26 11:50:40 -07005242 synchronized (mModemNetworkLock) {
5243 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
5244 mModemIfaces = includeInStringArray(mModemIfaces, iface);
5245 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
5246 } else {
5247 mModemIfaces = excludeFromStringArray(mModemIfaces, iface);
5248 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mModemIfaces);
5249 }
5250 }
5251
5252 synchronized (mWifiNetworkLock) {
5253 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
5254 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5255 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
5256 } else {
5257 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5258 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
5259 }
5260 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005261 }
5262
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005263 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5264 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005265 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005266
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005267 @Override public int getScreenOnCount(int which) {
5268 return mScreenOnTimer.getCountLocked(which);
5269 }
5270
Dianne Hackborn617f8772009-03-31 15:04:46 -07005271 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005272 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005273 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005274 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005275 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005276
Jeff Browne95c3cd2014-05-02 16:59:26 -07005277 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5278 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005279 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005280
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005281 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5282 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005283 }
5284
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005285 @Override public int getPowerSaveModeEnabledCount(int which) {
5286 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5287 }
5288
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005289 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5290 int which) {
5291 switch (mode) {
5292 case DEVICE_IDLE_MODE_LIGHT:
5293 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005294 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005295 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5296 }
5297 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005298 }
5299
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005300 @Override public int getDeviceIdleModeCount(int mode, int which) {
5301 switch (mode) {
5302 case DEVICE_IDLE_MODE_LIGHT:
5303 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005304 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005305 return mDeviceIdleModeFullTimer.getCountLocked(which);
5306 }
5307 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005308 }
5309
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005310 @Override public long getLongestDeviceIdleModeTime(int mode) {
5311 switch (mode) {
5312 case DEVICE_IDLE_MODE_LIGHT:
5313 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005314 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005315 return mLongestFullIdleTime;
5316 }
5317 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005318 }
5319
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005320 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5321 switch (mode) {
5322 case DEVICE_IDLE_MODE_LIGHT:
5323 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005324 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005325 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5326 }
5327 return 0;
5328 }
5329
5330 @Override public int getDeviceIdlingCount(int mode, int which) {
5331 switch (mode) {
5332 case DEVICE_IDLE_MODE_LIGHT:
5333 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005334 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005335 return mDeviceIdlingTimer.getCountLocked(which);
5336 }
5337 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005338 }
5339
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005340 @Override public int getNumConnectivityChange(int which) {
5341 int val = mNumConnectivityChange;
5342 if (which == STATS_CURRENT) {
5343 val -= mLoadedNumConnectivityChange;
5344 } else if (which == STATS_SINCE_UNPLUGGED) {
5345 val -= mUnpluggedNumConnectivityChange;
5346 }
5347 return val;
5348 }
5349
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005350 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5351 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005353
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005354 @Override public int getPhoneOnCount(int which) {
5355 return mPhoneOnTimer.getCountLocked(which);
5356 }
5357
Dianne Hackborn627bba72009-03-24 22:32:56 -07005358 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005359 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005360 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005361 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005362 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005363
5364 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005365 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005366 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005367 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005368 }
5369
Catherine Liufb900812012-07-17 14:12:56 -05005370 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5371 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005372 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005373
Dianne Hackborn627bba72009-03-24 22:32:56 -07005374 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005375 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005376 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005377 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005378 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005379
Dianne Hackborn617f8772009-03-31 15:04:46 -07005380 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005381 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005382 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005383
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005384 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5385 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005386 }
5387
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005388 @Override public int getMobileRadioActiveCount(int which) {
5389 return mMobileRadioActiveTimer.getCountLocked(which);
5390 }
5391
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005392 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5393 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5394 }
5395
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005396 @Override public long getMobileRadioActiveUnknownTime(int which) {
5397 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5398 }
5399
5400 @Override public int getMobileRadioActiveUnknownCount(int which) {
5401 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5402 }
5403
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005404 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5405 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005406 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005407
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005408 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5409 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005410 }
5411
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005412 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005413 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005414 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005415 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005416 }
5417
5418 @Override public int getWifiStateCount(int wifiState, int which) {
5419 return mWifiStateTimer[wifiState].getCountLocked(which);
5420 }
5421
Dianne Hackborn3251b902014-06-20 14:40:53 -07005422 @Override public long getWifiSupplStateTime(int state,
5423 long elapsedRealtimeUs, int which) {
5424 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5425 elapsedRealtimeUs, which);
5426 }
5427
5428 @Override public int getWifiSupplStateCount(int state, int which) {
5429 return mWifiSupplStateTimer[state].getCountLocked(which);
5430 }
5431
5432 @Override public long getWifiSignalStrengthTime(int strengthBin,
5433 long elapsedRealtimeUs, int which) {
5434 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5435 elapsedRealtimeUs, which);
5436 }
5437
5438 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5439 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5440 }
5441
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005442 @Override
5443 public ControllerActivityCounter getBluetoothControllerActivity() {
5444 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005445 }
5446
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005447 @Override
5448 public ControllerActivityCounter getWifiControllerActivity() {
5449 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005450 }
5451
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005452 @Override
5453 public ControllerActivityCounter getModemControllerActivity() {
5454 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005455 }
5456
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005457 @Override
5458 public boolean hasBluetoothActivityReporting() {
5459 return mHasBluetoothReporting;
5460 }
5461
5462 @Override
5463 public boolean hasWifiActivityReporting() {
5464 return mHasWifiReporting;
5465 }
5466
5467 @Override
5468 public boolean hasModemActivityReporting() {
5469 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005470 }
5471
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005472 @Override
5473 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005474 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5475 }
5476
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005477 @Override
5478 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005479 return mFlashlightOnTimer.getCountLocked(which);
5480 }
5481
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005482 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005483 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5484 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5485 }
5486
5487 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005488 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5489 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5490 }
5491
5492 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005493 public long getNetworkActivityBytes(int type, int which) {
5494 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5495 return mNetworkByteActivityCounters[type].getCountLocked(which);
5496 } else {
5497 return 0;
5498 }
5499 }
5500
5501 @Override
5502 public long getNetworkActivityPackets(int type, int which) {
5503 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5504 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005505 } else {
5506 return 0;
5507 }
5508 }
5509
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005510 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005511 final long currentTime = System.currentTimeMillis();
5512 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005513 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5514 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005515 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005516 return mStartClockTime;
5517 }
5518
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005519 @Override public String getStartPlatformVersion() {
5520 return mStartPlatformVersion;
5521 }
5522
5523 @Override public String getEndPlatformVersion() {
5524 return mEndPlatformVersion;
5525 }
5526
5527 @Override public int getParcelVersion() {
5528 return VERSION;
5529 }
5530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 @Override public boolean getIsOnBattery() {
5532 return mOnBattery;
5533 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5536 return mUidStats;
5537 }
5538
Adam Lesinski5f056f62016-07-14 16:56:08 -07005539 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5540 if (timer != null) {
5541 timer.detach();
5542 }
5543 }
5544
5545 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5546 boolean detachIfReset) {
5547 if (timer != null) {
5548 return timer.reset(detachIfReset);
5549 }
5550 return true;
5551 }
5552
Bookatz867c0d72017-03-07 18:23:42 -08005553 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5554 if (timer != null) {
5555 return timer.reset(detachIfReset);
5556 }
5557 return true;
5558 }
5559
Adam Lesinski5f056f62016-07-14 16:56:08 -07005560 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5561 if (counter != null) {
5562 counter.detach();
5563 }
5564 }
5565
5566 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5567 boolean detachIfReset) {
5568 if (counter != null) {
5569 counter.reset(detachIfReset);
5570 }
5571 }
5572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 /**
5574 * The statistics associated with a particular uid.
5575 */
Joe Onoratoabded112016-02-08 16:49:39 -08005576 public static class Uid extends BatteryStats.Uid {
5577 /**
5578 * BatteryStatsImpl that we are associated with.
5579 */
5580 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005583
Bookatz867c0d72017-03-07 18:23:42 -08005584 /** TimeBase for when uid is in background and device is on battery. */
5585 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5586 public final TimeBase mOnBatteryBackgroundTimeBase;
Bookatzc8c44962017-05-11 12:12:54 -07005587 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5588 public final TimeBase mOnBatteryScreenOffBackgroundTimeBase;
Bookatz867c0d72017-03-07 18:23:42 -08005589
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005590 boolean mWifiRunning;
5591 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005592
The Android Open Source Project10592532009-03-18 17:39:46 -07005593 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005594 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005595
Nick Pelly6ccaa542012-06-15 15:22:47 -07005596 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005597 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005598
Dianne Hackborn61659e52014-07-09 16:13:01 -07005599 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005600 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5601 StopwatchTimer[] mWifiBatchedScanTimer;
5602
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005603 boolean mWifiMulticastEnabled;
5604 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005605
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005606 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005607 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005608 StopwatchTimer mFlashlightTurnedOnTimer;
5609 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005610 StopwatchTimer mForegroundActivityTimer;
Bookatzc8c44962017-05-11 12:12:54 -07005611 /** Total time spent by the uid holding any partial wakelocks. */
5612 DualTimer mAggregatedPartialWakelockTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005613 DualTimer mBluetoothScanTimer;
Bookatzb1f04f32017-05-19 13:57:32 -07005614 DualTimer mBluetoothUnoptimizedScanTimer;
Bookatz956f36bf2017-04-28 09:48:17 -07005615 Counter mBluetoothScanResultCounter;
Bookatzb1f04f32017-05-19 13:57:32 -07005616 Counter mBluetoothScanResultBgCounter;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005617
Dianne Hackborna8d10942015-11-19 17:55:19 -08005618 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005619 StopwatchTimer[] mProcessStateTimer;
5620
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005621 BatchTimer mVibratorOnTimer;
5622
Dianne Hackborn617f8772009-03-31 15:04:46 -07005623 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005624
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005625 LongSamplingCounter[] mNetworkByteActivityCounters;
5626 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005627 LongSamplingCounter mMobileRadioActiveTime;
5628 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005631 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5632 */
5633 private LongSamplingCounter mMobileRadioApWakeupCount;
5634
5635 /**
5636 * How many times this UID woke up the Application Processor due to a Wifi packet.
5637 */
5638 private LongSamplingCounter mWifiRadioApWakeupCount;
5639
5640 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005641 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005642 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005643 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005644 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005645
5646 /**
5647 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005648 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005649 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005650 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5651
5652 /**
5653 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5654 * Can be null if the UID has had no such activity.
5655 */
5656 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005657
5658 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005659 * The CPU times we had at the last history details update.
5660 */
5661 long mLastStepUserTime;
5662 long mLastStepSystemTime;
5663 long mCurStepUserTime;
5664 long mCurStepSystemTime;
5665
Joe Onoratoabded112016-02-08 16:49:39 -08005666 LongSamplingCounter mUserCpuTime;
5667 LongSamplingCounter mSystemCpuTime;
Adam Lesinski6832f392015-09-05 18:05:40 -07005668 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005669
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005670 LongSamplingCounterArray mCpuFreqTimeMs;
5671 LongSamplingCounterArray mScreenOffCpuFreqTimeMs;
5672
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 * The statistics we have collected for this uid's wake locks.
5675 */
Joe Onoratoabded112016-02-08 16:49:39 -08005676 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677
5678 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005679 * The statistics we have collected for this uid's syncs.
5680 */
Bookatz2bffb5b2017-04-13 11:59:33 -07005681 final OverflowArrayMap<DualTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005682
5683 /**
5684 * The statistics we have collected for this uid's jobs.
5685 */
Bookatzaa4594a2017-03-24 12:39:56 -07005686 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005687
5688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 * The statistics we have collected for this uid's sensor activations.
5690 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005691 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692
5693 /**
5694 * The statistics we have collected for this uid's processes.
5695 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005696 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697
5698 /**
5699 * The statistics we have collected for this uid's processes.
5700 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005701 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005702
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005703 /**
5704 * The transient wake stats we have collected for this uid's pids.
5705 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005706 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005707
Joe Onoratoabded112016-02-08 16:49:39 -08005708 public Uid(BatteryStatsImpl bsi, int uid) {
5709 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005711
Bookatz867c0d72017-03-07 18:23:42 -08005712 mOnBatteryBackgroundTimeBase = new TimeBase();
5713 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5714 mBsi.mClocks.elapsedRealtime() * 1000);
5715
Bookatzc8c44962017-05-11 12:12:54 -07005716 mOnBatteryScreenOffBackgroundTimeBase = new TimeBase();
5717 mOnBatteryScreenOffBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5718 mBsi.mClocks.elapsedRealtime() * 1000);
5719
Joe Onoratoabded112016-02-08 16:49:39 -08005720 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5721 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005722
Dianne Hackborn657153b2016-07-29 14:54:14 -07005723 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005724 @Override public Wakelock instantiateObject() {
5725 return new Wakelock(mBsi, Uid.this);
5726 }
5727 };
Bookatz2bffb5b2017-04-13 11:59:33 -07005728 mSyncStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5729 @Override public DualTimer instantiateObject() {
5730 return new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
5731 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005732 }
5733 };
Bookatzaa4594a2017-03-24 12:39:56 -07005734 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5735 @Override public DualTimer instantiateObject() {
5736 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
5737 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005738 }
5739 };
5740
5741 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5742 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5743 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5744 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005745 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5746 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005747 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005748 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5749 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005750 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 }
5752
5753 @Override
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005754 public long[] getCpuFreqTimes(int which) {
5755 if (mCpuFreqTimeMs == null) {
5756 return null;
5757 }
5758 final long[] cpuFreqTimes = mCpuFreqTimeMs.getCountsLocked(which);
5759 if (cpuFreqTimes == null) {
5760 return null;
5761 }
5762 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5763 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5764 if (cpuFreqTimes[i] != 0) {
5765 return cpuFreqTimes;
5766 }
5767 }
5768 return null;
5769 }
5770
5771 @Override
5772 public long[] getScreenOffCpuFreqTimes(int which) {
5773 if (mScreenOffCpuFreqTimeMs == null) {
5774 return null;
5775 }
5776 final long[] cpuFreqTimes = mScreenOffCpuFreqTimeMs.getCountsLocked(which);
5777 if (cpuFreqTimes == null) {
5778 return null;
5779 }
5780 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5781 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5782 if (cpuFreqTimes[i] != 0) {
5783 return cpuFreqTimes;
5784 }
5785 }
5786 return null;
5787 }
5788
5789 @Override
Bookatzc8c44962017-05-11 12:12:54 -07005790 public Timer getAggregatedPartialWakelockTimer() {
5791 return mAggregatedPartialWakelockTimer;
5792 }
5793
5794 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005795 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005796 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 }
5798
5799 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005800 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005801 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005802 }
5803
5804 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005805 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005806 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005807 }
5808
5809 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005810 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 return mSensorStats;
5812 }
5813
5814 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005815 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 return mProcessStats;
5817 }
5818
5819 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005820 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 return mPackageStats;
5822 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005823
5824 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 public int getUid() {
5826 return mUid;
5827 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005828
5829 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005830 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005831 if (!mWifiRunning) {
5832 mWifiRunning = true;
5833 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005834 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5835 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005836 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005837 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005838 }
5839 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005840
Dianne Hackborn617f8772009-03-31 15:04:46 -07005841 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005842 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005843 if (mWifiRunning) {
5844 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005845 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005846 }
5847 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005848
Dianne Hackborn617f8772009-03-31 15:04:46 -07005849 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005850 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005851 if (!mFullWifiLockOut) {
5852 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005853 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005854 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5855 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005856 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005857 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005858 }
5859 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005860
The Android Open Source Project10592532009-03-18 17:39:46 -07005861 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005862 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005863 if (mFullWifiLockOut) {
5864 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005865 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005866 }
5867 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005868
The Android Open Source Project10592532009-03-18 17:39:46 -07005869 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005870 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005871 if (!mWifiScanStarted) {
5872 mWifiScanStarted = true;
5873 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005874 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5875 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
5876 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005877 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005878 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005879 }
5880 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005881
The Android Open Source Project10592532009-03-18 17:39:46 -07005882 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005883 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005884 if (mWifiScanStarted) {
5885 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005886 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005887 }
5888 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005889
5890 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005891 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005892 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005893 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005894 csph = csph >> 3;
5895 bin++;
5896 }
5897
5898 if (mWifiBatchedScanBinStarted == bin) return;
5899
5900 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5901 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005902 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005903 }
5904 mWifiBatchedScanBinStarted = bin;
5905 if (mWifiBatchedScanTimer[bin] == null) {
5906 makeWifiBatchedScanBin(bin, null);
5907 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005908 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005909 }
5910
5911 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005912 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005913 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5914 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005915 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005916 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5917 }
5918 }
5919
5920 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005921 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005922 if (!mWifiMulticastEnabled) {
5923 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005924 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005925 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5926 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005927 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005928 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005929 }
5930 }
5931
5932 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005933 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005934 if (mWifiMulticastEnabled) {
5935 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005936 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005937 }
5938 }
5939
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005940 @Override
5941 public ControllerActivityCounter getWifiControllerActivity() {
5942 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005943 }
5944
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005945 @Override
5946 public ControllerActivityCounter getBluetoothControllerActivity() {
5947 return mBluetoothControllerActivity;
5948 }
5949
5950 @Override
5951 public ControllerActivityCounter getModemControllerActivity() {
5952 return mModemControllerActivity;
5953 }
5954
5955 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5956 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005957 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005958 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005959 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005960 return mWifiControllerActivity;
5961 }
5962
5963 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5964 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005965 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005966 NUM_BT_TX_LEVELS);
5967 }
5968 return mBluetoothControllerActivity;
5969 }
5970
5971 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5972 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005973 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005974 ModemActivityInfo.TX_POWER_LEVELS);
5975 }
5976 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005977 }
5978
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005979 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5980 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005981 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5982 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005983 }
5984 return mAudioTurnedOnTimer;
5985 }
5986
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005987 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005988 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5989 }
5990
5991 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5992 if (mAudioTurnedOnTimer != null) {
5993 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005994 }
5995 }
5996
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005997 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5998 if (mAudioTurnedOnTimer != null) {
5999 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006000 }
6001 }
6002
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006003 public StopwatchTimer createVideoTurnedOnTimerLocked() {
6004 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006005 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6006 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006007 }
6008 return mVideoTurnedOnTimer;
6009 }
6010
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006011 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006012 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6013 }
6014
6015 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
6016 if (mVideoTurnedOnTimer != null) {
6017 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006018 }
6019 }
6020
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006021 public void noteResetVideoLocked(long elapsedRealtimeMs) {
6022 if (mVideoTurnedOnTimer != null) {
6023 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006024 }
6025 }
6026
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006027 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
6028 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006029 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6030 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006031 }
6032 return mFlashlightTurnedOnTimer;
6033 }
6034
6035 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
6036 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6037 }
6038
6039 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
6040 if (mFlashlightTurnedOnTimer != null) {
6041 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6042 }
6043 }
6044
6045 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
6046 if (mFlashlightTurnedOnTimer != null) {
6047 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6048 }
6049 }
6050
6051 public StopwatchTimer createCameraTurnedOnTimerLocked() {
6052 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006053 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6054 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006055 }
6056 return mCameraTurnedOnTimer;
6057 }
6058
6059 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
6060 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6061 }
6062
6063 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
6064 if (mCameraTurnedOnTimer != null) {
6065 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6066 }
6067 }
6068
6069 public void noteResetCameraLocked(long elapsedRealtimeMs) {
6070 if (mCameraTurnedOnTimer != null) {
6071 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6072 }
6073 }
6074
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006075 public StopwatchTimer createForegroundActivityTimerLocked() {
6076 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006077 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6078 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006079 }
6080 return mForegroundActivityTimer;
6081 }
6082
Bookatzc8c44962017-05-11 12:12:54 -07006083 public DualTimer createAggregatedPartialWakelockTimerLocked() {
6084 if (mAggregatedPartialWakelockTimer == null) {
6085 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
6086 AGGREGATED_WAKE_TYPE_PARTIAL, null,
6087 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase);
6088 }
6089 return mAggregatedPartialWakelockTimer;
6090 }
6091
Bookatz867c0d72017-03-07 18:23:42 -08006092 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006093 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006094 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6095 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6096 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006097 }
6098 return mBluetoothScanTimer;
6099 }
6100
Bookatzb1f04f32017-05-19 13:57:32 -07006101 public DualTimer createBluetoothUnoptimizedScanTimerLocked() {
6102 if (mBluetoothUnoptimizedScanTimer == null) {
6103 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
6104 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
6105 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
6106 }
6107 return mBluetoothUnoptimizedScanTimer;
6108 }
6109
6110 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006111 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07006112 if (isUnoptimized) {
6113 createBluetoothUnoptimizedScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
6114 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006115 }
6116
Bookatz20e22f62017-07-11 16:49:17 -07006117 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006118 if (mBluetoothScanTimer != null) {
6119 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
6120 }
Bookatz20e22f62017-07-11 16:49:17 -07006121 if (isUnoptimized && mBluetoothUnoptimizedScanTimer != null) {
Bookatzb1f04f32017-05-19 13:57:32 -07006122 mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
6123 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006124 }
6125
6126 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
6127 if (mBluetoothScanTimer != null) {
6128 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6129 }
Bookatzb1f04f32017-05-19 13:57:32 -07006130 if (mBluetoothUnoptimizedScanTimer != null) {
6131 mBluetoothUnoptimizedScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6132 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006133 }
6134
Bookatz956f36bf2017-04-28 09:48:17 -07006135 public Counter createBluetoothScanResultCounterLocked() {
6136 if (mBluetoothScanResultCounter == null) {
6137 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase);
6138 }
6139 return mBluetoothScanResultCounter;
6140 }
6141
Bookatzb1f04f32017-05-19 13:57:32 -07006142 public Counter createBluetoothScanResultBgCounterLocked() {
6143 if (mBluetoothScanResultBgCounter == null) {
6144 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase);
6145 }
6146 return mBluetoothScanResultBgCounter;
6147 }
6148
Bookatz4ebc0642017-05-11 12:21:19 -07006149 public void noteBluetoothScanResultsLocked(int numNewResults) {
6150 createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
Bookatzb1f04f32017-05-19 13:57:32 -07006151 // Uses background timebase, so the count will only be incremented if uid in background.
6152 createBluetoothScanResultBgCounterLocked().addAtomic(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07006153 }
6154
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006155 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006156 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006157 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006158 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006159 }
6160
6161 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006162 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006163 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006164 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006165 }
6166 }
6167
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006168 public BatchTimer createVibratorOnTimerLocked() {
6169 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006170 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6171 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006172 }
6173 return mVibratorOnTimer;
6174 }
6175
6176 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08006177 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006178 }
6179
6180 public void noteVibratorOffLocked() {
6181 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006182 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006183 }
6184 }
6185
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006186 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006187 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006188 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006189 return 0;
6190 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006191 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006192 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006193
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006194 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006195 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006196 if (mFullWifiLockTimer == null) {
6197 return 0;
6198 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006199 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006200 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006201
6202 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006203 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006204 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006205 return 0;
6206 }
Bookatzaa4594a2017-03-24 12:39:56 -07006207 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006208 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006209
6210 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006211 public int getWifiScanCount(int which) {
6212 if (mWifiScanTimer == null) {
6213 return 0;
6214 }
Bookatzaa4594a2017-03-24 12:39:56 -07006215 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08006216 }
6217
6218 @Override
6219 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07006220 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006221 return 0;
6222 }
6223 return mWifiScanTimer.getSubTimer().getCountLocked(which);
6224 }
6225
6226 @Override
6227 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
6228 if (mWifiScanTimer == null) {
6229 return 0;
6230 }
6231 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07006232 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08006233 }
6234
6235 @Override
6236 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07006237 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006238 return 0;
6239 }
6240 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
6241 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07006242 }
6243
6244 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006245 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006246 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6247 if (mWifiBatchedScanTimer[csphBin] == null) {
6248 return 0;
6249 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006250 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006251 }
6252
6253 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006254 public int getWifiBatchedScanCount(int csphBin, int which) {
6255 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6256 if (mWifiBatchedScanTimer[csphBin] == null) {
6257 return 0;
6258 }
6259 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
6260 }
6261
6262 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006263 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006264 if (mWifiMulticastTimer == null) {
6265 return 0;
6266 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006267 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006268 }
6269
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006270 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006271 public Timer getAudioTurnedOnTimer() {
6272 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006273 }
6274
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006275 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006276 public Timer getVideoTurnedOnTimer() {
6277 return mVideoTurnedOnTimer;
6278 }
6279
6280 @Override
6281 public Timer getFlashlightTurnedOnTimer() {
6282 return mFlashlightTurnedOnTimer;
6283 }
6284
6285 @Override
6286 public Timer getCameraTurnedOnTimer() {
6287 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006288 }
6289
Dianne Hackborn617f8772009-03-31 15:04:46 -07006290 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006291 public Timer getForegroundActivityTimer() {
6292 return mForegroundActivityTimer;
6293 }
6294
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006295 @Override
6296 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07006297 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08006298 }
6299
6300 @Override
6301 public Timer getBluetoothScanBackgroundTimer() {
6302 if (mBluetoothScanTimer == null) {
6303 return null;
6304 }
6305 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006306 }
6307
Bookatz956f36bf2017-04-28 09:48:17 -07006308 @Override
Bookatzb1f04f32017-05-19 13:57:32 -07006309 public Timer getBluetoothUnoptimizedScanTimer() {
6310 return mBluetoothUnoptimizedScanTimer;
6311 }
6312
6313 @Override
6314 public Timer getBluetoothUnoptimizedScanBackgroundTimer() {
6315 if (mBluetoothUnoptimizedScanTimer == null) {
6316 return null;
6317 }
6318 return mBluetoothUnoptimizedScanTimer.getSubTimer();
6319 }
6320
6321 @Override
Bookatz956f36bf2017-04-28 09:48:17 -07006322 public Counter getBluetoothScanResultCounter() {
6323 return mBluetoothScanResultCounter;
6324 }
6325
Bookatzb1f04f32017-05-19 13:57:32 -07006326 @Override
6327 public Counter getBluetoothScanResultBgCounter() {
6328 return mBluetoothScanResultBgCounter;
6329 }
6330
Dianne Hackborn61659e52014-07-09 16:13:01 -07006331 void makeProcessState(int i, Parcel in) {
6332 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6333
6334 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006335 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6336 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006337 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006338 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6339 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006340 }
6341 }
6342
6343 @Override
6344 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6345 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6346 if (mProcessStateTimer[state] == null) {
6347 return 0;
6348 }
6349 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6350 }
6351
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006352 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006353 public Timer getProcessStateTimer(int state) {
6354 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6355 return mProcessStateTimer[state];
6356 }
6357
6358 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006359 public Timer getVibratorOnTimer() {
6360 return mVibratorOnTimer;
6361 }
6362
6363 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006364 public void noteUserActivityLocked(int type) {
6365 if (mUserActivityCounters == null) {
6366 initUserActivityLocked();
6367 }
Jeff Browndf693de2012-07-27 12:03:38 -07006368 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6369 mUserActivityCounters[type].stepAtomic();
6370 } else {
6371 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6372 new Throwable());
6373 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006374 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006375
Dianne Hackborn617f8772009-03-31 15:04:46 -07006376 @Override
6377 public boolean hasUserActivity() {
6378 return mUserActivityCounters != null;
6379 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006380
Dianne Hackborn617f8772009-03-31 15:04:46 -07006381 @Override
6382 public int getUserActivityCount(int type, int which) {
6383 if (mUserActivityCounters == null) {
6384 return 0;
6385 }
Evan Millarc64edde2009-04-18 12:26:32 -07006386 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006387 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006388
Robert Greenwalta029ea12013-09-25 16:38:12 -07006389 void makeWifiBatchedScanBin(int i, Parcel in) {
6390 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6391
Joe Onoratoabded112016-02-08 16:49:39 -08006392 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006393 if (collected == null) {
6394 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006395 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006396 }
6397 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006398 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6399 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006400 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006401 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6402 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006403 }
6404 }
6405
6406
Dianne Hackborn617f8772009-03-31 15:04:46 -07006407 void initUserActivityLocked() {
6408 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6409 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006410 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006411 }
6412 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006413
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006414 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6415 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006416 initNetworkActivityLocked();
6417 }
6418 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006419 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6420 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006421 } else {
6422 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6423 new Throwable());
6424 }
6425 }
6426
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006427 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6428 if (mNetworkByteActivityCounters == null) {
6429 initNetworkActivityLocked();
6430 }
6431 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6432 mMobileRadioActiveCount.addCountLocked(1);
6433 }
6434
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006435 @Override
6436 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006437 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006438 }
6439
6440 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006441 public long getNetworkActivityBytes(int type, int which) {
6442 if (mNetworkByteActivityCounters != null && type >= 0
6443 && type < mNetworkByteActivityCounters.length) {
6444 return mNetworkByteActivityCounters[type].getCountLocked(which);
6445 } else {
6446 return 0;
6447 }
6448 }
6449
6450 @Override
6451 public long getNetworkActivityPackets(int type, int which) {
6452 if (mNetworkPacketActivityCounters != null && type >= 0
6453 && type < mNetworkPacketActivityCounters.length) {
6454 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006455 } else {
6456 return 0;
6457 }
6458 }
6459
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006460 @Override
6461 public long getMobileRadioActiveTime(int which) {
6462 return mMobileRadioActiveTime != null
6463 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6464 }
6465
6466 @Override
6467 public int getMobileRadioActiveCount(int which) {
6468 return mMobileRadioActiveCount != null
6469 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6470 }
6471
Adam Lesinskie08af192015-03-25 16:42:59 -07006472 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006473 public long getUserCpuTimeUs(int which) {
6474 return mUserCpuTime.getCountLocked(which);
6475 }
6476
6477 @Override
6478 public long getSystemCpuTimeUs(int which) {
6479 return mSystemCpuTime.getCountLocked(which);
6480 }
6481
6482 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006483 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
6484 if (mCpuClusterSpeed != null) {
6485 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
6486 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
6487 if (cpuSpeeds != null) {
6488 if (step >= 0 && step < cpuSpeeds.length) {
6489 final LongSamplingCounter c = cpuSpeeds[step];
6490 if (c != null) {
6491 return c.getCountLocked(which);
6492 }
6493 }
6494 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006495 }
6496 }
6497 return 0;
6498 }
6499
Adam Lesinski5f056f62016-07-14 16:56:08 -07006500 public void noteMobileRadioApWakeupLocked() {
6501 if (mMobileRadioApWakeupCount == null) {
6502 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6503 }
6504 mMobileRadioApWakeupCount.addCountLocked(1);
6505 }
6506
6507 @Override
6508 public long getMobileRadioApWakeupCount(int which) {
6509 if (mMobileRadioApWakeupCount != null) {
6510 return mMobileRadioApWakeupCount.getCountLocked(which);
6511 }
6512 return 0;
6513 }
6514
6515 public void noteWifiRadioApWakeupLocked() {
6516 if (mWifiRadioApWakeupCount == null) {
6517 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6518 }
6519 mWifiRadioApWakeupCount.addCountLocked(1);
6520 }
6521
6522 @Override
6523 public long getWifiRadioApWakeupCount(int which) {
6524 if (mWifiRadioApWakeupCount != null) {
6525 return mWifiRadioApWakeupCount.getCountLocked(which);
6526 }
6527 return 0;
6528 }
6529
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006530 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006531 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6532 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006533 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006534 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6535 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006536 }
Joe Onoratoabded112016-02-08 16:49:39 -08006537 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6538 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006540
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006541 /**
6542 * Clear all stats for this uid. Returns true if the uid is completely
6543 * inactive so can be dropped.
6544 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006545 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
6546 public boolean reset() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006547 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006548
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006549 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006550 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006551 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006552 }
6553 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006554 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006555 active |= mFullWifiLockOut;
6556 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006557 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006558 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006559 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006560 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006561 if (mWifiBatchedScanTimer != null) {
6562 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6563 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006564 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006565 }
6566 }
6567 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6568 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006569 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006570 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006571 active |= mWifiMulticastEnabled;
6572 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006573
6574 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6575 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6576 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6577 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6578 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
Bookatzc8c44962017-05-11 12:12:54 -07006579 active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
Adam Lesinski5f056f62016-07-14 16:56:08 -07006580 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
Bookatzb1f04f32017-05-19 13:57:32 -07006581 active |= !resetTimerIfNotNull(mBluetoothUnoptimizedScanTimer, false);
Bookatz956f36bf2017-04-28 09:48:17 -07006582 if (mBluetoothScanResultCounter != null) {
6583 mBluetoothScanResultCounter.reset(false);
6584 }
Bookatzb1f04f32017-05-19 13:57:32 -07006585 if (mBluetoothScanResultBgCounter != null) {
6586 mBluetoothScanResultBgCounter.reset(false);
6587 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006588
Dianne Hackborn61659e52014-07-09 16:13:01 -07006589 if (mProcessStateTimer != null) {
6590 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6591 if (mProcessStateTimer[i] != null) {
6592 active |= !mProcessStateTimer[i].reset(false);
6593 }
6594 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006595 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006596 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006597 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006598 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006599 mVibratorOnTimer.detach();
6600 mVibratorOnTimer = null;
6601 } else {
6602 active = true;
6603 }
6604 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006605
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006606 if (mUserActivityCounters != null) {
6607 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6608 mUserActivityCounters[i].reset(false);
6609 }
6610 }
6611
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006612 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006613 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006614 mNetworkByteActivityCounters[i].reset(false);
6615 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006616 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006617 mMobileRadioActiveTime.reset(false);
6618 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006619 }
6620
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006621 if (mWifiControllerActivity != null) {
6622 mWifiControllerActivity.reset(false);
6623 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006624
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006625 if (mBluetoothControllerActivity != null) {
6626 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006627 }
6628
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006629 if (mModemControllerActivity != null) {
6630 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006631 }
6632
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006633 mUserCpuTime.reset(false);
6634 mSystemCpuTime.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006635
6636 if (mCpuClusterSpeed != null) {
6637 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
6638 if (speeds != null) {
6639 for (LongSamplingCounter speed : speeds) {
6640 if (speed != null) {
6641 speed.reset(false);
6642 }
6643 }
6644 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006645 }
6646 }
6647
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006648 if (mCpuFreqTimeMs != null) {
6649 mCpuFreqTimeMs.reset(false);
6650 }
6651 if (mScreenOffCpuFreqTimeMs != null) {
6652 mScreenOffCpuFreqTimeMs.reset(false);
6653 }
6654
Adam Lesinski5f056f62016-07-14 16:56:08 -07006655 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6656 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6657
Dianne Hackbornd953c532014-08-16 18:17:38 -07006658 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6659 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6660 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006661 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006662 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006663 } else {
6664 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006665 }
6666 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006667 mWakelockStats.cleanup();
Bookatz2bffb5b2017-04-13 11:59:33 -07006668 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006669 for (int is=syncStats.size()-1; is>=0; is--) {
Bookatz2bffb5b2017-04-13 11:59:33 -07006670 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006671 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006672 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006673 timer.detach();
6674 } else {
6675 active = true;
6676 }
6677 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006678 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07006679 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006680 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07006681 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006682 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006683 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006684 timer.detach();
6685 } else {
6686 active = true;
6687 }
6688 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006689 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006690 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6691 Sensor s = mSensorStats.valueAt(ise);
6692 if (s.reset()) {
6693 mSensorStats.removeAt(ise);
6694 } else {
6695 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006696 }
6697 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006698 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6699 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006700 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006701 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006702 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006703 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006704 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006705 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006706 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006707 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006708 } else {
6709 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006710 }
6711 }
6712 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006713 if (mPackageStats.size() > 0) {
6714 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6715 while (it.hasNext()) {
6716 Map.Entry<String, Pkg> pkgEntry = it.next();
6717 Pkg p = pkgEntry.getValue();
6718 p.detach();
6719 if (p.mServiceStats.size() > 0) {
6720 Iterator<Map.Entry<String, Pkg.Serv>> it2
6721 = p.mServiceStats.entrySet().iterator();
6722 while (it2.hasNext()) {
6723 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6724 servEntry.getValue().detach();
6725 }
6726 }
6727 }
6728 mPackageStats.clear();
6729 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006730
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006731 mLastStepUserTime = mLastStepSystemTime = 0;
6732 mCurStepUserTime = mCurStepSystemTime = 0;
6733
Bookatz867c0d72017-03-07 18:23:42 -08006734 mOnBatteryBackgroundTimeBase.reset(mBsi.mClocks.elapsedRealtime() * 1000,
6735 mBsi.mClocks.uptimeMillis() * 1000);
Bookatzc8c44962017-05-11 12:12:54 -07006736 mOnBatteryScreenOffBackgroundTimeBase.reset(mBsi.mClocks.elapsedRealtime() * 1000,
6737 mBsi.mClocks.uptimeMillis() * 1000);
Bookatz867c0d72017-03-07 18:23:42 -08006738
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006739 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006740 if (mWifiRunningTimer != null) {
6741 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006742 }
6743 if (mFullWifiLockTimer != null) {
6744 mFullWifiLockTimer.detach();
6745 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006746 if (mWifiScanTimer != null) {
6747 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006748 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006749 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6750 if (mWifiBatchedScanTimer[i] != null) {
6751 mWifiBatchedScanTimer[i].detach();
6752 }
6753 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006754 if (mWifiMulticastTimer != null) {
6755 mWifiMulticastTimer.detach();
6756 }
6757 if (mAudioTurnedOnTimer != null) {
6758 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006759 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006760 }
6761 if (mVideoTurnedOnTimer != null) {
6762 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006763 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006764 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006765 if (mFlashlightTurnedOnTimer != null) {
6766 mFlashlightTurnedOnTimer.detach();
6767 mFlashlightTurnedOnTimer = null;
6768 }
6769 if (mCameraTurnedOnTimer != null) {
6770 mCameraTurnedOnTimer.detach();
6771 mCameraTurnedOnTimer = null;
6772 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006773 if (mForegroundActivityTimer != null) {
6774 mForegroundActivityTimer.detach();
6775 mForegroundActivityTimer = null;
6776 }
Bookatzc8c44962017-05-11 12:12:54 -07006777 if (mAggregatedPartialWakelockTimer != null) {
6778 mAggregatedPartialWakelockTimer.detach();
6779 mAggregatedPartialWakelockTimer = null;
6780 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006781 if (mBluetoothScanTimer != null) {
6782 mBluetoothScanTimer.detach();
6783 mBluetoothScanTimer = null;
6784 }
Bookatzb1f04f32017-05-19 13:57:32 -07006785 if (mBluetoothUnoptimizedScanTimer != null) {
6786 mBluetoothUnoptimizedScanTimer.detach();
6787 mBluetoothUnoptimizedScanTimer = null;
6788 }
Bookatz956f36bf2017-04-28 09:48:17 -07006789 if (mBluetoothScanResultCounter != null) {
6790 mBluetoothScanResultCounter.detach();
6791 mBluetoothScanResultCounter = null;
6792 }
Bookatzb1f04f32017-05-19 13:57:32 -07006793 if (mBluetoothScanResultBgCounter != null) {
6794 mBluetoothScanResultBgCounter.detach();
6795 mBluetoothScanResultBgCounter = null;
6796 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006797 if (mUserActivityCounters != null) {
6798 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6799 mUserActivityCounters[i].detach();
6800 }
6801 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006802 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006803 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006804 mNetworkByteActivityCounters[i].detach();
6805 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006806 }
6807 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006808
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006809 if (mWifiControllerActivity != null) {
6810 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006811 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006812
6813 if (mBluetoothControllerActivity != null) {
6814 mBluetoothControllerActivity.detach();
6815 }
6816
6817 if (mModemControllerActivity != null) {
6818 mModemControllerActivity.detach();
6819 }
6820
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006821 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006822
6823 mUserCpuTime.detach();
6824 mSystemCpuTime.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006825
6826 if (mCpuClusterSpeed != null) {
6827 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6828 if (cpuSpeeds != null) {
6829 for (LongSamplingCounter c : cpuSpeeds) {
6830 if (c != null) {
6831 c.detach();
6832 }
6833 }
6834 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006835 }
6836 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006837
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006838 if (mCpuFreqTimeMs != null) {
6839 mCpuFreqTimeMs.detach();
6840 }
6841 if (mScreenOffCpuFreqTimeMs != null) {
6842 mScreenOffCpuFreqTimeMs.detach();
6843 }
6844
Adam Lesinski5f056f62016-07-14 16:56:08 -07006845 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6846 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006847 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006848
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006849 return !active;
6850 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006851
Bookatz867c0d72017-03-07 18:23:42 -08006852 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
6853 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatzc8c44962017-05-11 12:12:54 -07006854 mOnBatteryScreenOffBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08006855
Dianne Hackbornd953c532014-08-16 18:17:38 -07006856 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6857 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006858 out.writeInt(NW);
6859 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006860 out.writeString(wakeStats.keyAt(iw));
6861 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006862 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864
Bookatz2bffb5b2017-04-13 11:59:33 -07006865 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006866 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006867 out.writeInt(NS);
6868 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006869 out.writeString(syncStats.keyAt(is));
Bookatz2bffb5b2017-04-13 11:59:33 -07006870 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006871 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6872 }
6873
Bookatzaa4594a2017-03-24 12:39:56 -07006874 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006875 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006876 out.writeInt(NJ);
6877 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006878 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07006879 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006880 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6881 }
6882
Dianne Hackborn61659e52014-07-09 16:13:01 -07006883 int NSE = mSensorStats.size();
6884 out.writeInt(NSE);
6885 for (int ise=0; ise<NSE; ise++) {
6886 out.writeInt(mSensorStats.keyAt(ise));
6887 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006888 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 }
6890
Dianne Hackborn61659e52014-07-09 16:13:01 -07006891 int NP = mProcessStats.size();
6892 out.writeInt(NP);
6893 for (int ip=0; ip<NP; ip++) {
6894 out.writeString(mProcessStats.keyAt(ip));
6895 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 proc.writeToParcelLocked(out);
6897 }
6898
6899 out.writeInt(mPackageStats.size());
6900 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6901 out.writeString(pkgEntry.getKey());
6902 Uid.Pkg pkg = pkgEntry.getValue();
6903 pkg.writeToParcelLocked(out);
6904 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006905
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006906 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006907 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006908 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006909 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006910 out.writeInt(0);
6911 }
6912 if (mFullWifiLockTimer != null) {
6913 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006914 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006915 } else {
6916 out.writeInt(0);
6917 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006918 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006919 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006920 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006921 } else {
6922 out.writeInt(0);
6923 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006924 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6925 if (mWifiBatchedScanTimer[i] != null) {
6926 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006927 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006928 } else {
6929 out.writeInt(0);
6930 }
6931 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006932 if (mWifiMulticastTimer != null) {
6933 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006934 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006935 } else {
6936 out.writeInt(0);
6937 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006938
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006939 if (mAudioTurnedOnTimer != null) {
6940 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006941 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006942 } else {
6943 out.writeInt(0);
6944 }
6945 if (mVideoTurnedOnTimer != null) {
6946 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006947 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006948 } else {
6949 out.writeInt(0);
6950 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006951 if (mFlashlightTurnedOnTimer != null) {
6952 out.writeInt(1);
6953 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6954 } else {
6955 out.writeInt(0);
6956 }
6957 if (mCameraTurnedOnTimer != null) {
6958 out.writeInt(1);
6959 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6960 } else {
6961 out.writeInt(0);
6962 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006963 if (mForegroundActivityTimer != null) {
6964 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006965 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006966 } else {
6967 out.writeInt(0);
6968 }
Bookatzc8c44962017-05-11 12:12:54 -07006969 if (mAggregatedPartialWakelockTimer != null) {
6970 out.writeInt(1);
6971 mAggregatedPartialWakelockTimer.writeToParcel(out, elapsedRealtimeUs);
6972 } else {
6973 out.writeInt(0);
6974 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006975 if (mBluetoothScanTimer != null) {
6976 out.writeInt(1);
6977 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6978 } else {
6979 out.writeInt(0);
6980 }
Bookatzb1f04f32017-05-19 13:57:32 -07006981 if (mBluetoothUnoptimizedScanTimer != null) {
6982 out.writeInt(1);
6983 mBluetoothUnoptimizedScanTimer.writeToParcel(out, elapsedRealtimeUs);
6984 } else {
6985 out.writeInt(0);
6986 }
Bookatz956f36bf2017-04-28 09:48:17 -07006987 if (mBluetoothScanResultCounter != null) {
6988 out.writeInt(1);
6989 mBluetoothScanResultCounter.writeToParcel(out);
6990 } else {
6991 out.writeInt(0);
6992 }
Bookatzb1f04f32017-05-19 13:57:32 -07006993 if (mBluetoothScanResultBgCounter != null) {
6994 out.writeInt(1);
6995 mBluetoothScanResultBgCounter.writeToParcel(out);
6996 } else {
6997 out.writeInt(0);
6998 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006999 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7000 if (mProcessStateTimer[i] != null) {
7001 out.writeInt(1);
7002 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
7003 } else {
7004 out.writeInt(0);
7005 }
7006 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007007 if (mVibratorOnTimer != null) {
7008 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007009 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007010 } else {
7011 out.writeInt(0);
7012 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007013 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007014 out.writeInt(1);
7015 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
7016 mUserActivityCounters[i].writeToParcel(out);
7017 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007018 } else {
7019 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007020 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007021 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007022 out.writeInt(1);
7023 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007024 mNetworkByteActivityCounters[i].writeToParcel(out);
7025 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007026 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007027 mMobileRadioActiveTime.writeToParcel(out);
7028 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007029 } else {
7030 out.writeInt(0);
7031 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007032
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007033 if (mWifiControllerActivity != null) {
7034 out.writeInt(1);
7035 mWifiControllerActivity.writeToParcel(out, 0);
7036 } else {
7037 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007038 }
7039
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007040 if (mBluetoothControllerActivity != null) {
7041 out.writeInt(1);
7042 mBluetoothControllerActivity.writeToParcel(out, 0);
7043 } else {
7044 out.writeInt(0);
7045 }
7046
7047 if (mModemControllerActivity != null) {
7048 out.writeInt(1);
7049 mModemControllerActivity.writeToParcel(out, 0);
7050 } else {
7051 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007052 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007053
7054 mUserCpuTime.writeToParcel(out);
7055 mSystemCpuTime.writeToParcel(out);
7056
Adam Lesinski6832f392015-09-05 18:05:40 -07007057 if (mCpuClusterSpeed != null) {
7058 out.writeInt(1);
7059 out.writeInt(mCpuClusterSpeed.length);
7060 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
7061 if (cpuSpeeds != null) {
7062 out.writeInt(1);
7063 out.writeInt(cpuSpeeds.length);
7064 for (LongSamplingCounter c : cpuSpeeds) {
7065 if (c != null) {
7066 out.writeInt(1);
7067 c.writeToParcel(out);
7068 } else {
7069 out.writeInt(0);
7070 }
7071 }
7072 } else {
7073 out.writeInt(0);
7074 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007075 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007076 } else {
7077 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007078 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007079
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007080 LongSamplingCounterArray.writeToParcel(out, mCpuFreqTimeMs);
7081 LongSamplingCounterArray.writeToParcel(out, mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007082
Adam Lesinski5f056f62016-07-14 16:56:08 -07007083 if (mMobileRadioApWakeupCount != null) {
7084 out.writeInt(1);
7085 mMobileRadioApWakeupCount.writeToParcel(out);
7086 } else {
7087 out.writeInt(0);
7088 }
7089
7090 if (mWifiRadioApWakeupCount != null) {
7091 out.writeInt(1);
7092 mWifiRadioApWakeupCount.writeToParcel(out);
7093 } else {
7094 out.writeInt(0);
7095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 }
7097
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007098 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08007099 mOnBatteryBackgroundTimeBase.readFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -07007100 mOnBatteryScreenOffBackgroundTimeBase.readFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -08007101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 int numWakelocks = in.readInt();
7103 mWakelockStats.clear();
7104 for (int j = 0; j < numWakelocks; j++) {
7105 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007106 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Bookatz5b5ec322017-05-26 09:40:38 -07007107 wakelock.readFromParcelLocked(
7108 timeBase, screenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007109 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 }
7111
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007112 int numSyncs = in.readInt();
7113 mSyncStats.clear();
7114 for (int j = 0; j < numSyncs; j++) {
7115 String syncName = in.readString();
7116 if (in.readInt() != 0) {
Bookatz2bffb5b2017-04-13 11:59:33 -07007117 mSyncStats.add(syncName, new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
7118 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007119 }
7120 }
7121
7122 int numJobs = in.readInt();
7123 mJobStats.clear();
7124 for (int j = 0; j < numJobs; j++) {
7125 String jobName = in.readString();
7126 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07007127 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
7128 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007129 }
7130 }
7131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 int numSensors = in.readInt();
7133 mSensorStats.clear();
7134 for (int k = 0; k < numSensors; k++) {
7135 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007136 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08007137 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7138 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 mSensorStats.put(sensorNumber, sensor);
7140 }
7141
7142 int numProcs = in.readInt();
7143 mProcessStats.clear();
7144 for (int k = 0; k < numProcs; k++) {
7145 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007146 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007147 proc.readFromParcelLocked(in);
7148 mProcessStats.put(processName, proc);
7149 }
7150
7151 int numPkgs = in.readInt();
7152 mPackageStats.clear();
7153 for (int l = 0; l < numPkgs; l++) {
7154 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007155 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 pkg.readFromParcelLocked(in);
7157 mPackageStats.put(packageName, pkg);
7158 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007159
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007160 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007161 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007162 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
7163 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007164 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007165 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007166 }
7167 mFullWifiLockOut = false;
7168 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007169 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
7170 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007171 } else {
7172 mFullWifiLockTimer = null;
7173 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007174 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007175 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007176 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
7177 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7178 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007179 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007180 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007181 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007182 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
7183 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7184 if (in.readInt() != 0) {
7185 makeWifiBatchedScanBin(i, in);
7186 } else {
7187 mWifiBatchedScanTimer[i] = null;
7188 }
7189 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007190 mWifiMulticastEnabled = false;
7191 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007192 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
7193 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007194 } else {
7195 mWifiMulticastTimer = null;
7196 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007197 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007198 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
7199 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007200 } else {
7201 mAudioTurnedOnTimer = null;
7202 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007203 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007204 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
7205 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007206 } else {
7207 mVideoTurnedOnTimer = null;
7208 }
7209 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007210 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7211 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007212 } else {
7213 mFlashlightTurnedOnTimer = null;
7214 }
7215 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007216 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
7217 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007218 } else {
7219 mCameraTurnedOnTimer = null;
7220 }
7221 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007222 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7223 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007224 } else {
7225 mForegroundActivityTimer = null;
7226 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007227 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -07007228 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
7229 AGGREGATED_WAKE_TYPE_PARTIAL, null,
7230 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase,
7231 in);
7232 } else {
7233 mAggregatedPartialWakelockTimer = null;
7234 }
7235 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007236 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
7237 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
7238 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007239 } else {
7240 mBluetoothScanTimer = null;
7241 }
Bookatz956f36bf2017-04-28 09:48:17 -07007242 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -07007243 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
7244 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
7245 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in);
7246 } else {
7247 mBluetoothUnoptimizedScanTimer = null;
7248 }
7249 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -07007250 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
7251 } else {
7252 mBluetoothScanResultCounter = null;
7253 }
Bookatzb1f04f32017-05-19 13:57:32 -07007254 if (in.readInt() != 0) {
7255 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase, in);
7256 } else {
7257 mBluetoothScanResultBgCounter = null;
7258 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007259 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007260 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7261 if (in.readInt() != 0) {
7262 makeProcessState(i, in);
7263 } else {
7264 mProcessStateTimer[i] = null;
7265 }
7266 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007267 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007268 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
7269 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007270 } else {
7271 mVibratorOnTimer = null;
7272 }
7273 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007274 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
7275 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007276 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007277 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007278 } else {
7279 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07007280 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007281 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007282 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
7283 mNetworkPacketActivityCounters
7284 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007285 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007286 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007287 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007288 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007289 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007290 }
Joe Onoratoabded112016-02-08 16:49:39 -08007291 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7292 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007293 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007294 mNetworkByteActivityCounters = null;
7295 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007296 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007297
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007298 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007299 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007300 NUM_WIFI_TX_LEVELS, in);
7301 } else {
7302 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007303 }
7304
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007305 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007306 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007307 NUM_BT_TX_LEVELS, in);
7308 } else {
7309 mBluetoothControllerActivity = null;
7310 }
7311
7312 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007313 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007314 ModemActivityInfo.TX_POWER_LEVELS, in);
7315 } else {
7316 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007317 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007318
Joe Onoratoabded112016-02-08 16:49:39 -08007319 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7320 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007321
Adam Lesinski6832f392015-09-05 18:05:40 -07007322 if (in.readInt() != 0) {
7323 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007324 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007325 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007326 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007327
7328 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
7329 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
7330 if (in.readInt() != 0) {
7331 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007332 if (mBsi.mPowerProfile != null &&
7333 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007334 throw new ParcelFormatException("Incompatible number of cpu speeds");
7335 }
7336
7337 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
7338 mCpuClusterSpeed[cluster] = cpuSpeeds;
7339 for (int speed = 0; speed < numSpeeds; speed++) {
7340 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007341 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07007342 }
7343 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07007344 } else {
7345 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07007346 }
7347 }
7348 } else {
7349 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007350 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007351
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007352 mCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(in, mBsi.mOnBatteryTimeBase);
7353 mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(
7354 in, mBsi.mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007355
7356 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07007357 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7358 } else {
7359 mMobileRadioApWakeupCount = null;
7360 }
7361
7362 if (in.readInt() != 0) {
7363 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7364 } else {
7365 mWifiRadioApWakeupCount = null;
7366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 }
7368
7369 /**
7370 * The statistics associated with a particular wake lock.
7371 */
Joe Onoratoabded112016-02-08 16:49:39 -08007372 public static class Wakelock extends BatteryStats.Uid.Wakelock {
7373 /**
7374 * BatteryStatsImpl that we are associated with.
7375 */
7376 protected BatteryStatsImpl mBsi;
7377
7378 /**
7379 * BatteryStatsImpl that we are associated with.
7380 */
7381 protected Uid mUid;
7382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 /**
7384 * How long (in ms) this uid has been keeping the device partially awake.
Bookatz5b5ec322017-05-26 09:40:38 -07007385 * 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 -08007386 */
Bookatz5b5ec322017-05-26 09:40:38 -07007387 DualTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007388
7389 /**
7390 * How long (in ms) this uid has been keeping the device fully awake.
7391 */
Evan Millarc64edde2009-04-18 12:26:32 -07007392 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393
7394 /**
7395 * How long (in ms) this uid has had a window keeping the device awake.
7396 */
Evan Millarc64edde2009-04-18 12:26:32 -07007397 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398
7399 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007400 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07007401 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007402 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007403
Joe Onoratoabded112016-02-08 16:49:39 -08007404 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
7405 mBsi = bsi;
7406 mUid = uid;
7407 }
7408
Adam Lesinski9425fe22015-06-19 12:02:13 -07007409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7411 * proper timer pool from the given BatteryStatsImpl object.
7412 *
7413 * @param in the Parcel to be read from.
7414 * return a new Timer, or null.
7415 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007416 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7417 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 if (in.readInt() == 0) {
7419 return null;
7420 }
7421
Joe Onoratoabded112016-02-08 16:49:39 -08007422 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 }
7424
Joe Onorato92fd23f2016-07-25 11:18:42 -07007425 /**
7426 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7427 * proper timer pool from the given BatteryStatsImpl object.
7428 *
7429 * @param in the Parcel to be read from.
7430 * return a new Timer, or null.
7431 */
Bookatz5b5ec322017-05-26 09:40:38 -07007432 private DualTimer readDualTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
7433 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007434 if (in.readInt() == 0) {
7435 return null;
7436 }
7437
Bookatz5b5ec322017-05-26 09:40:38 -07007438 return new DualTimer(mBsi.mClocks, mUid, type, pool, timeBase, bgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007439 }
7440
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007441 boolean reset() {
7442 boolean wlactive = false;
7443 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007444 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007445 }
7446 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007447 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007448 }
7449 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007450 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007451 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007452 if (mTimerDraw != null) {
7453 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007454 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007455 if (!wlactive) {
7456 if (mTimerFull != null) {
7457 mTimerFull.detach();
7458 mTimerFull = null;
7459 }
7460 if (mTimerPartial != null) {
7461 mTimerPartial.detach();
7462 mTimerPartial = null;
7463 }
7464 if (mTimerWindow != null) {
7465 mTimerWindow.detach();
7466 mTimerWindow = null;
7467 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007468 if (mTimerDraw != null) {
7469 mTimerDraw.detach();
7470 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007471 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007472 }
7473 return !wlactive;
7474 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007475
Bookatz5b5ec322017-05-26 09:40:38 -07007476 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase,
7477 TimeBase screenOffBgTimeBase, Parcel in) {
7478 mTimerPartial = readDualTimerFromParcel(WAKE_TYPE_PARTIAL,
7479 mBsi.mPartialTimers, screenOffTimeBase, screenOffBgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007480 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7481 mBsi.mFullTimers, timeBase, in);
7482 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7483 mBsi.mWindowTimers, timeBase, in);
7484 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7485 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486 }
7487
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007488 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7489 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7490 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7491 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007492 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 }
7494
7495 @Override
7496 public Timer getWakeTime(int type) {
7497 switch (type) {
7498 case WAKE_TYPE_FULL: return mTimerFull;
7499 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7500 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007501 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 default: throw new IllegalArgumentException("type = " + type);
7503 }
7504 }
7505 }
7506
Joe Onoratoabded112016-02-08 16:49:39 -08007507 public static class Sensor extends BatteryStats.Uid.Sensor {
7508 /**
7509 * BatteryStatsImpl that we are associated with.
7510 */
7511 protected BatteryStatsImpl mBsi;
7512
7513 /**
Bookatz867c0d72017-03-07 18:23:42 -08007514 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007515 */
7516 protected Uid mUid;
7517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007519 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007520
Joe Onoratoabded112016-02-08 16:49:39 -08007521 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7522 mBsi = bsi;
7523 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 mHandle = handle;
7525 }
7526
Bookatz867c0d72017-03-07 18:23:42 -08007527 private DualTimer readTimersFromParcel(
7528 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007529 if (in.readInt() == 0) {
7530 return null;
7531 }
7532
Joe Onoratoabded112016-02-08 16:49:39 -08007533 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007535 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007536 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 }
Bookatz867c0d72017-03-07 18:23:42 -08007538 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007539 }
7540
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007541 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007542 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007543 mTimer = null;
7544 return true;
7545 }
7546 return false;
7547 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007548
Bookatz867c0d72017-03-07 18:23:42 -08007549 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7550 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007551 }
7552
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007553 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007554 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 }
7556
7557 @Override
7558 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07007559 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007561
7562 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007563 public Timer getSensorBackgroundTime() {
7564 if (mTimer == null) {
7565 return null;
7566 }
7567 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007568 }
7569
7570 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 public int getHandle() {
7572 return mHandle;
7573 }
7574 }
7575
7576 /**
7577 * The statistics associated with a particular process.
7578 */
Joe Onoratoabded112016-02-08 16:49:39 -08007579 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7580 /**
7581 * BatteryStatsImpl that we are associated with.
7582 */
7583 protected BatteryStatsImpl mBsi;
7584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007586 * The name of this process.
7587 */
7588 final String mName;
7589
7590 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007591 * Remains true until removed from the stats.
7592 */
7593 boolean mActive = true;
7594
7595 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007596 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007597 */
7598 long mUserTime;
7599
7600 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007601 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007602 */
7603 long mSystemTime;
7604
7605 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007606 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007607 */
7608 long mForegroundTime;
7609
7610 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007611 * Number of times the process has been started.
7612 */
7613 int mStarts;
7614
7615 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007616 * Number of times the process has crashed.
7617 */
7618 int mNumCrashes;
7619
7620 /**
7621 * Number of times the process has had an ANR.
7622 */
7623 int mNumAnrs;
7624
7625 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 * The amount of user time loaded from a previous save.
7627 */
7628 long mLoadedUserTime;
7629
7630 /**
7631 * The amount of system time loaded from a previous save.
7632 */
7633 long mLoadedSystemTime;
7634
7635 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007636 * The amount of foreground time loaded from a previous save.
7637 */
7638 long mLoadedForegroundTime;
7639
7640 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007641 * The number of times the process has started from a previous save.
7642 */
7643 int mLoadedStarts;
7644
7645 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007646 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007647 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007648 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649
7650 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007651 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007652 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007653 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007654
7655 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 * The amount of user time when last unplugged.
7657 */
7658 long mUnpluggedUserTime;
7659
7660 /**
7661 * The amount of system time when last unplugged.
7662 */
7663 long mUnpluggedSystemTime;
7664
7665 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007666 * The amount of foreground time since unplugged.
7667 */
7668 long mUnpluggedForegroundTime;
7669
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007670 /**
7671 * The number of times the process has started before unplugged.
7672 */
7673 int mUnpluggedStarts;
7674
Dianne Hackborn61659e52014-07-09 16:13:01 -07007675 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007676 * Number of times the process has crashed before unplugged.
7677 */
7678 int mUnpluggedNumCrashes;
7679
7680 /**
7681 * Number of times the process has had an ANR before unplugged.
7682 */
7683 int mUnpluggedNumAnrs;
7684
Dianne Hackborn287952c2010-09-22 22:34:31 -07007685 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007686
Joe Onoratoabded112016-02-08 16:49:39 -08007687 public Proc(BatteryStatsImpl bsi, String name) {
7688 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007689 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007690 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007692
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007693 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 mUnpluggedUserTime = mUserTime;
7695 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007696 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007697 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007698 mUnpluggedNumCrashes = mNumCrashes;
7699 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007700 }
7701
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007702 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007704
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007705 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007706 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007707 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007708 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007709
Dianne Hackborn287952c2010-09-22 22:34:31 -07007710 public int countExcessivePowers() {
7711 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007712 }
7713
Dianne Hackborn287952c2010-09-22 22:34:31 -07007714 public ExcessivePower getExcessivePower(int i) {
7715 if (mExcessivePower != null) {
7716 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007717 }
7718 return null;
7719 }
7720
7721 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007722 if (mExcessivePower == null) {
7723 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007724 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007725 ExcessivePower ew = new ExcessivePower();
7726 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007727 ew.overTime = overTime;
7728 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007729 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007730 }
7731
Dianne Hackborn287952c2010-09-22 22:34:31 -07007732 public void addExcessiveCpu(long overTime, long usedTime) {
7733 if (mExcessivePower == null) {
7734 mExcessivePower = new ArrayList<ExcessivePower>();
7735 }
7736 ExcessivePower ew = new ExcessivePower();
7737 ew.type = ExcessivePower.TYPE_CPU;
7738 ew.overTime = overTime;
7739 ew.usedTime = usedTime;
7740 mExcessivePower.add(ew);
7741 }
7742
7743 void writeExcessivePowerToParcelLocked(Parcel out) {
7744 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007745 out.writeInt(0);
7746 return;
7747 }
7748
Dianne Hackborn287952c2010-09-22 22:34:31 -07007749 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007750 out.writeInt(N);
7751 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007752 ExcessivePower ew = mExcessivePower.get(i);
7753 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007754 out.writeLong(ew.overTime);
7755 out.writeLong(ew.usedTime);
7756 }
7757 }
7758
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007759 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007760 final int N = in.readInt();
7761 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007762 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007763 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007764 }
7765
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007766 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007767 throw new ParcelFormatException(
7768 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007769 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007770
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007771 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007772 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007773 ExcessivePower ew = new ExcessivePower();
7774 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007775 ew.overTime = in.readLong();
7776 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007777 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007778 }
7779 }
7780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007781 void writeToParcelLocked(Parcel out) {
7782 out.writeLong(mUserTime);
7783 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007784 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007786 out.writeInt(mNumCrashes);
7787 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 out.writeLong(mLoadedUserTime);
7789 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007790 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007792 out.writeInt(mLoadedNumCrashes);
7793 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 out.writeLong(mUnpluggedUserTime);
7795 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007796 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007798 out.writeInt(mUnpluggedNumCrashes);
7799 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007800 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 }
7802
7803 void readFromParcelLocked(Parcel in) {
7804 mUserTime = in.readLong();
7805 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007806 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007808 mNumCrashes = in.readInt();
7809 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 mLoadedUserTime = in.readLong();
7811 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007812 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007814 mLoadedNumCrashes = in.readInt();
7815 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007816 mUnpluggedUserTime = in.readLong();
7817 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007818 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007820 mUnpluggedNumCrashes = in.readInt();
7821 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007822 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 }
7824
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007825 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 mUserTime += utime;
7827 mSystemTime += stime;
7828 }
7829
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007830 public void addForegroundTimeLocked(long ttime) {
7831 mForegroundTime += ttime;
7832 }
7833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 public void incStartsLocked() {
7835 mStarts++;
7836 }
7837
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007838 public void incNumCrashesLocked() {
7839 mNumCrashes++;
7840 }
7841
7842 public void incNumAnrsLocked() {
7843 mNumAnrs++;
7844 }
7845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007847 public boolean isActive() {
7848 return mActive;
7849 }
7850
7851 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007853 long val = mUserTime;
7854 if (which == STATS_CURRENT) {
7855 val -= mLoadedUserTime;
7856 } else if (which == STATS_SINCE_UNPLUGGED) {
7857 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 }
7859 return val;
7860 }
7861
7862 @Override
7863 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007864 long val = mSystemTime;
7865 if (which == STATS_CURRENT) {
7866 val -= mLoadedSystemTime;
7867 } else if (which == STATS_SINCE_UNPLUGGED) {
7868 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 }
7870 return val;
7871 }
7872
7873 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007874 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007875 long val = mForegroundTime;
7876 if (which == STATS_CURRENT) {
7877 val -= mLoadedForegroundTime;
7878 } else if (which == STATS_SINCE_UNPLUGGED) {
7879 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007880 }
7881 return val;
7882 }
7883
7884 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007886 int val = mStarts;
7887 if (which == STATS_CURRENT) {
7888 val -= mLoadedStarts;
7889 } else if (which == STATS_SINCE_UNPLUGGED) {
7890 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 }
7892 return val;
7893 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007894
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007895 @Override
7896 public int getNumCrashes(int which) {
7897 int val = mNumCrashes;
7898 if (which == STATS_CURRENT) {
7899 val -= mLoadedNumCrashes;
7900 } else if (which == STATS_SINCE_UNPLUGGED) {
7901 val -= mUnpluggedNumCrashes;
7902 }
7903 return val;
7904 }
7905
7906 @Override
7907 public int getNumAnrs(int which) {
7908 int val = mNumAnrs;
7909 if (which == STATS_CURRENT) {
7910 val -= mLoadedNumAnrs;
7911 } else if (which == STATS_SINCE_UNPLUGGED) {
7912 val -= mUnpluggedNumAnrs;
7913 }
7914 return val;
7915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 }
7917
7918 /**
7919 * The statistics associated with a particular package.
7920 */
Joe Onoratoabded112016-02-08 16:49:39 -08007921 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7922 /**
7923 * BatteryStatsImpl that we are associated with.
7924 */
7925 protected BatteryStatsImpl mBsi;
7926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007927 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007928 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007930 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931
7932 /**
7933 * The statics we have collected for this package's services.
7934 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007935 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936
Joe Onoratoabded112016-02-08 16:49:39 -08007937 public Pkg(BatteryStatsImpl bsi) {
7938 mBsi = bsi;
7939 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007940 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007941
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007942 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 }
7944
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007945 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007947
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007948 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007949 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007950 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007953 int numWA = in.readInt();
7954 mWakeupAlarms.clear();
7955 for (int i=0; i<numWA; i++) {
7956 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007957 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959
7960 int numServs = in.readInt();
7961 mServiceStats.clear();
7962 for (int m = 0; m < numServs; m++) {
7963 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007964 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 mServiceStats.put(serviceName, serv);
7966
7967 serv.readFromParcelLocked(in);
7968 }
7969 }
7970
7971 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007972 int numWA = mWakeupAlarms.size();
7973 out.writeInt(numWA);
7974 for (int i=0; i<numWA; i++) {
7975 out.writeString(mWakeupAlarms.keyAt(i));
7976 mWakeupAlarms.valueAt(i).writeToParcel(out);
7977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007979 final int NS = mServiceStats.size();
7980 out.writeInt(NS);
7981 for (int i=0; i<NS; i++) {
7982 out.writeString(mServiceStats.keyAt(i));
7983 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 serv.writeToParcelLocked(out);
7985 }
7986 }
7987
7988 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007989 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7990 return mWakeupAlarms;
7991 }
7992
7993 public void noteWakeupAlarmLocked(String tag) {
7994 Counter c = mWakeupAlarms.get(tag);
7995 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007996 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007997 mWakeupAlarms.put(tag, c);
7998 }
7999 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 }
8001
8002 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008003 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
8004 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008005 }
8006
8007 /**
8008 * The statistics associated with a particular service.
8009 */
Joe Onoratoabded112016-02-08 16:49:39 -08008010 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
8011 /**
8012 * BatteryStatsImpl that we are associated with.
8013 */
8014 protected BatteryStatsImpl mBsi;
8015
8016 /**
8017 * The android package in which this service resides.
8018 */
8019 protected Pkg mPkg;
8020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 /**
8022 * Total time (ms in battery uptime) the service has been left started.
8023 */
Joe Onoratoabded112016-02-08 16:49:39 -08008024 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025
8026 /**
8027 * If service has been started and not yet stopped, this is
8028 * when it was started.
8029 */
Joe Onoratoabded112016-02-08 16:49:39 -08008030 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031
8032 /**
8033 * True if we are currently running.
8034 */
Joe Onoratoabded112016-02-08 16:49:39 -08008035 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008036
8037 /**
8038 * Total number of times startService() has been called.
8039 */
Joe Onoratoabded112016-02-08 16:49:39 -08008040 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041
8042 /**
8043 * Total time (ms in battery uptime) the service has been left launched.
8044 */
Joe Onoratoabded112016-02-08 16:49:39 -08008045 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046
8047 /**
8048 * If service has been launched and not yet exited, this is
8049 * when it was launched (ms in battery uptime).
8050 */
Joe Onoratoabded112016-02-08 16:49:39 -08008051 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052
8053 /**
8054 * True if we are currently launched.
8055 */
Joe Onoratoabded112016-02-08 16:49:39 -08008056 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057
8058 /**
8059 * Total number times the service has been launched.
8060 */
Joe Onoratoabded112016-02-08 16:49:39 -08008061 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008062
8063 /**
8064 * The amount of time spent started loaded from a previous save
8065 * (ms in battery uptime).
8066 */
Joe Onoratoabded112016-02-08 16:49:39 -08008067 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068
8069 /**
8070 * The number of starts loaded from a previous save.
8071 */
Joe Onoratoabded112016-02-08 16:49:39 -08008072 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073
8074 /**
8075 * The number of launches loaded from a previous save.
8076 */
Joe Onoratoabded112016-02-08 16:49:39 -08008077 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078
8079 /**
8080 * The amount of time spent started as of the last run (ms
8081 * in battery uptime).
8082 */
Joe Onoratoabded112016-02-08 16:49:39 -08008083 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084
8085 /**
8086 * The number of starts as of the last run.
8087 */
Joe Onoratoabded112016-02-08 16:49:39 -08008088 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089
8090 /**
8091 * The number of launches as of the last run.
8092 */
Joe Onoratoabded112016-02-08 16:49:39 -08008093 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094
8095 /**
8096 * The amount of time spent started when last unplugged (ms
8097 * in battery uptime).
8098 */
Joe Onoratoabded112016-02-08 16:49:39 -08008099 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100
8101 /**
8102 * The number of starts when last unplugged.
8103 */
Joe Onoratoabded112016-02-08 16:49:39 -08008104 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105
8106 /**
8107 * The number of launches when last unplugged.
8108 */
Joe Onoratoabded112016-02-08 16:49:39 -08008109 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110
Joe Onoratoabded112016-02-08 16:49:39 -08008111 /**
8112 * Construct a Serv. Also adds it to the on-battery time base as a listener.
8113 */
8114 public Serv(BatteryStatsImpl bsi) {
8115 mBsi = bsi;
8116 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008118
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008119 public void onTimeStarted(long elapsedRealtime, long baseUptime,
8120 long baseRealtime) {
8121 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 mUnpluggedStarts = mStarts;
8123 mUnpluggedLaunches = mLaunches;
8124 }
8125
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008126 public void onTimeStopped(long elapsedRealtime, long baseUptime,
8127 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008129
Joe Onoratoabded112016-02-08 16:49:39 -08008130 /**
8131 * Remove this Serv as a listener from the time base.
8132 */
8133 public void detach() {
8134 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008135 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008136
Joe Onoratoabded112016-02-08 16:49:39 -08008137 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008138 mStartTime = in.readLong();
8139 mRunningSince = in.readLong();
8140 mRunning = in.readInt() != 0;
8141 mStarts = in.readInt();
8142 mLaunchedTime = in.readLong();
8143 mLaunchedSince = in.readLong();
8144 mLaunched = in.readInt() != 0;
8145 mLaunches = in.readInt();
8146 mLoadedStartTime = in.readLong();
8147 mLoadedStarts = in.readInt();
8148 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008149 mLastStartTime = 0;
8150 mLastStarts = 0;
8151 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 mUnpluggedStartTime = in.readLong();
8153 mUnpluggedStarts = in.readInt();
8154 mUnpluggedLaunches = in.readInt();
8155 }
8156
Joe Onoratoabded112016-02-08 16:49:39 -08008157 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 out.writeLong(mStartTime);
8159 out.writeLong(mRunningSince);
8160 out.writeInt(mRunning ? 1 : 0);
8161 out.writeInt(mStarts);
8162 out.writeLong(mLaunchedTime);
8163 out.writeLong(mLaunchedSince);
8164 out.writeInt(mLaunched ? 1 : 0);
8165 out.writeInt(mLaunches);
8166 out.writeLong(mLoadedStartTime);
8167 out.writeInt(mLoadedStarts);
8168 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 out.writeLong(mUnpluggedStartTime);
8170 out.writeInt(mUnpluggedStarts);
8171 out.writeInt(mUnpluggedLaunches);
8172 }
8173
Joe Onoratoabded112016-02-08 16:49:39 -08008174 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 if (!mLaunched) return mLaunchedTime;
8176 return mLaunchedTime + batteryUptime - mLaunchedSince;
8177 }
8178
Joe Onoratoabded112016-02-08 16:49:39 -08008179 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 if (!mRunning) return mStartTime;
8181 return mStartTime + batteryUptime - mRunningSince;
8182 }
8183
8184 public void startLaunchedLocked() {
8185 if (!mLaunched) {
8186 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08008187 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008188 mLaunched = true;
8189 }
8190 }
8191
8192 public void stopLaunchedLocked() {
8193 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08008194 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008195 if (time > 0) {
8196 mLaunchedTime += time;
8197 } else {
8198 mLaunches--;
8199 }
8200 mLaunched = false;
8201 }
8202 }
8203
8204 public void startRunningLocked() {
8205 if (!mRunning) {
8206 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08008207 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008208 mRunning = true;
8209 }
8210 }
8211
8212 public void stopRunningLocked() {
8213 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08008214 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 if (time > 0) {
8216 mStartTime += time;
8217 } else {
8218 mStarts--;
8219 }
8220 mRunning = false;
8221 }
8222 }
8223
8224 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008225 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008226 }
8227
8228 @Override
8229 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008230 int val = mLaunches;
8231 if (which == STATS_CURRENT) {
8232 val -= mLoadedLaunches;
8233 } else if (which == STATS_SINCE_UNPLUGGED) {
8234 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 return val;
8237 }
8238
8239 @Override
8240 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008241 long val = getStartTimeToNowLocked(now);
8242 if (which == STATS_CURRENT) {
8243 val -= mLoadedStartTime;
8244 } else if (which == STATS_SINCE_UNPLUGGED) {
8245 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008247 return val;
8248 }
8249
8250 @Override
8251 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008252 int val = mStarts;
8253 if (which == STATS_CURRENT) {
8254 val -= mLoadedStarts;
8255 } else if (which == STATS_SINCE_UNPLUGGED) {
8256 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 }
8258
8259 return val;
8260 }
8261 }
8262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08008264 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008265 }
8266 }
8267
8268 /**
8269 * Retrieve the statistics object for a particular process, creating
8270 * if needed.
8271 */
8272 public Proc getProcessStatsLocked(String name) {
8273 Proc ps = mProcessStats.get(name);
8274 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008275 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 mProcessStats.put(name, ps);
8277 }
8278
8279 return ps;
8280 }
8281
Dianne Hackborna8d10942015-11-19 17:55:19 -08008282 public void updateUidProcessStateLocked(int procState) {
8283 int uidRunningState;
8284 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
8285 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
8286 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
8287 uidRunningState = PROCESS_STATE_TOP;
8288 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
8289 // Persistent and other foreground states go here.
8290 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
8291 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
8292 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
8293 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
8294 // Persistent and other foreground states go here.
8295 uidRunningState = PROCESS_STATE_FOREGROUND;
8296 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
8297 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008298 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08008299 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008300 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008301
Dianne Hackborna8d10942015-11-19 17:55:19 -08008302 if (mProcessState == uidRunningState) return;
8303
Bookatz867c0d72017-03-07 18:23:42 -08008304 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
8305 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08008306
8307 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
Bookatz867c0d72017-03-07 18:23:42 -08008308 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborna8d10942015-11-19 17:55:19 -08008309 }
8310 mProcessState = uidRunningState;
8311 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8312 if (mProcessStateTimer[uidRunningState] == null) {
8313 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008314 }
Bookatz867c0d72017-03-07 18:23:42 -08008315 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008316 }
Bookatz867c0d72017-03-07 18:23:42 -08008317
Bookatzc8c44962017-05-11 12:12:54 -07008318 updateOnBatteryBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
8319 updateOnBatteryScreenOffBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
Bookatz867c0d72017-03-07 18:23:42 -08008320 }
8321
Bookatzc8c44962017-05-11 12:12:54 -07008322 /** Whether to consider Uid to be in the background for background timebase purposes. */
8323 public boolean isInBackground() {
Bookatz867c0d72017-03-07 18:23:42 -08008324 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
8325 // also considered to be 'background' for our purposes, because it's not foreground.
Bookatzc8c44962017-05-11 12:12:54 -07008326 return mProcessState >= PROCESS_STATE_BACKGROUND;
8327 }
8328
8329 public boolean updateOnBatteryBgTimeBase(long uptimeUs, long realtimeUs) {
8330 boolean on = mBsi.mOnBatteryTimeBase.isRunning() && isInBackground();
8331 return mOnBatteryBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
8332 }
8333
8334 public boolean updateOnBatteryScreenOffBgTimeBase(long uptimeUs, long realtimeUs) {
8335 boolean on = mBsi.mOnBatteryScreenOffTimeBase.isRunning() && isInBackground();
8336 return mOnBatteryScreenOffBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008337 }
8338
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008339 public SparseArray<? extends Pid> getPidStats() {
8340 return mPids;
8341 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008342
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008343 public Pid getPidStatsLocked(int pid) {
8344 Pid p = mPids.get(pid);
8345 if (p == null) {
8346 p = new Pid();
8347 mPids.put(pid, p);
8348 }
8349 return p;
8350 }
8351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008352 /**
8353 * Retrieve the statistics object for a particular service, creating
8354 * if needed.
8355 */
8356 public Pkg getPackageStatsLocked(String name) {
8357 Pkg ps = mPackageStats.get(name);
8358 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008359 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 mPackageStats.put(name, ps);
8361 }
8362
8363 return ps;
8364 }
8365
8366 /**
8367 * Retrieve the statistics object for a particular service, creating
8368 * if needed.
8369 */
8370 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
8371 Pkg ps = getPackageStatsLocked(pkg);
8372 Pkg.Serv ss = ps.mServiceStats.get(serv);
8373 if (ss == null) {
8374 ss = ps.newServiceStatsLocked();
8375 ps.mServiceStats.put(serv, ss);
8376 }
8377
8378 return ss;
8379 }
8380
Dianne Hackbornd953c532014-08-16 18:17:38 -07008381 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008382 DualTimer timer = mSyncStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008383 timer.readSummaryFromParcelLocked(in);
8384 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008385 }
8386
Dianne Hackbornd953c532014-08-16 18:17:38 -07008387 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07008388 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008389 timer.readSummaryFromParcelLocked(in);
8390 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008391 }
8392
Dianne Hackbornd953c532014-08-16 18:17:38 -07008393 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008394 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008395 mWakelockStats.add(wlName, wl);
8396 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008397 getWakelockTimerLocked(wl, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008399 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008400 getWakelockTimerLocked(wl, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008401 }
8402 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008403 getWakelockTimerLocked(wl, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008404 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008405 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008406 getWakelockTimerLocked(wl, WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 }
8409
Bookatz867c0d72017-03-07 18:23:42 -08008410 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 Sensor se = mSensorStats.get(sensor);
8412 if (se == null) {
8413 if (!create) {
8414 return null;
8415 }
Joe Onoratoabded112016-02-08 16:49:39 -08008416 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 mSensorStats.put(sensor, se);
8418 }
Bookatz867c0d72017-03-07 18:23:42 -08008419 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 if (t != null) {
8421 return t;
8422 }
Joe Onoratoabded112016-02-08 16:49:39 -08008423 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008424 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008425 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008426 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 }
Bookatz867c0d72017-03-07 18:23:42 -08008428 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8429 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 se.mTimer = t;
8431 return t;
8432 }
8433
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008434 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008435 DualTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008436 if (t != null) {
8437 t.startRunningLocked(elapsedRealtimeMs);
8438 }
8439 }
8440
8441 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008442 DualTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008443 if (t != null) {
8444 t.stopRunningLocked(elapsedRealtimeMs);
8445 }
8446 }
8447
8448 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008449 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008450 if (t != null) {
8451 t.startRunningLocked(elapsedRealtimeMs);
8452 }
8453 }
8454
8455 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008456 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008457 if (t != null) {
8458 t.stopRunningLocked(elapsedRealtimeMs);
8459 }
8460 }
8461
Bookatz5b5ec322017-05-26 09:40:38 -07008462 public StopwatchTimer getWakelockTimerLocked(Wakelock wl, int type) {
8463 if (wl == null) {
8464 return null;
8465 }
8466 switch (type) {
8467 case WAKE_TYPE_PARTIAL: {
8468 DualTimer t = wl.mTimerPartial;
8469 if (t == null) {
8470 t = new DualTimer(mBsi.mClocks, this, WAKE_TYPE_PARTIAL,
8471 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase,
8472 mOnBatteryScreenOffBackgroundTimeBase);
8473 wl.mTimerPartial = t;
8474 }
8475 return t;
8476 }
8477 case WAKE_TYPE_FULL: {
8478 StopwatchTimer t = wl.mTimerFull;
8479 if (t == null) {
8480 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_FULL,
8481 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
8482 wl.mTimerFull = t;
8483 }
8484 return t;
8485 }
8486 case WAKE_TYPE_WINDOW: {
8487 StopwatchTimer t = wl.mTimerWindow;
8488 if (t == null) {
8489 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_WINDOW,
8490 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
8491 wl.mTimerWindow = t;
8492 }
8493 return t;
8494 }
8495 case WAKE_TYPE_DRAW: {
8496 StopwatchTimer t = wl.mTimerDraw;
8497 if (t == null) {
8498 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_DRAW,
8499 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
8500 wl.mTimerDraw = t;
8501 }
8502 return t;
8503 }
8504 default:
8505 throw new IllegalArgumentException("type=" + type);
8506 }
8507 }
8508
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008509 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008510 Wakelock wl = mWakelockStats.startObject(name);
8511 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008512 getWakelockTimerLocked(wl, type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513 }
Bookatzc8c44962017-05-11 12:12:54 -07008514 if (type == WAKE_TYPE_PARTIAL) {
8515 createAggregatedPartialWakelockTimerLocked().startRunningLocked(elapsedRealtimeMs);
8516 if (pid >= 0) {
8517 Pid p = getPidStatsLocked(pid);
8518 if (p.mWakeNesting++ == 0) {
8519 p.mWakeStartMs = elapsedRealtimeMs;
8520 }
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008521 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 }
8524
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008525 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008526 Wakelock wl = mWakelockStats.stopObject(name);
8527 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008528 getWakelockTimerLocked(wl, type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 }
Bookatzc8c44962017-05-11 12:12:54 -07008530 if (type == WAKE_TYPE_PARTIAL) {
8531 if (mAggregatedPartialWakelockTimer != null) {
8532 mAggregatedPartialWakelockTimer.stopRunningLocked(elapsedRealtimeMs);
8533 }
8534 if (pid >= 0) {
8535 Pid p = mPids.get(pid);
8536 if (p != null && p.mWakeNesting > 0) {
8537 if (p.mWakeNesting-- == 1) {
8538 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8539 p.mWakeStartMs = 0;
8540 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008541 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008542 }
8543 }
8544 }
8545
8546 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
8547 Proc p = getProcessStatsLocked(proc);
8548 if (p != null) {
8549 p.addExcessiveWake(overTime, usedTime);
8550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008552
Dianne Hackborn287952c2010-09-22 22:34:31 -07008553 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8554 Proc p = getProcessStatsLocked(proc);
8555 if (p != null) {
8556 p.addExcessiveCpu(overTime, usedTime);
8557 }
8558 }
8559
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008560 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008561 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008562 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008563 }
8564
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008565 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008567 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008569 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008572
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008573 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008574 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008576
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008577 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008578 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 }
8580
8581 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008582 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 }
8584 }
8585
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008586 public long[] getCpuFreqs() {
8587 return mCpuFreqs;
8588 }
8589
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008590 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008591 this(new SystemClocks(), systemDir, handler, externalSync, null);
8592 }
8593
8594 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
8595 PlatformIdleStateCallback cb) {
8596 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08008597 }
8598
8599 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008600 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08008601 init(clocks);
8602
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008603 if (systemDir != null) {
8604 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
8605 new File(systemDir, "batterystats.bin.tmp"));
8606 } else {
8607 mFile = null;
8608 }
8609 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008610 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008611 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08008612 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08008614 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008615 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008616 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008617 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008618 }
Joe Onoratoabded112016-02-08 16:49:39 -08008619 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8620 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8621 mOnBatteryTimeBase);
8622 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8623 mOnBatteryTimeBase);
8624 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8625 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8626 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8627 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8628 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8629 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8630 mOnBatteryTimeBase);
8631 }
8632 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8633 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008634 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008635 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008636 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008637 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008638 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008639 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8640 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008641 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008642 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8643 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8644 NUM_BT_TX_LEVELS);
8645 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8646 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008647 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8648 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8649 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008650 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008651 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8652 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008653 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8654 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008655 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008656 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008657 mOnBatteryTimeBase);
8658 }
Joe Onoratoabded112016-02-08 16:49:39 -08008659 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8660 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8661 mOnBatteryTimeBase);
8662 }
8663 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8664 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8665 mOnBatteryTimeBase);
8666 }
8667 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8668 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8669 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8670 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8671 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008672 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8673 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008675 long uptime = mClocks.uptimeMillis() * 1000;
8676 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008677 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008678 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008679 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008680 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008681 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008682 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008683 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008684 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008685 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008686 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008687 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 }
8689
8690 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008691 this(new SystemClocks(), p);
8692 }
8693
8694 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8695 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008696 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008697 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008698 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008699 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008700 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008701 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008703 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008704 }
8705
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008706 public void setPowerProfileLocked(PowerProfile profile) {
8707 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008708
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008709 // We need to initialize the KernelCpuSpeedReaders to read from
8710 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8711 // information.
8712 final int numClusters = mPowerProfile.getNumCpuClusters();
8713 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8714 int firstCpuOfCluster = 0;
8715 for (int i = 0; i < numClusters; i++) {
8716 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8717 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8718 numSpeedSteps);
8719 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8720 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008721
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008722 if (mEstimatedBatteryCapacity == -1) {
8723 // Initialize the estimated battery capacity to a known preset one.
8724 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
Adam Lesinskie08af192015-03-25 16:42:59 -07008725 }
8726 }
8727
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008728 public void setCallback(BatteryCallback cb) {
8729 mCallback = cb;
8730 }
8731
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008732 public void setRadioScanningTimeoutLocked(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07008733 if (mPhoneSignalScanningTimer != null) {
8734 mPhoneSignalScanningTimer.setTimeout(timeout);
8735 }
8736 }
8737
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008738 public void updateDailyDeadlineLocked() {
8739 // Get the current time.
8740 long currentTime = mDailyStartTime = System.currentTimeMillis();
8741 Calendar calDeadline = Calendar.getInstance();
8742 calDeadline.setTimeInMillis(currentTime);
8743
8744 // Move time up to the next day, ranging from 1am to 3pm.
8745 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8746 calDeadline.set(Calendar.MILLISECOND, 0);
8747 calDeadline.set(Calendar.SECOND, 0);
8748 calDeadline.set(Calendar.MINUTE, 0);
8749 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8750 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8751 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8752 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8753 }
8754
8755 public void recordDailyStatsIfNeededLocked(boolean settled) {
8756 long currentTime = System.currentTimeMillis();
8757 if (currentTime >= mNextMaxDailyDeadline) {
8758 recordDailyStatsLocked();
8759 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8760 recordDailyStatsLocked();
8761 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8762 recordDailyStatsLocked();
8763 }
8764 }
8765
8766 public void recordDailyStatsLocked() {
8767 DailyItem item = new DailyItem();
8768 item.mStartTime = mDailyStartTime;
8769 item.mEndTime = System.currentTimeMillis();
8770 boolean hasData = false;
8771 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8772 hasData = true;
8773 item.mDischargeSteps = new LevelStepTracker(
8774 mDailyDischargeStepTracker.mNumStepDurations,
8775 mDailyDischargeStepTracker.mStepDurations);
8776 }
8777 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8778 hasData = true;
8779 item.mChargeSteps = new LevelStepTracker(
8780 mDailyChargeStepTracker.mNumStepDurations,
8781 mDailyChargeStepTracker.mStepDurations);
8782 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008783 if (mDailyPackageChanges != null) {
8784 hasData = true;
8785 item.mPackageChanges = mDailyPackageChanges;
8786 mDailyPackageChanges = null;
8787 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008788 mDailyDischargeStepTracker.init();
8789 mDailyChargeStepTracker.init();
8790 updateDailyDeadlineLocked();
8791
8792 if (hasData) {
8793 mDailyItems.add(item);
8794 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8795 mDailyItems.remove(0);
8796 }
8797 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8798 try {
8799 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008800 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008801 writeDailyItemsLocked(out);
8802 BackgroundThread.getHandler().post(new Runnable() {
8803 @Override
8804 public void run() {
8805 synchronized (mCheckinFile) {
8806 FileOutputStream stream = null;
8807 try {
8808 stream = mDailyFile.startWrite();
8809 memStream.writeTo(stream);
8810 stream.flush();
8811 FileUtils.sync(stream);
8812 stream.close();
8813 mDailyFile.finishWrite(stream);
8814 } catch (IOException e) {
8815 Slog.w("BatteryStats",
8816 "Error writing battery daily items", e);
8817 mDailyFile.failWrite(stream);
8818 }
8819 }
8820 }
8821 });
8822 } catch (IOException e) {
8823 }
8824 }
8825 }
8826
8827 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8828 StringBuilder sb = new StringBuilder(64);
8829 out.startDocument(null, true);
8830 out.startTag(null, "daily-items");
8831 for (int i=0; i<mDailyItems.size(); i++) {
8832 final DailyItem dit = mDailyItems.get(i);
8833 out.startTag(null, "item");
8834 out.attribute(null, "start", Long.toString(dit.mStartTime));
8835 out.attribute(null, "end", Long.toString(dit.mEndTime));
8836 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8837 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008838 if (dit.mPackageChanges != null) {
8839 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8840 PackageChange pc = dit.mPackageChanges.get(j);
8841 if (pc.mUpdate) {
8842 out.startTag(null, "upd");
8843 out.attribute(null, "pkg", pc.mPackageName);
8844 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8845 out.endTag(null, "upd");
8846 } else {
8847 out.startTag(null, "rem");
8848 out.attribute(null, "pkg", pc.mPackageName);
8849 out.endTag(null, "rem");
8850 }
8851 }
8852 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008853 out.endTag(null, "item");
8854 }
8855 out.endTag(null, "daily-items");
8856 out.endDocument();
8857 }
8858
8859 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
8860 StringBuilder tmpBuilder) throws IOException {
8861 if (steps != null) {
8862 out.startTag(null, tag);
8863 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
8864 for (int i=0; i<steps.mNumStepDurations; i++) {
8865 out.startTag(null, "s");
8866 tmpBuilder.setLength(0);
8867 steps.encodeEntryAt(i, tmpBuilder);
8868 out.attribute(null, "v", tmpBuilder.toString());
8869 out.endTag(null, "s");
8870 }
8871 out.endTag(null, tag);
8872 }
8873 }
8874
8875 public void readDailyStatsLocked() {
8876 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
8877 mDailyItems.clear();
8878 FileInputStream stream;
8879 try {
8880 stream = mDailyFile.openRead();
8881 } catch (FileNotFoundException e) {
8882 return;
8883 }
8884 try {
8885 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008886 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008887 readDailyItemsLocked(parser);
8888 } catch (XmlPullParserException e) {
8889 } finally {
8890 try {
8891 stream.close();
8892 } catch (IOException e) {
8893 }
8894 }
8895 }
8896
8897 private void readDailyItemsLocked(XmlPullParser parser) {
8898 try {
8899 int type;
8900 while ((type = parser.next()) != XmlPullParser.START_TAG
8901 && type != XmlPullParser.END_DOCUMENT) {
8902 ;
8903 }
8904
8905 if (type != XmlPullParser.START_TAG) {
8906 throw new IllegalStateException("no start tag found");
8907 }
8908
8909 int outerDepth = parser.getDepth();
8910 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8911 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8912 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8913 continue;
8914 }
8915
8916 String tagName = parser.getName();
8917 if (tagName.equals("item")) {
8918 readDailyItemTagLocked(parser);
8919 } else {
8920 Slog.w(TAG, "Unknown element under <daily-items>: "
8921 + parser.getName());
8922 XmlUtils.skipCurrentTag(parser);
8923 }
8924 }
8925
8926 } catch (IllegalStateException e) {
8927 Slog.w(TAG, "Failed parsing daily " + e);
8928 } catch (NullPointerException e) {
8929 Slog.w(TAG, "Failed parsing daily " + e);
8930 } catch (NumberFormatException e) {
8931 Slog.w(TAG, "Failed parsing daily " + e);
8932 } catch (XmlPullParserException e) {
8933 Slog.w(TAG, "Failed parsing daily " + e);
8934 } catch (IOException e) {
8935 Slog.w(TAG, "Failed parsing daily " + e);
8936 } catch (IndexOutOfBoundsException e) {
8937 Slog.w(TAG, "Failed parsing daily " + e);
8938 }
8939 }
8940
8941 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8942 XmlPullParserException, IOException {
8943 DailyItem dit = new DailyItem();
8944 String attr = parser.getAttributeValue(null, "start");
8945 if (attr != null) {
8946 dit.mStartTime = Long.parseLong(attr);
8947 }
8948 attr = parser.getAttributeValue(null, "end");
8949 if (attr != null) {
8950 dit.mEndTime = Long.parseLong(attr);
8951 }
8952 int outerDepth = parser.getDepth();
8953 int type;
8954 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8955 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8956 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8957 continue;
8958 }
8959
8960 String tagName = parser.getName();
8961 if (tagName.equals("dis")) {
8962 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8963 } else if (tagName.equals("chg")) {
8964 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008965 } else if (tagName.equals("upd")) {
8966 if (dit.mPackageChanges == null) {
8967 dit.mPackageChanges = new ArrayList<>();
8968 }
8969 PackageChange pc = new PackageChange();
8970 pc.mUpdate = true;
8971 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8972 String verStr = parser.getAttributeValue(null, "ver");
8973 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8974 dit.mPackageChanges.add(pc);
8975 XmlUtils.skipCurrentTag(parser);
8976 } else if (tagName.equals("rem")) {
8977 if (dit.mPackageChanges == null) {
8978 dit.mPackageChanges = new ArrayList<>();
8979 }
8980 PackageChange pc = new PackageChange();
8981 pc.mUpdate = false;
8982 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8983 dit.mPackageChanges.add(pc);
8984 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008985 } else {
8986 Slog.w(TAG, "Unknown element under <item>: "
8987 + parser.getName());
8988 XmlUtils.skipCurrentTag(parser);
8989 }
8990 }
8991 mDailyItems.add(dit);
8992 }
8993
8994 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8995 String tag)
8996 throws NumberFormatException, XmlPullParserException, IOException {
8997 final String numAttr = parser.getAttributeValue(null, "n");
8998 if (numAttr == null) {
8999 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
9000 XmlUtils.skipCurrentTag(parser);
9001 return;
9002 }
9003 final int num = Integer.parseInt(numAttr);
9004 LevelStepTracker steps = new LevelStepTracker(num);
9005 if (isCharge) {
9006 dit.mChargeSteps = steps;
9007 } else {
9008 dit.mDischargeSteps = steps;
9009 }
9010 int i = 0;
9011 int outerDepth = parser.getDepth();
9012 int type;
9013 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9014 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9015 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9016 continue;
9017 }
9018
9019 String tagName = parser.getName();
9020 if ("s".equals(tagName)) {
9021 if (i < num) {
9022 String valueAttr = parser.getAttributeValue(null, "v");
9023 if (valueAttr != null) {
9024 steps.decodeEntryAt(i, valueAttr);
9025 i++;
9026 }
9027 }
9028 } else {
9029 Slog.w(TAG, "Unknown element under <" + tag + ">: "
9030 + parser.getName());
9031 XmlUtils.skipCurrentTag(parser);
9032 }
9033 }
9034 steps.mNumStepDurations = i;
9035 }
9036
9037 @Override
9038 public DailyItem getDailyItemLocked(int daysAgo) {
9039 int index = mDailyItems.size()-1-daysAgo;
9040 return index >= 0 ? mDailyItems.get(index) : null;
9041 }
9042
9043 @Override
9044 public long getCurrentDailyStartTime() {
9045 return mDailyStartTime;
9046 }
9047
9048 @Override
9049 public long getNextMinDailyDeadline() {
9050 return mNextMinDailyDeadline;
9051 }
9052
9053 @Override
9054 public long getNextMaxDailyDeadline() {
9055 return mNextMaxDailyDeadline;
9056 }
9057
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009058 @Override
9059 public boolean startIteratingOldHistoryLocked() {
9060 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9061 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009062 if ((mHistoryIterator = mHistory) == null) {
9063 return false;
9064 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009065 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009066 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009067 mReadOverflow = false;
9068 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009069 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009070 }
9071
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009072 @Override
9073 public boolean getNextOldHistoryLocked(HistoryItem out) {
9074 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
9075 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009076 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009077 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009078 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009079 HistoryItem cur = mHistoryIterator;
9080 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009081 if (!mReadOverflow && !end) {
9082 Slog.w(TAG, "Old history ends before new history!");
9083 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009084 return false;
9085 }
9086 out.setTo(cur);
9087 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009088 if (!mReadOverflow) {
9089 if (end) {
9090 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009091 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07009092 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009093 pw.println("Histories differ!");
9094 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009095 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009096 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009097 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
9098 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07009099 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009100 }
9101 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009102 return true;
9103 }
9104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009106 public void finishIteratingOldHistoryLocked() {
9107 mIteratingHistory = false;
9108 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009109 mHistoryIterator = null;
9110 }
9111
9112 public int getHistoryTotalSize() {
9113 return MAX_HISTORY_BUFFER;
9114 }
9115
9116 public int getHistoryUsedSize() {
9117 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009118 }
9119
9120 @Override
9121 public boolean startIteratingHistoryLocked() {
9122 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9123 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009124 if (mHistoryBuffer.dataSize() <= 0) {
9125 return false;
9126 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009127 mHistoryBuffer.setDataPosition(0);
9128 mReadOverflow = false;
9129 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009130 mReadHistoryStrings = new String[mHistoryTagPool.size()];
9131 mReadHistoryUids = new int[mHistoryTagPool.size()];
9132 mReadHistoryChars = 0;
9133 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9134 final HistoryTag tag = ent.getKey();
9135 final int idx = ent.getValue();
9136 mReadHistoryStrings[idx] = tag.string;
9137 mReadHistoryUids[idx] = tag.uid;
9138 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009139 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009140 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009141 }
9142
9143 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08009144 public int getHistoryStringPoolSize() {
9145 return mReadHistoryStrings.length;
9146 }
9147
9148 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009149 public int getHistoryStringPoolBytes() {
9150 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
9151 // Each string character is 2 bytes.
9152 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
9153 }
9154
9155 @Override
9156 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009157 return mReadHistoryStrings[index];
9158 }
9159
9160 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009161 public int getHistoryTagPoolUid(int index) {
9162 return mReadHistoryUids[index];
9163 }
9164
9165 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009166 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009167 final int pos = mHistoryBuffer.dataPosition();
9168 if (pos == 0) {
9169 out.clear();
9170 }
9171 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009172 if (end) {
9173 return false;
9174 }
9175
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009176 final long lastRealtime = out.time;
9177 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009178 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07009179 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
9180 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009181 out.currentTime = lastWalltime + (out.time - lastRealtime);
9182 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009183 return true;
9184 }
9185
9186 @Override
9187 public void finishIteratingHistoryLocked() {
9188 mIteratingHistory = false;
9189 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08009190 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009191 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009192
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009193 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009194 public long getHistoryBaseTime() {
9195 return mHistoryBaseTime;
9196 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009197
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009198 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009199 public int getStartCount() {
9200 return mStartCount;
9201 }
9202
9203 public boolean isOnBattery() {
9204 return mOnBattery;
9205 }
9206
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009207 public boolean isCharging() {
9208 return mCharging;
9209 }
9210
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009211 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009212 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009213 }
9214
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009215 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009216 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009217 mOnBatteryTimeBase.init(uptime, realtime);
9218 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009219 mRealtime = 0;
9220 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009221 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07009222 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009223 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009224
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009225 void initDischarge() {
9226 mLowDischargeAmountSinceCharge = 0;
9227 mHighDischargeAmountSinceCharge = 0;
9228 mDischargeAmountScreenOn = 0;
9229 mDischargeAmountScreenOnSinceCharge = 0;
9230 mDischargeAmountScreenOff = 0;
9231 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009232 mDischargeStepTracker.init();
9233 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009234 mDischargeScreenOffCounter.reset(false);
9235 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009236 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009237
9238 public void resetAllStatsCmdLocked() {
9239 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08009240 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009241 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08009242 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009243 long realtime = mSecRealtime * 1000;
9244 mDischargeStartLevel = mHistoryCur.batteryLevel;
9245 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009246 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009247 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
9248 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009249 mOnBatteryTimeBase.reset(uptime, realtime);
9250 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
9251 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009252 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009253 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
9254 mDischargeScreenOffUnplugLevel = 0;
9255 } else {
9256 mDischargeScreenOnUnplugLevel = 0;
9257 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
9258 }
9259 mDischargeAmountScreenOn = 0;
9260 mDischargeAmountScreenOff = 0;
9261 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009262 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009263 }
9264
9265 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07009266 final long uptimeMillis = mClocks.uptimeMillis();
9267 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009268 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07009269 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009270 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009271 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009272 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009273 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07009274
9275 if (mPowerProfile != null) {
9276 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
9277 } else {
9278 mEstimatedBatteryCapacity = -1;
9279 }
Jocelyn Dangc627d102017-04-14 13:15:14 -07009280 mMinLearnedBatteryCapacity = -1;
9281 mMaxLearnedBatteryCapacity = -1;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009282 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009283 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07009284 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009285 mLongestLightIdleTime = 0;
9286 mLongestFullIdleTime = 0;
9287 mDeviceIdleModeLightTimer.reset(false);
9288 mDeviceIdleModeFullTimer.reset(false);
9289 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009290 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009291 mPhoneOnTimer.reset(false);
9292 mAudioOnTimer.reset(false);
9293 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009294 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009295 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009296 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08009297 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009298 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009299 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009300 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009301 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009302 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009303 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009304 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009305 mNetworkByteActivityCounters[i].reset(false);
9306 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009307 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009308 mMobileRadioActiveTimer.reset(false);
9309 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009310 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009311 mMobileRadioActiveUnknownTime.reset(false);
9312 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009313 mWifiOnTimer.reset(false);
9314 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009315 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009316 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009317 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009318 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9319 mWifiSupplStateTimer[i].reset(false);
9320 }
9321 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9322 mWifiSignalStrengthsTimer[i].reset(false);
9323 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009324 mWifiActivity.reset(false);
9325 mBluetoothActivity.reset(false);
9326 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009327 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009328
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009329 for (int i=0; i<mUidStats.size(); i++) {
9330 if (mUidStats.valueAt(i).reset()) {
9331 mUidStats.remove(mUidStats.keyAt(i));
9332 i--;
9333 }
9334 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009335
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009336 if (mKernelWakelockStats.size() > 0) {
9337 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009338 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009339 }
9340 mKernelWakelockStats.clear();
9341 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009342
James Carr3a226052016-07-01 14:49:52 -07009343 if (mKernelMemoryStats.size() > 0) {
9344 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
9345 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
9346 }
9347 mKernelMemoryStats.clear();
9348 }
9349
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009350 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009351 for (SamplingTimer timer : mWakeupReasonStats.values()) {
9352 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009353 }
9354 mWakeupReasonStats.clear();
9355 }
9356
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009357 mLastHistoryStepDetails = null;
9358 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
9359 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
9360 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
9361 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
9362 mLastStepStatUserTime = mCurStepStatUserTime = 0;
9363 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
9364 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
9365 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
9366 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
9367 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
9368
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009369 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009370
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009371 clearHistoryLocked();
9372 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009373
Dianne Hackborn40c87252014-03-19 16:55:40 -07009374 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009375 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009376 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
9377 // Not recording process starts/stops.
9378 continue;
9379 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009380 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009381 if (active == null) {
9382 continue;
9383 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009384 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
9385 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009386 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07009387 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
9388 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009389 }
9390 }
9391 }
9392 }
9393
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009394 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009395 if (oldScreenOn) {
9396 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
9397 if (diff > 0) {
9398 mDischargeAmountScreenOn += diff;
9399 mDischargeAmountScreenOnSinceCharge += diff;
9400 }
9401 } else {
9402 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
9403 if (diff > 0) {
9404 mDischargeAmountScreenOff += diff;
9405 mDischargeAmountScreenOffSinceCharge += diff;
9406 }
9407 }
9408 if (newScreenOn) {
9409 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
9410 mDischargeScreenOffUnplugLevel = 0;
9411 } else {
9412 mDischargeScreenOnUnplugLevel = 0;
9413 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
9414 }
9415 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009416
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009417 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009418 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009419 final boolean screenOn = mScreenState == Display.STATE_ON;
9420 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009421 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009422 }
9423
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009424 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
9425 private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
9426
9427 private final Object mWifiNetworkLock = new Object();
9428
9429 @GuardedBy("mWifiNetworkLock")
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009430 private String[] mWifiIfaces = EmptyArray.STRING;
9431
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009432 @GuardedBy("mWifiNetworkLock")
9433 private NetworkStats mLastWifiNetworkStats = new NetworkStats(0, -1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009434
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009435 private final Object mModemNetworkLock = new Object();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009436
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009437 @GuardedBy("mModemNetworkLock")
9438 private String[] mModemIfaces = EmptyArray.STRING;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009439
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009440 @GuardedBy("mModemNetworkLock")
9441 private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
9442
9443 private NetworkStats readNetworkStatsLocked(String[] ifaces) {
9444 try {
9445 if (!ArrayUtils.isEmpty(ifaces)) {
9446 return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
9447 NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
9448 }
9449 } catch (IOException e) {
9450 Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009451 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009452 return null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009453 }
9454
9455 /**
9456 * Distribute WiFi energy info and network traffic to apps.
9457 * @param info The energy information from the WiFi controller.
9458 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009459 public void updateWifiState(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009460 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009461 Slog.d(TAG, "Updating wifi stats: " + Arrays.toString(mWifiIfaces));
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009462 }
9463
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009464 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009465 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009466 synchronized (mWifiNetworkLock) {
9467 final NetworkStats latestStats = readNetworkStatsLocked(mWifiIfaces);
9468 if (latestStats != null) {
9469 delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
9470 mNetworkStatsPool.acquire());
9471 mNetworkStatsPool.release(mLastWifiNetworkStats);
9472 mLastWifiNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009473 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009474 }
9475
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009476 synchronized (this) {
9477 if (!mOnBatteryInternal) {
9478 if (delta != null) {
9479 mNetworkStatsPool.release(delta);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009480 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009481 return;
9482 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009483
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009484 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
9485 SparseLongArray rxPackets = new SparseLongArray();
9486 SparseLongArray txPackets = new SparseLongArray();
9487 long totalTxPackets = 0;
9488 long totalRxPackets = 0;
9489 if (delta != null) {
9490 NetworkStats.Entry entry = new NetworkStats.Entry();
9491 final int size = delta.size();
9492 for (int i = 0; i < size; i++) {
9493 entry = delta.getValues(i, entry);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009494
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009495 if (DEBUG_ENERGY) {
9496 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
9497 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9498 + " txPackets=" + entry.txPackets);
9499 }
9500
9501 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9502 // Skip the lookup below since there is no work to do.
9503 continue;
9504 }
9505
9506 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9507 if (entry.rxBytes != 0) {
9508 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009509 entry.rxPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009510 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9511 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
9512 entry.rxPackets);
9513 }
9514 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9515 entry.rxBytes);
9516 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9517 entry.rxPackets);
9518
9519 rxPackets.put(u.getUid(), entry.rxPackets);
9520
9521 // Sum the total number of packets so that the Rx Power can
9522 // be evenly distributed amongst the apps.
9523 totalRxPackets += entry.rxPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009524 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009525
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009526 if (entry.txBytes != 0) {
9527 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009528 entry.txPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009529 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9530 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
9531 entry.txPackets);
9532 }
9533 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9534 entry.txBytes);
9535 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9536 entry.txPackets);
9537
9538 txPackets.put(u.getUid(), entry.txPackets);
9539
9540 // Sum the total number of packets so that the Tx Power can
9541 // be evenly distributed amongst the apps.
9542 totalTxPackets += entry.txPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009543 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009544 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009545 mNetworkStatsPool.release(delta);
9546 delta = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07009547 }
9548
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009549 if (info != null) {
9550 mHasWifiReporting = true;
Adam Lesinskie08af192015-03-25 16:42:59 -07009551
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009552 // Measured in mAms
9553 final long txTimeMs = info.getControllerTxTimeMillis();
9554 final long rxTimeMs = info.getControllerRxTimeMillis();
9555 final long idleTimeMs = info.getControllerIdleTimeMillis();
9556 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009557
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009558 long leftOverRxTimeMs = rxTimeMs;
9559 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009560
Adam Lesinskie08af192015-03-25 16:42:59 -07009561 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009562 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
9563 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9564 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9565 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
9566 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009567 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009568
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009569 long totalWifiLockTimeMs = 0;
9570 long totalScanTimeMs = 0;
9571
9572 // On the first pass, collect some totals so that we can normalize power
9573 // calculations if we need to.
9574 final int uidStatsSize = mUidStats.size();
9575 for (int i = 0; i < uidStatsSize; i++) {
9576 final Uid uid = mUidStats.valueAt(i);
9577
9578 // Sum the total scan power for all apps.
9579 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
9580 elapsedRealtimeMs * 1000) / 1000;
9581
9582 // Sum the total time holding wifi lock for all apps.
9583 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9584 elapsedRealtimeMs * 1000) / 1000;
9585 }
9586
9587 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
9588 Slog.d(TAG,
9589 " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
9590 + rxTimeMs + " ms). Normalizing scan time.");
9591 }
9592 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
9593 Slog.d(TAG,
9594 " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
9595 + txTimeMs + " ms). Normalizing scan time.");
9596 }
9597
9598 // Actually assign and distribute power usage to apps.
9599 for (int i = 0; i < uidStatsSize; i++) {
9600 final Uid uid = mUidStats.valueAt(i);
9601
9602 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
9603 elapsedRealtimeMs * 1000) / 1000;
9604 if (scanTimeSinceMarkMs > 0) {
9605 // Set the new mark so that next time we get new data since this point.
9606 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
9607
9608 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
9609 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
9610
9611 // Our total scan time is more than the reported Tx/Rx time.
9612 // This is possible because the cost of a scan is approximate.
9613 // Let's normalize the result so that we evenly blame each app
9614 // scanning.
9615 //
9616 // This means that we may have apps that transmitted/received packets not be
9617 // blamed for this, but this is fine as scans are relatively more expensive.
9618 if (totalScanTimeMs > rxTimeMs) {
9619 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
9620 totalScanTimeMs;
9621 }
9622 if (totalScanTimeMs > txTimeMs) {
9623 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9624 totalScanTimeMs;
9625 }
9626
9627 if (DEBUG_ENERGY) {
9628 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9629 + scanRxTimeSinceMarkMs + " ms Tx:"
9630 + scanTxTimeSinceMarkMs + " ms)");
9631 }
9632
9633 ControllerActivityCounterImpl activityCounter =
9634 uid.getOrCreateWifiControllerActivityLocked();
9635 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9636 activityCounter.getTxTimeCounters()[0].addCountLocked(
9637 scanTxTimeSinceMarkMs);
9638 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9639 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
9640 }
9641
9642 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9643 // lock.
9644 final long wifiLockTimeSinceMarkMs =
9645 uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9646 elapsedRealtimeMs * 1000) / 1000;
9647 if (wifiLockTimeSinceMarkMs > 0) {
9648 // Set the new mark so that next time we get new data since this point.
9649 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9650
9651 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9652 / totalWifiLockTimeMs;
9653 if (DEBUG_ENERGY) {
9654 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9655 + myIdleTimeMs + " ms");
9656 }
9657 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9658 .addCountLocked(myIdleTimeMs);
9659 }
9660 }
9661
Adam Lesinskie08af192015-03-25 16:42:59 -07009662 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009663 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
9664 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009665 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009666
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009667 // Distribute the remaining Tx power appropriately between all apps that transmitted
9668 // packets.
9669 for (int i = 0; i < txPackets.size(); i++) {
9670 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
9671 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs)
9672 / totalTxPackets;
9673 if (DEBUG_ENERGY) {
9674 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9675 }
9676 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9677 .addCountLocked(myTxTimeMs);
9678 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009679
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009680 // Distribute the remaining Rx power appropriately between all apps that received
9681 // packets.
9682 for (int i = 0; i < rxPackets.size(); i++) {
9683 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9684 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs)
9685 / totalRxPackets;
9686 if (DEBUG_ENERGY) {
9687 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9688 }
9689 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9690 .addCountLocked(myRxTimeMs);
9691 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009692
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009693 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9694
9695
9696 // Update WiFi controller stats.
9697 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9698 mWifiActivity.getTxTimeCounters()[0].addCountLocked(
9699 info.getControllerTxTimeMillis());
9700 mWifiActivity.getIdleTimeCounter().addCountLocked(
9701 info.getControllerIdleTimeMillis());
9702
9703 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9704 final double opVolt = mPowerProfile.getAveragePower(
9705 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9706 if (opVolt != 0) {
9707 // We store the power drain as mAms.
9708 mWifiActivity.getPowerCounter().addCountLocked(
9709 (long) (info.getControllerEnergyUsed() / opVolt));
9710 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009711 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009712 }
9713 }
9714
9715 /**
9716 * Distribute Cell radio energy info and network traffic to apps.
9717 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009718 public void updateMobileRadioState(@Nullable final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009719 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009720 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009721 }
9722
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009723 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009724 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009725 synchronized (mModemNetworkLock) {
9726 final NetworkStats latestStats = readNetworkStatsLocked(mModemIfaces);
9727 if (latestStats != null) {
9728 delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
9729 mNetworkStatsPool.acquire());
9730 mNetworkStatsPool.release(mLastModemNetworkStats);
9731 mLastModemNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009732 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009733 }
9734
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009735 synchronized (this) {
9736 if (!mOnBatteryInternal) {
9737 if (delta != null) {
9738 mNetworkStatsPool.release(delta);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009739 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009740 return;
Adam Lesinskie08af192015-03-25 16:42:59 -07009741 }
9742
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009743 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
9744 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9745 elapsedRealtimeMs * 1000);
9746 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
9747
9748 long totalRxPackets = 0;
9749 long totalTxPackets = 0;
9750 if (delta != null) {
9751 NetworkStats.Entry entry = new NetworkStats.Entry();
9752 final int size = delta.size();
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009753 for (int i = 0; i < size; i++) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009754 entry = delta.getValues(i, entry);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009755 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9756 continue;
9757 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009758
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009759 if (DEBUG_ENERGY) {
9760 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9761 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9762 + " txPackets=" + entry.txPackets);
9763 }
9764
9765 totalRxPackets += entry.rxPackets;
9766 totalTxPackets += entry.txPackets;
9767
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009768 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009769 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
9770 entry.rxPackets);
9771 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
9772 entry.txPackets);
9773 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9774 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
9775 entry.rxBytes, entry.rxPackets);
9776 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
9777 entry.txBytes, entry.txPackets);
9778 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009779
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009780 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9781 entry.rxBytes);
9782 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9783 entry.txBytes);
9784 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9785 entry.rxPackets);
9786 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9787 entry.txPackets);
9788 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009789
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009790 // Now distribute proportional blame to the apps that did networking.
9791 long totalPackets = totalRxPackets + totalTxPackets;
9792 if (totalPackets > 0) {
9793 for (int i = 0; i < size; i++) {
9794 entry = delta.getValues(i, entry);
9795 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9796 continue;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009797 }
9798
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009799 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9800
9801 // Distribute total radio active time in to this app.
9802 final long appPackets = entry.rxPackets + entry.txPackets;
9803 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9804 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9805
9806 // Remove this app from the totals, so that we don't lose any time
9807 // due to rounding.
9808 radioTime -= appRadioTime;
9809 totalPackets -= appPackets;
9810
9811 if (activityInfo != null) {
9812 ControllerActivityCounterImpl activityCounter =
9813 u.getOrCreateModemControllerActivityLocked();
9814 if (totalRxPackets > 0 && entry.rxPackets > 0) {
9815 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9816 / totalRxPackets;
9817 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9818 }
9819
9820 if (totalTxPackets > 0 && entry.txPackets > 0) {
9821 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9822 long txMs =
9823 entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9824 txMs /= totalTxPackets;
9825 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9826 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009827 }
9828 }
9829 }
9830 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009831
9832 if (radioTime > 0) {
9833 // Whoops, there is some radio time we can't blame on an app!
9834 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9835 mMobileRadioActiveUnknownCount.addCountLocked(1);
9836 }
9837
9838 mNetworkStatsPool.release(delta);
9839 delta = null;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009840 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009841
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009842 if (activityInfo != null) {
9843 mHasModemReporting = true;
9844 mModemActivity.getIdleTimeCounter().addCountLocked(
9845 activityInfo.getIdleTimeMillis());
9846 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9847 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9848 mModemActivity.getTxTimeCounters()[lvl]
9849 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9850 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009851
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009852 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9853 final double opVolt = mPowerProfile.getAveragePower(
9854 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9855 if (opVolt != 0) {
9856 // We store the power drain as mAms.
9857 mModemActivity.getPowerCounter().addCountLocked(
9858 (long) (activityInfo.getEnergyUsed() / opVolt));
9859 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009860 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009861 }
9862 }
9863
9864 /**
9865 * Distribute Bluetooth energy info and network traffic to apps.
9866 * @param info The energy information from the bluetooth controller.
9867 */
9868 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009869 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009870 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009871 }
9872
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009873 if (info == null || !mOnBatteryInternal) {
9874 return;
9875 }
Adam Lesinskie283d332015-04-16 12:29:25 -07009876
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009877 mHasBluetoothReporting = true;
9878
Bookatz867c0d72017-03-07 18:23:42 -08009879 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009880 final long rxTimeMs = info.getControllerRxTimeMillis();
9881 final long txTimeMs = info.getControllerTxTimeMillis();
9882
9883 if (DEBUG_ENERGY) {
9884 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
9885 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9886 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9887 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
9888 }
9889
9890 long totalScanTimeMs = 0;
9891
9892 final int uidCount = mUidStats.size();
9893 for (int i = 0; i < uidCount; i++) {
9894 final Uid u = mUidStats.valueAt(i);
9895 if (u.mBluetoothScanTimer == null) {
9896 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07009897 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009898
Bookatzaa4594a2017-03-24 12:39:56 -07009899 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009900 elapsedRealtimeMs * 1000) / 1000;
9901 }
9902
9903 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
9904 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
9905
9906 if (DEBUG_ENERGY) {
9907 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
9908 + " TX=" + normalizeScanTxTime);
9909 }
9910
9911 long leftOverRxTimeMs = rxTimeMs;
9912 long leftOverTxTimeMs = txTimeMs;
9913
9914 for (int i = 0; i < uidCount; i++) {
9915 final Uid u = mUidStats.valueAt(i);
9916 if (u.mBluetoothScanTimer == null) {
9917 continue;
9918 }
9919
Bookatzaa4594a2017-03-24 12:39:56 -07009920 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009921 elapsedRealtimeMs * 1000) / 1000;
9922 if (scanTimeSinceMarkMs > 0) {
9923 // Set the new mark so that next time we get new data since this point.
9924 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
9925
9926 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
9927 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
9928
9929 if (normalizeScanRxTime) {
9930 // Scan time is longer than the total rx time in the controller,
9931 // so distribute the scan time proportionately. This means regular traffic
9932 // will not blamed, but scans are more expensive anyways.
9933 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
9934 }
9935
9936 if (normalizeScanTxTime) {
9937 // Scan time is longer than the total tx time in the controller,
9938 // so distribute the scan time proportionately. This means regular traffic
9939 // will not blamed, but scans are more expensive anyways.
9940 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
9941 }
9942
9943 final ControllerActivityCounterImpl counter =
9944 u.getOrCreateBluetoothControllerActivityLocked();
9945 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
9946 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
9947
9948 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
9949 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
9950 }
9951 }
9952
9953 if (DEBUG_ENERGY) {
9954 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
9955 + " TX=" + leftOverTxTimeMs);
9956 }
9957
9958 //
9959 // Now distribute blame to apps that did bluetooth traffic.
9960 //
9961
9962 long totalTxBytes = 0;
9963 long totalRxBytes = 0;
9964
9965 final UidTraffic[] uidTraffic = info.getUidTraffic();
9966 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9967 for (int i = 0; i < numUids; i++) {
9968 final UidTraffic traffic = uidTraffic[i];
9969
9970 // Add to the global counters.
9971 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9972 traffic.getRxBytes());
9973 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9974 traffic.getTxBytes());
9975
9976 // Add to the UID counters.
9977 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9978 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9979 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9980
9981 // Calculate the total traffic.
9982 totalTxBytes += traffic.getTxBytes();
9983 totalRxBytes += traffic.getRxBytes();
9984 }
9985
9986 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9987 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009988 for (int i = 0; i < numUids; i++) {
9989 final UidTraffic traffic = uidTraffic[i];
9990
Adam Lesinski50e47602015-12-04 17:04:54 -08009991 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009992 final ControllerActivityCounterImpl counter =
9993 u.getOrCreateBluetoothControllerActivityLocked();
9994
9995 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
9996 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9997
9998 if (DEBUG_ENERGY) {
9999 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
10000 + " rx_time=" + timeRxMs);
10001 }
10002 counter.getRxTimeCounter().addCountLocked(timeRxMs);
10003 leftOverRxTimeMs -= timeRxMs;
10004 }
10005
10006 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
10007 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
10008
10009 if (DEBUG_ENERGY) {
10010 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
10011 + " tx_time=" + timeTxMs);
10012 }
10013
10014 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
10015 leftOverTxTimeMs -= timeTxMs;
10016 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010017 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010018 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010019
10020 mBluetoothActivity.getRxTimeCounter().addCountLocked(
10021 info.getControllerRxTimeMillis());
10022 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
10023 info.getControllerTxTimeMillis());
10024 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
10025 info.getControllerIdleTimeMillis());
10026
10027 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10028 final double opVolt = mPowerProfile.getAveragePower(
10029 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10030 if (opVolt != 0) {
10031 // We store the power drain as mAms.
10032 mBluetoothActivity.getPowerCounter().addCountLocked(
10033 (long) (info.getControllerEnergyUsed() / opVolt));
10034 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010035 }
10036
10037 /**
10038 * Read and distribute kernel wake lock use across apps.
10039 */
10040 public void updateKernelWakelocksLocked() {
10041 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
10042 mTmpWakelockStats);
10043 if (wakelockStats == null) {
10044 // Not crashing might make board bringup easier.
10045 Slog.w(TAG, "Couldn't get kernel wake lock stats");
10046 return;
10047 }
10048
10049 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
10050 String name = ent.getKey();
10051 KernelWakelockStats.Entry kws = ent.getValue();
10052
10053 SamplingTimer kwlt = mKernelWakelockStats.get(name);
10054 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010055 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010056 mKernelWakelockStats.put(name, kwlt);
10057 }
Adam Lesinskid84ad302016-05-17 18:31:02 -070010058
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010059 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010060 kwlt.setUpdateVersion(kws.mVersion);
10061 }
10062
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010063 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -070010064 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
10065 // this time.
10066 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10067 SamplingTimer st = ent.getValue();
10068 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
10069 st.endSample();
10070 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010071 }
10072 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010073
Adam Lesinskid84ad302016-05-17 18:31:02 -070010074 // Record whether we've seen a non-zero time (for debugging b/22716723).
10075 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010076 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
10077 }
10078
10079 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
10080 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
10081 wakelockStats.kernelWakelockVersion);
10082 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010083 }
10084
Adam Lesinski72478f02015-06-17 15:39:43 -070010085 // We use an anonymous class to access these variables,
10086 // so they can't live on the stack or they'd have to be
10087 // final MutableLong objects (more allocations).
10088 // Used in updateCpuTimeLocked().
10089 long mTempTotalCpuUserTimeUs;
10090 long mTempTotalCpuSystemTimeUs;
10091
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010092 /**
James Carr3a226052016-07-01 14:49:52 -070010093 * Reads the newest memory stats from the kernel.
10094 */
10095 public void updateKernelMemoryBandwidthLocked() {
10096 mKernelMemoryBandwidthStats.updateStats();
10097 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
10098 final int bandwidthEntryCount = bandwidthEntries.size();
10099 int index;
10100 for (int i = 0; i < bandwidthEntryCount; i++) {
10101 SamplingTimer timer;
10102 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
10103 timer = mKernelMemoryStats.valueAt(index);
10104 } else {
10105 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
10106 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
10107 }
10108 timer.update(bandwidthEntries.valueAt(i), 1);
10109 if (DEBUG_MEMORY) {
10110 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
10111 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
10112 mKernelMemoryStats.get(
10113 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
10114 mKernelMemoryStats.size()));
10115 }
10116 }
10117 }
10118
10119 /**
Adam Lesinski72478f02015-06-17 15:39:43 -070010120 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
10121 * and we are on battery with screen off, we give more of the cpu time to those apps holding
10122 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010123 */
Sudheer Shanka671985f2017-05-19 11:33:42 -070010124 public void updateCpuTimeLocked(boolean updateCpuFreqData) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010125 if (mPowerProfile == null) {
10126 return;
10127 }
10128
Adam Lesinski72478f02015-06-17 15:39:43 -070010129 if (DEBUG_ENERGY_CPU) {
10130 Slog.d(TAG, "!Cpu updating!");
10131 }
10132
10133 // Holding a wakelock costs more than just using the cpu.
10134 // Currently, we assign only half the cpu time to an app that is running but
10135 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
10136 // If no app is holding a wakelock, then the distribution is normal.
10137 final int wakelockWeight = 50;
10138
Adam Lesinski6832f392015-09-05 18:05:40 -070010139 // Read the time spent for each cluster at various cpu frequencies.
10140 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
10141 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
10142 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
10143 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010144
10145 int numWakelocks = 0;
10146
10147 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
10148 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
10149 final int numPartialTimers = mPartialTimers.size();
10150 if (mOnBatteryScreenOffTimeBase.isRunning()) {
10151 for (int i = 0; i < numPartialTimers; i++) {
10152 final StopwatchTimer timer = mPartialTimers.get(i);
10153 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
10154 // Since the collection and blaming of wakelocks can be scheduled to run after
10155 // some delay, the mPartialTimers list may have new entries. We can't blame
10156 // the newly added timer for past cpu time, so we only consider timers that
10157 // were present for one round of collection. Once a timer has gone through
10158 // a round of collection, its mInList field is set to true.
10159 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010160 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010161 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010162 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010163
10164 final int numWakelocksF = numWakelocks;
10165 mTempTotalCpuUserTimeUs = 0;
10166 mTempTotalCpuSystemTimeUs = 0;
10167
10168 // Read the CPU data for each UID. This will internally generate a snapshot so next time
10169 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
10170 // we just ignore the data.
Amith Yamasanid0ddeba2017-05-26 09:46:58 -070010171 final long startTimeMs = mClocks.uptimeMillis();
Adam Lesinski72478f02015-06-17 15:39:43 -070010172 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
10173 new KernelUidCpuTimeReader.Callback() {
10174 @Override
Adam Lesinskid4abd1e2017-04-12 11:29:13 -070010175 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs) {
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010176 uid = mapUid(uid);
10177 if (Process.isIsolated(uid)) {
10178 // This could happen if the isolated uid mapping was removed before
10179 // that process was actually killed.
10180 mKernelUidCpuTimeReader.removeUid(uid);
10181 Slog.d(TAG, "Got readings for an isolated uid with"
10182 + " no mapping to owning uid: " + uid);
10183 return;
10184 }
10185 final Uid u = getUidStatsLocked(uid);
Adam Lesinski72478f02015-06-17 15:39:43 -070010186
10187 // Accumulate the total system and user time.
10188 mTempTotalCpuUserTimeUs += userTimeUs;
10189 mTempTotalCpuSystemTimeUs += systemTimeUs;
10190
10191 StringBuilder sb = null;
10192 if (DEBUG_ENERGY_CPU) {
10193 sb = new StringBuilder();
10194 sb.append(" got time for uid=").append(u.mUid).append(": u=");
10195 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10196 sb.append(" s=");
10197 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -070010198 sb.append("\n");
Adam Lesinski72478f02015-06-17 15:39:43 -070010199 }
10200
10201 if (numWakelocksF > 0) {
10202 // We have wakelocks being held, so only give a portion of the
10203 // time to the process. The rest will be distributed among wakelock
10204 // holders.
10205 userTimeUs = (userTimeUs * wakelockWeight) / 100;
10206 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
10207 }
10208
10209 if (sb != null) {
10210 sb.append(" adding to uid=").append(u.mUid).append(": u=");
10211 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10212 sb.append(" s=");
10213 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10214 Slog.d(TAG, sb.toString());
10215 }
10216
10217 u.mUserCpuTime.addCountLocked(userTimeUs);
10218 u.mSystemCpuTime.addCountLocked(systemTimeUs);
10219
10220 // Add the cpu speeds to this UID. These are used as a ratio
10221 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010222 final int numClusters = mPowerProfile.getNumCpuClusters();
10223 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
10224 numClusters) {
10225 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -070010226 }
10227
10228 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010229 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
10230 cluster);
10231 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
10232 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010233 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010234 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -070010235 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010236
10237 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
10238 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
10239 if (cpuSpeeds[speed] == null) {
10240 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10241 }
10242 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
10243 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010244 }
10245 }
10246 });
10247
Sudheer Shanka671985f2017-05-19 11:33:42 -070010248 if (updateCpuFreqData) {
10249 readKernelUidCpuFreqTimesLocked();
10250 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010251
Amith Yamasanid0ddeba2017-05-26 09:46:58 -070010252 final long elapse = (mClocks.uptimeMillis() - startTimeMs);
Makoto Onuki3f8e2972017-05-11 10:52:37 -070010253 if (DEBUG_ENERGY_CPU || (elapse >= 100)) {
10254 Slog.d(TAG, "Reading cpu stats took " + elapse + " ms");
Adam Lesinski72478f02015-06-17 15:39:43 -070010255 }
10256
10257 if (mOnBatteryInternal && numWakelocks > 0) {
10258 // Distribute a portion of the total cpu time to wakelock holders.
10259 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
10260 mTempTotalCpuSystemTimeUs =
10261 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
10262
10263 for (int i = 0; i < numPartialTimers; i++) {
10264 final StopwatchTimer timer = mPartialTimers.get(i);
10265
10266 // The system does not share any blame, as it is usually holding the wakelock
10267 // on behalf of an app.
10268 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
10269 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
10270 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
10271
10272 if (DEBUG_ENERGY_CPU) {
10273 StringBuilder sb = new StringBuilder();
10274 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
10275 .append(": u=");
10276 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10277 sb.append(" s=");
10278 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10279 Slog.d(TAG, sb.toString());
10280 }
10281
10282 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
10283 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
10284
10285 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -070010286 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -070010287
10288 mTempTotalCpuUserTimeUs -= userTimeUs;
10289 mTempTotalCpuSystemTimeUs -= systemTimeUs;
10290 numWakelocks--;
10291 }
10292 }
10293
10294 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
10295 // Anything left over is given to the system.
10296 if (DEBUG_ENERGY_CPU) {
10297 StringBuilder sb = new StringBuilder();
10298 sb.append(" Distributing lost time to system: u=");
10299 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
10300 sb.append(" s=");
10301 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
10302 Slog.d(TAG, sb.toString());
10303 }
10304
10305 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
10306 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
10307 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
10308
10309 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -070010310 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
10311 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -070010312 }
10313 }
10314
10315 // See if there is a difference in wakelocks between this collection and the last
10316 // collection.
10317 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
10318 // No difference, so each timer is now considered for the next collection.
10319 for (int i = 0; i < numPartialTimers; i++) {
10320 mPartialTimers.get(i).mInList = true;
10321 }
10322 } else {
10323 // The lists are different, meaning we added (or removed a timer) since the last
10324 // collection.
10325 final int numLastPartialTimers = mLastPartialTimers.size();
10326 for (int i = 0; i < numLastPartialTimers; i++) {
10327 mLastPartialTimers.get(i).mInList = false;
10328 }
10329 mLastPartialTimers.clear();
10330
10331 // Mark the current timers as gone through a collection.
10332 for (int i = 0; i < numPartialTimers; i++) {
10333 final StopwatchTimer timer = mPartialTimers.get(i);
10334 timer.mInList = true;
10335 mLastPartialTimers.add(timer);
10336 }
10337 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010338 }
10339
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010340 void readKernelUidCpuFreqTimesLocked() {
10341 mKernelUidCpuFreqTimeReader.readDelta(!mOnBatteryInternal ? null :
10342 new KernelUidCpuFreqTimeReader.Callback() {
10343 @Override
10344 public void onCpuFreqs(long[] cpuFreqs) {
10345 mCpuFreqs = cpuFreqs;
10346 }
10347
10348 @Override
10349 public void onUidCpuFreqTime(int uid, long[] cpuFreqTimeMs) {
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010350 uid = mapUid(uid);
10351 if (Process.isIsolated(uid)) {
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070010352 mKernelUidCpuFreqTimeReader.removeUid(uid);
10353 Slog.d(TAG, "Got freq readings for an isolated uid with"
10354 + " no mapping to owning uid: " + uid);
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010355 return;
10356 }
10357 final Uid u = getUidStatsLocked(uid);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010358 if (u.mCpuFreqTimeMs == null) {
10359 u.mCpuFreqTimeMs = new LongSamplingCounterArray(mOnBatteryTimeBase);
10360 }
10361 u.mCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
10362 if (u.mScreenOffCpuFreqTimeMs == null) {
10363 u.mScreenOffCpuFreqTimeMs = new LongSamplingCounterArray(
10364 mOnBatteryScreenOffTimeBase);
10365 }
10366 u.mScreenOffCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
10367 }
10368 });
10369 }
10370
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010371 boolean setChargingLocked(boolean charging) {
10372 if (mCharging != charging) {
10373 mCharging = charging;
10374 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010375 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010376 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010377 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010378 }
10379 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
10380 return true;
10381 }
10382 return false;
10383 }
10384
Dianne Hackborn40c87252014-03-19 16:55:40 -070010385 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010386 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010387 boolean doWrite = false;
10388 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
10389 m.arg1 = onBattery ? 1 : 0;
10390 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010391
Dianne Hackborn40c87252014-03-19 16:55:40 -070010392 final long uptime = mSecUptime * 1000;
10393 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010394 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010395 if (onBattery) {
10396 // We will reset our status if we are unplugging after the
10397 // battery was last full, or the level is at 100, or
10398 // we have gone through a significant charge (from a very low
10399 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010400 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -070010401 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010402 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010403 || (mDischargeCurrentLevel < 20 && level >= 80)
10404 || (getHighDischargeAmountSinceCharge() >= 200
10405 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010406 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010407 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010408 + " lowAmount=" + getLowDischargeAmountSinceCharge()
10409 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010410 // Before we write, collect a snapshot of the final aggregated
10411 // stats to be reported in the next checkin. Only do this if we have
10412 // a sufficient amount of data to make it interesting.
10413 if (getLowDischargeAmountSinceCharge() >= 20) {
10414 final Parcel parcel = Parcel.obtain();
10415 writeSummaryToParcel(parcel, true);
10416 BackgroundThread.getHandler().post(new Runnable() {
10417 @Override public void run() {
10418 synchronized (mCheckinFile) {
10419 FileOutputStream stream = null;
10420 try {
10421 stream = mCheckinFile.startWrite();
10422 stream.write(parcel.marshall());
10423 stream.flush();
10424 FileUtils.sync(stream);
10425 stream.close();
10426 mCheckinFile.finishWrite(stream);
10427 } catch (IOException e) {
10428 Slog.w("BatteryStats",
10429 "Error writing checkin battery statistics", e);
10430 mCheckinFile.failWrite(stream);
10431 } finally {
10432 parcel.recycle();
10433 }
10434 }
10435 }
10436 });
10437 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010438 doWrite = true;
10439 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -080010440 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010441 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -080010442 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010443 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010444 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010445 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010446 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010447 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010448 if (mCharging) {
10449 setChargingLocked(false);
10450 }
10451 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010452 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010453 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010454 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010455 mDischargeStepTracker.clearTime();
10456 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010457 mInitStepMode = mCurStepMode;
10458 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010459 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010460 mHistoryCur.batteryLevel = (byte)level;
10461 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10462 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
10463 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010464 if (reset) {
10465 mRecordingHistory = true;
10466 startRecordingHistory(mSecRealtime, mSecUptime, reset);
10467 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010468 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010469 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010470 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010471 mDischargeScreenOnUnplugLevel = level;
10472 mDischargeScreenOffUnplugLevel = 0;
10473 } else {
10474 mDischargeScreenOnUnplugLevel = 0;
10475 mDischargeScreenOffUnplugLevel = level;
10476 }
10477 mDischargeAmountScreenOn = 0;
10478 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010479 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010480 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010481 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010482 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010483 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010484 mHistoryCur.batteryLevel = (byte)level;
10485 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10486 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
10487 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -070010488 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010489 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010490 if (level < mDischargeUnplugLevel) {
10491 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
10492 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
10493 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010494 updateDischargeScreenLevelsLocked(screenOn, screenOn);
10495 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010496 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010497 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010498 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010499 mInitStepMode = mCurStepMode;
10500 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010501 }
10502 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
10503 if (mFile != null) {
10504 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 }
10506 }
10507 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010508
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010509 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
10510 boolean reset) {
10511 mRecordingHistory = true;
10512 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070010513 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
10514 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010515 mHistoryCur);
10516 mHistoryCur.currentTime = 0;
10517 if (reset) {
10518 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
10519 }
10520 }
10521
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010522 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
10523 final long uptimeMs) {
10524 if (mRecordingHistory) {
10525 mHistoryCur.currentTime = currentTime;
10526 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
10527 mHistoryCur);
10528 mHistoryCur.currentTime = 0;
10529 }
10530 }
10531
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010532 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
10533 if (mRecordingHistory) {
10534 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010535 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
10536 mHistoryCur);
10537 mHistoryCur.currentTime = 0;
10538 }
10539 }
10540
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010541 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010542 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010543 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010544 }
10545 }
10546
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010547 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010548 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010549
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010550 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -080010551 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070010552 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
10553 temp = Math.max(0, temp);
10554
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010555 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080010556 final long uptime = mClocks.uptimeMillis();
10557 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010558 if (!mHaveBatteryLevel) {
10559 mHaveBatteryLevel = true;
10560 // We start out assuming that the device is plugged in (not
10561 // on battery). If our first report is now that we are indeed
10562 // plugged in, then twiddle our state to correctly reflect that
10563 // since we won't be going through the full setOnBattery().
10564 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010565 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010566 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010567 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010568 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010569 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010570 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010571 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010572 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010573 mHistoryCur.batteryStatus = (byte)status;
10574 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010575 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010576 mMaxChargeStepLevel = mMinDischargeStepLevel =
10577 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010578 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010579 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
10580 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
10581 }
10582 int oldStatus = mHistoryCur.batteryStatus;
10583 if (onBattery) {
10584 mDischargeCurrentLevel = level;
10585 if (!mRecordingHistory) {
10586 mRecordingHistory = true;
10587 startRecordingHistory(elapsedRealtime, uptime, true);
10588 }
10589 } else if (level < 96) {
10590 if (!mRecordingHistory) {
10591 mRecordingHistory = true;
10592 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010593 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010594 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010595 mCurrentBatteryLevel = level;
10596 if (mDischargePlugLevel < 0) {
10597 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070010598 }
Adam Lesinski926969b2016-04-28 17:31:12 -070010599
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010600 if (onBattery != mOnBattery) {
10601 mHistoryCur.batteryLevel = (byte)level;
10602 mHistoryCur.batteryStatus = (byte)status;
10603 mHistoryCur.batteryHealth = (byte)health;
10604 mHistoryCur.batteryPlugType = (byte)plugType;
10605 mHistoryCur.batteryTemperature = (short)temp;
10606 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010607 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10608 // Only record discharges
10609 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10610 mDischargeCounter.addCountLocked(chargeDiff);
10611 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10612 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010613 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010614 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010615 } else {
10616 boolean changed = false;
10617 if (mHistoryCur.batteryLevel != level) {
10618 mHistoryCur.batteryLevel = (byte)level;
10619 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070010620
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010621 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
10622 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010623 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070010624 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010625 if (mHistoryCur.batteryStatus != status) {
10626 mHistoryCur.batteryStatus = (byte)status;
10627 changed = true;
10628 }
10629 if (mHistoryCur.batteryHealth != health) {
10630 mHistoryCur.batteryHealth = (byte)health;
10631 changed = true;
10632 }
10633 if (mHistoryCur.batteryPlugType != plugType) {
10634 mHistoryCur.batteryPlugType = (byte)plugType;
10635 changed = true;
10636 }
10637 if (temp >= (mHistoryCur.batteryTemperature+10)
10638 || temp <= (mHistoryCur.batteryTemperature-10)) {
10639 mHistoryCur.batteryTemperature = (short)temp;
10640 changed = true;
10641 }
10642 if (volt > (mHistoryCur.batteryVoltage+20)
10643 || volt < (mHistoryCur.batteryVoltage-20)) {
10644 mHistoryCur.batteryVoltage = (char)volt;
10645 changed = true;
10646 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010647 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
10648 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010649 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10650 // Only record discharges
10651 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10652 mDischargeCounter.addCountLocked(chargeDiff);
10653 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10654 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010655 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070010656 changed = true;
10657 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010658 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
10659 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
10660 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
10661 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010662 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010663 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
10664 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10665 modeBits, elapsedRealtime);
10666 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10667 modeBits, elapsedRealtime);
10668 mLastDischargeStepLevel = level;
10669 mMinDischargeStepLevel = level;
10670 mInitStepMode = mCurStepMode;
10671 mModStepMode = 0;
10672 }
10673 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010674 if (level >= 90) {
10675 // If the battery level is at least 90%, always consider the device to be
10676 // charging even if it happens to go down a level.
10677 changed |= setChargingLocked(true);
10678 mLastChargeStepLevel = level;
10679 } if (!mCharging) {
10680 if (mLastChargeStepLevel < level) {
10681 // We have not reporting that we are charging, but the level has now
10682 // gone up, so consider the state to be charging.
10683 changed |= setChargingLocked(true);
10684 mLastChargeStepLevel = level;
10685 }
10686 } else {
10687 if (mLastChargeStepLevel > level) {
10688 // We had reported that the device was charging, but here we are with
10689 // power connected and the level going down. Looks like the current
10690 // power supplied isn't enough, so consider the device to now be
10691 // discharging.
10692 changed |= setChargingLocked(false);
10693 mLastChargeStepLevel = level;
10694 }
10695 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010696 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
10697 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10698 modeBits, elapsedRealtime);
10699 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10700 modeBits, elapsedRealtime);
10701 mLastChargeStepLevel = level;
10702 mMaxChargeStepLevel = level;
10703 mInitStepMode = mCurStepMode;
10704 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070010705 }
10706 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010707 if (changed) {
10708 addHistoryRecordLocked(elapsedRealtime, uptime);
10709 }
Evan Millarc64edde2009-04-18 12:26:32 -070010710 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010711 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10712 // We don't record history while we are plugged in and fully charged.
10713 // The next time we are unplugged, history will be cleared.
10714 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010715 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010716
Jocelyn Dangc627d102017-04-14 13:15:14 -070010717 if (mMinLearnedBatteryCapacity == -1) {
10718 mMinLearnedBatteryCapacity = chargeFullUAh;
10719 } else {
10720 Math.min(mMinLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski041d9172016-12-12 12:03:56 -080010721 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070010722 mMaxLearnedBatteryCapacity = Math.max(mMaxLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski33dac552015-03-09 15:24:48 -070010723 }
10724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010725 public long getAwakeTimeBattery() {
10726 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
10727 }
10728
10729 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080010730 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 }
10732
10733 @Override
10734 public long computeUptime(long curTime, int which) {
10735 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010736 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010737 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010738 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010739 }
10740 return 0;
10741 }
10742
10743 @Override
10744 public long computeRealtime(long curTime, int which) {
10745 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010746 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010748 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 }
10750 return 0;
10751 }
10752
10753 @Override
10754 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010755 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756 }
10757
10758 @Override
10759 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010760 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 }
10762
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010763 @Override
10764 public long computeBatteryScreenOffUptime(long curTime, int which) {
10765 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
10766 }
10767
10768 @Override
10769 public long computeBatteryScreenOffRealtime(long curTime, int which) {
10770 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 }
10772
Dianne Hackborn260c5022014-04-29 11:23:16 -070010773 private long computeTimePerLevel(long[] steps, int numSteps) {
10774 // For now we'll do a simple average across all steps.
10775 if (numSteps <= 0) {
10776 return -1;
10777 }
10778 long total = 0;
10779 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010780 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010781 }
10782 return total / numSteps;
10783 /*
10784 long[] buckets = new long[numSteps];
10785 int numBuckets = 0;
10786 int numToAverage = 4;
10787 int i = 0;
10788 while (i < numSteps) {
10789 long totalTime = 0;
10790 int num = 0;
10791 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010792 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010793 num++;
10794 }
10795 buckets[numBuckets] = totalTime / num;
10796 numBuckets++;
10797 numToAverage *= 2;
10798 i += num;
10799 }
10800 if (numBuckets < 1) {
10801 return -1;
10802 }
10803 long averageTime = buckets[numBuckets-1];
10804 for (i=numBuckets-2; i>=0; i--) {
10805 averageTime = (averageTime + buckets[i]) / 2;
10806 }
10807 return averageTime;
10808 */
10809 }
10810
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010811 @Override
10812 public long computeBatteryTimeRemaining(long curTime) {
10813 if (!mOnBattery) {
10814 return -1;
10815 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010816 /* Simple implementation just looks at the average discharge per level across the
10817 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010818 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
10819 if (discharge < 2) {
10820 return -1;
10821 }
10822 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
10823 if (duration < 1000*1000) {
10824 return -1;
10825 }
10826 long usPerLevel = duration/discharge;
10827 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010828 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010829 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010830 return -1;
10831 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010832 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010833 if (msPerLevel <= 0) {
10834 return -1;
10835 }
10836 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010837 }
10838
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010839 @Override
10840 public LevelStepTracker getDischargeLevelStepTracker() {
10841 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010842 }
10843
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010844 @Override
10845 public LevelStepTracker getDailyDischargeLevelStepTracker() {
10846 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010847 }
10848
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010849 @Override
10850 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010851 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010852 // Not yet working.
10853 return -1;
10854 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010855 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010856 int curLevel = mCurrentBatteryLevel;
10857 int plugLevel = mDischargePlugLevel;
10858 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
10859 return -1;
10860 }
10861 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
10862 if (duration < 1000*1000) {
10863 return -1;
10864 }
10865 long usPerLevel = duration/(curLevel-plugLevel);
10866 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070010867 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010868 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010869 return -1;
10870 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010871 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010872 if (msPerLevel <= 0) {
10873 return -1;
10874 }
10875 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010876 }
10877
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010878 @Override
10879 public LevelStepTracker getChargeLevelStepTracker() {
10880 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010881 }
10882
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010883 @Override
10884 public LevelStepTracker getDailyChargeLevelStepTracker() {
10885 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010886 }
10887
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010888 @Override
10889 public ArrayList<PackageChange> getDailyPackageChanges() {
10890 return mDailyPackageChanges;
10891 }
10892
Joe Onoratoe1acd632016-02-23 13:25:10 -080010893 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010894 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010895 }
10896
10897 @Override
10898 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010899 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 }
10901
10902 @Override
10903 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010904 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010905 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070010906
The Android Open Source Project10592532009-03-18 17:39:46 -070010907 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010908 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010909 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010910 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010911 }
10912 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010913
Evan Millar633a1742009-04-02 16:36:33 -070010914 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010915 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010916 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010917
The Android Open Source Project10592532009-03-18 17:39:46 -070010918 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010919 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010920 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010921 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010922 }
10923 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010924
Evan Millar633a1742009-04-02 16:36:33 -070010925 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010926 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928
Amith Yamasanie43530a2009-08-21 13:11:37 -070010929 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010930 public int getLowDischargeAmountSinceCharge() {
10931 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010932 int val = mLowDischargeAmountSinceCharge;
10933 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10934 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
10935 }
10936 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010937 }
10938 }
10939
10940 @Override
10941 public int getHighDischargeAmountSinceCharge() {
10942 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010943 int val = mHighDischargeAmountSinceCharge;
10944 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10945 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
10946 }
10947 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010948 }
10949 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010950
10951 @Override
10952 public int getDischargeAmount(int which) {
10953 int dischargeAmount = which == STATS_SINCE_CHARGED
10954 ? getHighDischargeAmountSinceCharge()
10955 : (getDischargeStartLevel() - getDischargeCurrentLevel());
10956 if (dischargeAmount < 0) {
10957 dischargeAmount = 0;
10958 }
10959 return dischargeAmount;
10960 }
10961
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010962 public int getDischargeAmountScreenOn() {
10963 synchronized(this) {
10964 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010965 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010966 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10967 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10968 }
10969 return val;
10970 }
10971 }
10972
10973 public int getDischargeAmountScreenOnSinceCharge() {
10974 synchronized(this) {
10975 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010976 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010977 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10978 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10979 }
10980 return val;
10981 }
10982 }
10983
10984 public int getDischargeAmountScreenOff() {
10985 synchronized(this) {
10986 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010987 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010988 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10989 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10990 }
10991 return val;
10992 }
10993 }
10994
10995 public int getDischargeAmountScreenOffSinceCharge() {
10996 synchronized(this) {
10997 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010998 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010999 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11000 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11001 }
11002 return val;
11003 }
11004 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 /**
11007 * Retrieve the statistics object for a particular uid, creating if needed.
11008 */
11009 public Uid getUidStatsLocked(int uid) {
11010 Uid u = mUidStats.get(uid);
11011 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080011012 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 mUidStats.put(uid, u);
11014 }
11015 return u;
11016 }
11017
11018 /**
11019 * Remove the statistics object for a particular uid.
11020 */
11021 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070011022 mKernelUidCpuTimeReader.removeUid(uid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070011023 mKernelUidCpuFreqTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024 mUidStats.remove(uid);
11025 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070011026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011027 /**
11028 * Retrieve the statistics object for a particular process, creating
11029 * if needed.
11030 */
11031 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011032 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011033 Uid u = getUidStatsLocked(uid);
11034 return u.getProcessStatsLocked(name);
11035 }
11036
11037 /**
11038 * Retrieve the statistics object for a particular process, creating
11039 * if needed.
11040 */
11041 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
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.getPackageStatsLocked(pkg);
11045 }
11046
11047 /**
11048 * Retrieve the statistics object for a particular service, creating
11049 * if needed.
11050 */
11051 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
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.getServiceStatsLocked(pkg, name);
11055 }
11056
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011057 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011058 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011059 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011060 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011061 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011062
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011063 Parcel mPendingWrite = null;
11064 final ReentrantLock mWriteLock = new ReentrantLock();
11065
11066 public void writeAsyncLocked() {
11067 writeLocked(false);
11068 }
11069
11070 public void writeSyncLocked() {
11071 writeLocked(true);
11072 }
11073
11074 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011075 if (mFile == null) {
11076 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011077 return;
11078 }
11079
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011080 if (mShuttingDown) {
11081 return;
11082 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011083
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011084 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011085 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080011086 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011087
11088 if (mPendingWrite != null) {
11089 mPendingWrite.recycle();
11090 }
11091 mPendingWrite = out;
11092
11093 if (sync) {
11094 commitPendingDataToDisk();
11095 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011096 BackgroundThread.getHandler().post(new Runnable() {
11097 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011098 commitPendingDataToDisk();
11099 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011100 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011101 }
11102 }
11103
11104 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011105 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011106 synchronized (this) {
11107 next = mPendingWrite;
11108 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011109 if (next == null) {
11110 return;
11111 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011112 }
11113
Amith Yamasanid2450862017-02-07 15:58:24 -080011114 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011116 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011117 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011118 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070011119 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011121 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011122 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011123 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011124 mFile.rollback();
11125 } finally {
11126 next.recycle();
11127 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070011128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 }
11130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011132 if (mDailyFile != null) {
11133 readDailyStatsLocked();
11134 }
11135
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011136 if (mFile == null) {
11137 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 return;
11139 }
11140
11141 mUidStats.clear();
11142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011144 File file = mFile.chooseForRead();
11145 if (!file.exists()) {
11146 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011147 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011148 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011150 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 Parcel in = Parcel.obtain();
11152 in.unmarshall(raw, 0, raw.length);
11153 in.setDataPosition(0);
11154 stream.close();
11155
11156 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080011157 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011158 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011159 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011161
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011162 mEndPlatformVersion = Build.ID;
11163
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011164 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011165 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080011166 final long elapsedRealtime = mClocks.elapsedRealtime();
11167 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011168 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011169 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011170 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011171 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
11172 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011173 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011174
11175 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011176 }
11177
11178 public int describeContents() {
11179 return 0;
11180 }
11181
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011182 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070011183 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011184
11185 mHistoryBuffer.setDataSize(0);
11186 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011187 mHistoryTagPool.clear();
11188 mNextHistoryTagIdx = 0;
11189 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011190
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011191 int numTags = in.readInt();
11192 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080011193 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011194 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011195 if (str == null) {
11196 throw new ParcelFormatException("null history tag string");
11197 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011198 int uid = in.readInt();
11199 HistoryTag tag = new HistoryTag();
11200 tag.string = str;
11201 tag.uid = uid;
11202 tag.poolIdx = idx;
11203 mHistoryTagPool.put(tag, idx);
11204 if (idx >= mNextHistoryTagIdx) {
11205 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011206 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011207 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011208 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011209
11210 int bufSize = in.readInt();
11211 int curPos = in.dataPosition();
11212 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011213 throw new ParcelFormatException("File corrupt: history data buffer too large " +
11214 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011215 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011216 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
11217 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011218 } else {
11219 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
11220 + " bytes at " + curPos);
11221 mHistoryBuffer.appendFrom(in, curPos, bufSize);
11222 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011223 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011224
Dianne Hackbornae384452011-06-28 12:33:48 -070011225 if (andOldHistory) {
11226 readOldHistory(in);
11227 }
11228
11229 if (DEBUG_HISTORY) {
11230 StringBuilder sb = new StringBuilder(128);
11231 sb.append("****************** OLD mHistoryBaseTime: ");
11232 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11233 Slog.i(TAG, sb.toString());
11234 }
11235 mHistoryBaseTime = historyBaseTime;
11236 if (DEBUG_HISTORY) {
11237 StringBuilder sb = new StringBuilder(128);
11238 sb.append("****************** NEW mHistoryBaseTime: ");
11239 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11240 Slog.i(TAG, sb.toString());
11241 }
11242
11243 // We are just arbitrarily going to insert 1 minute from the sample of
11244 // the last run until samples in this run.
11245 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080011246 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011247 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070011248 if (DEBUG_HISTORY) {
11249 StringBuilder sb = new StringBuilder(128);
11250 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
11251 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11252 Slog.i(TAG, sb.toString());
11253 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070011254 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011256
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011257 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011258 if (!USE_OLD_HISTORY) {
11259 return;
11260 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011261 mHistory = mHistoryEnd = mHistoryCache = null;
11262 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070011263 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011264 HistoryItem rec = new HistoryItem(time, in);
11265 addHistoryRecordLocked(rec);
11266 }
11267 }
11268
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011269 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070011270 if (DEBUG_HISTORY) {
11271 StringBuilder sb = new StringBuilder(128);
11272 sb.append("****************** WRITING mHistoryBaseTime: ");
11273 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070011274 sb.append(" mLastHistoryElapsedRealtime: ");
11275 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070011276 Slog.i(TAG, sb.toString());
11277 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011278 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011279 if (!inclData) {
11280 out.writeInt(0);
11281 out.writeInt(0);
11282 return;
11283 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011284 out.writeInt(mHistoryTagPool.size());
11285 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
11286 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080011287 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011288 out.writeString(tag.string);
11289 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080011290 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011291 out.writeInt(mHistoryBuffer.dataSize());
11292 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
11293 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
11294 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070011295
11296 if (andOldHistory) {
11297 writeOldHistory(out);
11298 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011299 }
11300
11301 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011302 if (!USE_OLD_HISTORY) {
11303 return;
11304 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011305 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011306 while (rec != null) {
11307 if (rec.time >= 0) rec.writeToParcel(out, 0);
11308 rec = rec.next;
11309 }
11310 out.writeLong(-1);
11311 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011312
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011313 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011314 final int version = in.readInt();
11315 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011316 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011317 + ", expected " + VERSION + "; erasing old stats");
11318 return;
11319 }
11320
Dianne Hackbornae384452011-06-28 12:33:48 -070011321 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011323 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011324 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011325 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011326 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011327 mStartPlatformVersion = in.readString();
11328 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011329 mOnBatteryTimeBase.readSummaryFromParcel(in);
11330 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011331 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011332 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011333 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011334 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011335 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070011336 mMinLearnedBatteryCapacity = in.readInt();
11337 mMaxLearnedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011338 mLowDischargeAmountSinceCharge = in.readInt();
11339 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011340 mDischargeAmountScreenOnSinceCharge = in.readInt();
11341 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011342 mDischargeStepTracker.readFromParcel(in);
11343 mChargeStepTracker.readFromParcel(in);
11344 mDailyDischargeStepTracker.readFromParcel(in);
11345 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011346 mDischargeCounter.readSummaryFromParcelLocked(in);
11347 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011348 int NPKG = in.readInt();
11349 if (NPKG > 0) {
11350 mDailyPackageChanges = new ArrayList<>(NPKG);
11351 while (NPKG > 0) {
11352 NPKG--;
11353 PackageChange pc = new PackageChange();
11354 pc.mPackageName = in.readString();
11355 pc.mUpdate = in.readInt() != 0;
11356 pc.mVersionCode = in.readInt();
11357 mDailyPackageChanges.add(pc);
11358 }
11359 } else {
11360 mDailyPackageChanges = null;
11361 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011362 mDailyStartTime = in.readLong();
11363 mNextMinDailyDeadline = in.readLong();
11364 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011367
Jeff Browne95c3cd2014-05-02 16:59:26 -070011368 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011370 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11371 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
11372 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011373 mInteractive = false;
11374 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011375 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011376 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011377 mLongestLightIdleTime = in.readLong();
11378 mLongestFullIdleTime = in.readLong();
11379 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
11380 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
11381 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011382 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011383 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080011384 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070011385 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11386 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011387 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011388 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11389 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
11390 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011391 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011392 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11393 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011394 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011395 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011396 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080011397 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011398 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011399 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
11400 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011401 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070011402 mWifiOn = false;
11403 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011404 mGlobalWifiRunning = false;
11405 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011406 for (int i=0; i<NUM_WIFI_STATES; i++) {
11407 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
11408 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011409 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11410 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
11411 }
11412 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11413 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11414 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011415 mWifiActivity.readSummaryFromParcel(in);
11416 mBluetoothActivity.readSummaryFromParcel(in);
11417 mModemActivity.readSummaryFromParcel(in);
11418 mHasWifiReporting = in.readInt() != 0;
11419 mHasBluetoothReporting = in.readInt() != 0;
11420 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070011421
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011422 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011423 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011424 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011425 mCameraOnNesting = 0;
11426 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011427 mBluetoothScanNesting = 0;
11428 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011429
Evan Millarc64edde2009-04-18 12:26:32 -070011430 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011431 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011432 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011433 }
Evan Millarc64edde2009-04-18 12:26:32 -070011434 for (int ikw = 0; ikw < NKW; ikw++) {
11435 if (in.readInt() != 0) {
11436 String kwltName = in.readString();
11437 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
11438 }
11439 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011440
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011441 int NWR = in.readInt();
11442 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011443 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011444 }
11445 for (int iwr = 0; iwr < NWR; iwr++) {
11446 if (in.readInt() != 0) {
11447 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011448 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011449 }
11450 }
11451
James Carr3a226052016-07-01 14:49:52 -070011452 int NMS = in.readInt();
11453 for (int ims = 0; ims < NMS; ims++) {
11454 if (in.readInt() != 0) {
11455 long kmstName = in.readLong();
11456 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
11457 }
11458 }
11459
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011460 mCpuFreqs = in.createLongArray();
11461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011462 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011463 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011464 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011466 for (int iu = 0; iu < NU; iu++) {
11467 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011468 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 mUidStats.put(uid, u);
11470
Bookatz867c0d72017-03-07 18:23:42 -080011471 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -070011472 u.mOnBatteryScreenOffBackgroundTimeBase.readSummaryFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -080011473
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011474 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011475 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011476 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011477 }
The Android Open Source Project10592532009-03-18 17:39:46 -070011478 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011479 if (in.readInt() != 0) {
11480 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
11481 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011482 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011483 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070011484 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011485 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011486 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
11487 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11488 if (in.readInt() != 0) {
11489 u.makeWifiBatchedScanBin(i, null);
11490 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
11491 }
11492 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011493 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011494 if (in.readInt() != 0) {
11495 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
11496 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011497 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011498 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011499 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011500 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011501 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11502 }
11503 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011504 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11505 }
11506 if (in.readInt() != 0) {
11507 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11508 }
11509 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011510 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
11511 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011512 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -070011513 u.createAggregatedPartialWakelockTimerLocked().readSummaryFromParcelLocked(in);
11514 }
11515 if (in.readInt() != 0) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011516 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
11517 }
Bookatz956f36bf2017-04-28 09:48:17 -070011518 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -070011519 u.createBluetoothUnoptimizedScanTimerLocked().readSummaryFromParcelLocked(in);
11520 }
11521 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -070011522 u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
11523 }
Bookatzb1f04f32017-05-19 13:57:32 -070011524 if (in.readInt() != 0) {
11525 u.createBluetoothScanResultBgCounterLocked().readSummaryFromParcelLocked(in);
11526 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080011527 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070011528 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11529 if (in.readInt() != 0) {
11530 u.makeProcessState(i, null);
11531 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
11532 }
11533 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011534 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011535 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011536 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011537
Dianne Hackborn617f8772009-03-31 15:04:46 -070011538 if (in.readInt() != 0) {
11539 if (u.mUserActivityCounters == null) {
11540 u.initUserActivityLocked();
11541 }
11542 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11543 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
11544 }
11545 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011546
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011547 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011548 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011549 u.initNetworkActivityLocked();
11550 }
11551 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011552 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11553 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011554 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011555 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
11556 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011557 }
11558
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011559 u.mUserCpuTime.readSummaryFromParcelLocked(in);
11560 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
11561
Adam Lesinski6832f392015-09-05 18:05:40 -070011562 if (in.readInt() != 0) {
11563 final int numClusters = in.readInt();
11564 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
11565 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011566 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011567
11568 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
11569 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070011570 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070011571 final int NSB = in.readInt();
11572 if (mPowerProfile != null &&
11573 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
11574 throw new ParcelFormatException("File corrupt: too many speed bins " +
11575 NSB);
11576 }
11577
Adam Lesinski6832f392015-09-05 18:05:40 -070011578 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
11579 for (int speed = 0; speed < NSB; speed++) {
11580 if (in.readInt() != 0) {
11581 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
11582 mOnBatteryTimeBase);
11583 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
11584 }
11585 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070011586 } else {
11587 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070011588 }
11589 }
11590 } else {
11591 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011592 }
11593
Sudheer Shanka59f5c002017-05-15 10:57:15 -070011594 u.mCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
11595 in, mOnBatteryTimeBase);
11596 u.mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
11597 in, mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011598
11599 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -070011600 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11601 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
11602 } else {
11603 u.mMobileRadioApWakeupCount = null;
11604 }
11605
11606 if (in.readInt() != 0) {
11607 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11608 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
11609 } else {
11610 u.mWifiRadioApWakeupCount = null;
11611 }
11612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011613 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011614 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011615 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 for (int iw = 0; iw < NW; iw++) {
11618 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011619 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011620 }
11621
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011622 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011623 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011624 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011625 }
11626 for (int is = 0; is < NS; is++) {
11627 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011628 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011629 }
11630
11631 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011632 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011633 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011634 }
11635 for (int ij = 0; ij < NJ; ij++) {
11636 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011637 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011638 }
11639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011640 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011641 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011642 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011644 for (int is = 0; is < NP; is++) {
11645 int seNumber = in.readInt();
11646 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080011647 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011648 }
11649 }
11650
11651 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011652 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011653 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011655 for (int ip = 0; ip < NP; ip++) {
11656 String procName = in.readString();
11657 Uid.Proc p = u.getProcessStatsLocked(procName);
11658 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011659 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011660 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011661 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011662 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
11663 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011664 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011665 }
11666
11667 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011668 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011669 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011671 for (int ip = 0; ip < NP; ip++) {
11672 String pkgName = in.readString();
11673 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011674 final int NWA = in.readInt();
11675 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011676 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011677 }
11678 p.mWakeupAlarms.clear();
11679 for (int iwa=0; iwa<NWA; iwa++) {
11680 String tag = in.readString();
11681 Counter c = new Counter(mOnBatteryTimeBase);
11682 c.readSummaryFromParcelLocked(in);
11683 p.mWakeupAlarms.put(tag, c);
11684 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011685 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011686 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011687 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011689 for (int is = 0; is < NS; is++) {
11690 String servName = in.readString();
11691 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
11692 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011694 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 }
11696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011697 }
11698 }
11699
11700 /**
11701 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
11702 * disk. This format does not allow a lossless round-trip.
11703 *
11704 * @param out the Parcel to be written to.
11705 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011706 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011707 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011708
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011709 // Pull the clock time. This may update the time and make a new history entry
11710 // if we had originally pulled a time before the RTC was set.
11711 long startClockTime = getStartClockTime();
11712
Joe Onoratoabded112016-02-08 16:49:39 -080011713 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
11714 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011715
11716 out.writeInt(VERSION);
11717
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011718 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011720 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011721 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011722 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011723 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011724 out.writeString(mStartPlatformVersion);
11725 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011726 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11727 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011728 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011729 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011730 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011731 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011732 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070011733 out.writeInt(mMinLearnedBatteryCapacity);
11734 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011735 out.writeInt(getLowDischargeAmountSinceCharge());
11736 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011737 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11738 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011739 mDischargeStepTracker.writeToParcel(out);
11740 mChargeStepTracker.writeToParcel(out);
11741 mDailyDischargeStepTracker.writeToParcel(out);
11742 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011743 mDischargeCounter.writeSummaryFromParcelLocked(out);
11744 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011745 if (mDailyPackageChanges != null) {
11746 final int NPKG = mDailyPackageChanges.size();
11747 out.writeInt(NPKG);
11748 for (int i=0; i<NPKG; i++) {
11749 PackageChange pc = mDailyPackageChanges.get(i);
11750 out.writeString(pc.mPackageName);
11751 out.writeInt(pc.mUpdate ? 1 : 0);
11752 out.writeInt(pc.mVersionCode);
11753 }
11754 } else {
11755 out.writeInt(0);
11756 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011757 out.writeLong(mDailyStartTime);
11758 out.writeLong(mNextMinDailyDeadline);
11759 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011760
11761 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011762 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011763 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011764 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011765 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011766 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011767 out.writeLong(mLongestLightIdleTime);
11768 out.writeLong(mLongestFullIdleTime);
11769 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11770 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11771 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011772 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011773 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011774 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011775 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011776 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011777 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011778 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011779 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011780 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011781 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011782 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11783 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011784 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011785 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11786 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011787 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011788 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11789 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011790 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11791 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011792 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011793 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011794 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011795 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11796 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11797 }
11798 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11799 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11800 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011801 mWifiActivity.writeSummaryToParcel(out);
11802 mBluetoothActivity.writeSummaryToParcel(out);
11803 mModemActivity.writeSummaryToParcel(out);
11804 out.writeInt(mHasWifiReporting ? 1 : 0);
11805 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11806 out.writeInt(mHasModemReporting ? 1 : 0);
11807
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011808 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011809 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011810 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011811 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011812
Evan Millarc64edde2009-04-18 12:26:32 -070011813 out.writeInt(mKernelWakelockStats.size());
11814 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11815 Timer kwlt = ent.getValue();
11816 if (kwlt != null) {
11817 out.writeInt(1);
11818 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011819 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11820 } else {
11821 out.writeInt(0);
11822 }
11823 }
11824
11825 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011826 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11827 SamplingTimer timer = ent.getValue();
11828 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011829 out.writeInt(1);
11830 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011831 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070011832 } else {
11833 out.writeInt(0);
11834 }
11835 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011836
James Carr3a226052016-07-01 14:49:52 -070011837 out.writeInt(mKernelMemoryStats.size());
11838 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11839 Timer kmt = mKernelMemoryStats.valueAt(i);
11840 if (kmt != null) {
11841 out.writeInt(1);
11842 out.writeLong(mKernelMemoryStats.keyAt(i));
11843 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11844 } else {
11845 out.writeInt(0);
11846 }
11847 }
11848
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011849 out.writeLongArray(mCpuFreqs);
11850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011851 final int NU = mUidStats.size();
11852 out.writeInt(NU);
11853 for (int iu = 0; iu < NU; iu++) {
11854 out.writeInt(mUidStats.keyAt(iu));
11855 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011856
Bookatz867c0d72017-03-07 18:23:42 -080011857 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatzc8c44962017-05-11 12:12:54 -070011858 u.mOnBatteryScreenOffBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatz867c0d72017-03-07 18:23:42 -080011859
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011860 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011861 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011862 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011863 } else {
11864 out.writeInt(0);
11865 }
11866 if (u.mFullWifiLockTimer != null) {
11867 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011868 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011869 } else {
11870 out.writeInt(0);
11871 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011872 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011873 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011874 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011875 } else {
11876 out.writeInt(0);
11877 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011878 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11879 if (u.mWifiBatchedScanTimer[i] != null) {
11880 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011881 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070011882 } else {
11883 out.writeInt(0);
11884 }
11885 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011886 if (u.mWifiMulticastTimer != null) {
11887 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011888 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011889 } else {
11890 out.writeInt(0);
11891 }
11892 if (u.mAudioTurnedOnTimer != null) {
11893 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011894 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011895 } else {
11896 out.writeInt(0);
11897 }
11898 if (u.mVideoTurnedOnTimer != null) {
11899 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011900 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011901 } else {
11902 out.writeInt(0);
11903 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011904 if (u.mFlashlightTurnedOnTimer != null) {
11905 out.writeInt(1);
11906 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11907 } else {
11908 out.writeInt(0);
11909 }
11910 if (u.mCameraTurnedOnTimer != null) {
11911 out.writeInt(1);
11912 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11913 } else {
11914 out.writeInt(0);
11915 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011916 if (u.mForegroundActivityTimer != null) {
11917 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011918 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011919 } else {
11920 out.writeInt(0);
11921 }
Bookatzc8c44962017-05-11 12:12:54 -070011922 if (u.mAggregatedPartialWakelockTimer != null) {
11923 out.writeInt(1);
11924 u.mAggregatedPartialWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11925 } else {
11926 out.writeInt(0);
11927 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011928 if (u.mBluetoothScanTimer != null) {
11929 out.writeInt(1);
11930 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11931 } else {
11932 out.writeInt(0);
11933 }
Bookatzb1f04f32017-05-19 13:57:32 -070011934 if (u.mBluetoothUnoptimizedScanTimer != null) {
11935 out.writeInt(1);
11936 u.mBluetoothUnoptimizedScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11937 } else {
11938 out.writeInt(0);
11939 }
Bookatz956f36bf2017-04-28 09:48:17 -070011940 if (u.mBluetoothScanResultCounter != null) {
11941 out.writeInt(1);
11942 u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
11943 } else {
11944 out.writeInt(0);
11945 }
Bookatzb1f04f32017-05-19 13:57:32 -070011946 if (u.mBluetoothScanResultBgCounter != null) {
11947 out.writeInt(1);
11948 u.mBluetoothScanResultBgCounter.writeSummaryFromParcelLocked(out);
11949 } else {
11950 out.writeInt(0);
11951 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070011952 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11953 if (u.mProcessStateTimer[i] != null) {
11954 out.writeInt(1);
11955 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11956 } else {
11957 out.writeInt(0);
11958 }
11959 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011960 if (u.mVibratorOnTimer != null) {
11961 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011962 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011963 } else {
11964 out.writeInt(0);
11965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011966
Dianne Hackborn617f8772009-03-31 15:04:46 -070011967 if (u.mUserActivityCounters == null) {
11968 out.writeInt(0);
11969 } else {
11970 out.writeInt(1);
11971 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11972 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
11973 }
11974 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011975
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011976 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011977 out.writeInt(0);
11978 } else {
11979 out.writeInt(1);
11980 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011981 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11982 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011983 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011984 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
11985 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011986 }
11987
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011988 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
11989 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
11990
Adam Lesinski6832f392015-09-05 18:05:40 -070011991 if (u.mCpuClusterSpeed != null) {
11992 out.writeInt(1);
11993 out.writeInt(u.mCpuClusterSpeed.length);
11994 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
11995 if (cpuSpeeds != null) {
11996 out.writeInt(1);
11997 out.writeInt(cpuSpeeds.length);
11998 for (LongSamplingCounter c : cpuSpeeds) {
11999 if (c != null) {
12000 out.writeInt(1);
12001 c.writeSummaryFromParcelLocked(out);
12002 } else {
12003 out.writeInt(0);
12004 }
12005 }
12006 } else {
12007 out.writeInt(0);
12008 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012009 }
Adam Lesinski6832f392015-09-05 18:05:40 -070012010 } else {
12011 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012012 }
12013
Sudheer Shanka59f5c002017-05-15 10:57:15 -070012014 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mCpuFreqTimeMs);
12015 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012016
Adam Lesinski5f056f62016-07-14 16:56:08 -070012017 if (u.mMobileRadioApWakeupCount != null) {
12018 out.writeInt(1);
12019 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12020 } else {
12021 out.writeInt(0);
12022 }
12023
12024 if (u.mWifiRadioApWakeupCount != null) {
12025 out.writeInt(1);
12026 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12027 } else {
12028 out.writeInt(0);
12029 }
12030
Dianne Hackbornd953c532014-08-16 18:17:38 -070012031 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
12032 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012033 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012034 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012035 out.writeString(wakeStats.keyAt(iw));
12036 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012037 if (wl.mTimerFull != null) {
12038 out.writeInt(1);
12039 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12040 } else {
12041 out.writeInt(0);
12042 }
12043 if (wl.mTimerPartial != null) {
12044 out.writeInt(1);
12045 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12046 } else {
12047 out.writeInt(0);
12048 }
12049 if (wl.mTimerWindow != null) {
12050 out.writeInt(1);
12051 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12052 } else {
12053 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012055 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070012056 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012057 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070012058 } else {
12059 out.writeInt(0);
12060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012061 }
12062
Bookatz2bffb5b2017-04-13 11:59:33 -070012063 final ArrayMap<String, DualTimer> syncStats = u.mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012064 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012065 out.writeInt(NS);
12066 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012067 out.writeString(syncStats.keyAt(is));
12068 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012069 }
12070
Bookatzaa4594a2017-03-24 12:39:56 -070012071 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012072 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012073 out.writeInt(NJ);
12074 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012075 out.writeString(jobStats.keyAt(ij));
12076 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012077 }
12078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012079 int NSE = u.mSensorStats.size();
12080 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012081 for (int ise=0; ise<NSE; ise++) {
12082 out.writeInt(u.mSensorStats.keyAt(ise));
12083 Uid.Sensor se = u.mSensorStats.valueAt(ise);
12084 if (se.mTimer != null) {
12085 out.writeInt(1);
12086 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12087 } else {
12088 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012089 }
12090 }
12091
12092 int NP = u.mProcessStats.size();
12093 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012094 for (int ip=0; ip<NP; ip++) {
12095 out.writeString(u.mProcessStats.keyAt(ip));
12096 Uid.Proc ps = u.mProcessStats.valueAt(ip);
12097 out.writeLong(ps.mUserTime);
12098 out.writeLong(ps.mSystemTime);
12099 out.writeLong(ps.mForegroundTime);
12100 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012101 out.writeInt(ps.mNumCrashes);
12102 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012103 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012104 }
12105
12106 NP = u.mPackageStats.size();
12107 out.writeInt(NP);
12108 if (NP > 0) {
12109 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
12110 : u.mPackageStats.entrySet()) {
12111 out.writeString(ent.getKey());
12112 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012113 final int NWA = ps.mWakeupAlarms.size();
12114 out.writeInt(NWA);
12115 for (int iwa=0; iwa<NWA; iwa++) {
12116 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
12117 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
12118 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012119 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012120 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012121 for (int is=0; is<NS; is++) {
12122 out.writeString(ps.mServiceStats.keyAt(is));
12123 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
12124 long time = ss.getStartTimeToNowLocked(
12125 mOnBatteryTimeBase.getUptime(NOW_SYS));
12126 out.writeLong(time);
12127 out.writeInt(ss.mStarts);
12128 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012129 }
12130 }
12131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012132 }
12133 }
12134
12135 public void readFromParcel(Parcel in) {
12136 readFromParcelLocked(in);
12137 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012139 void readFromParcelLocked(Parcel in) {
12140 int magic = in.readInt();
12141 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012142 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012143 }
12144
Dianne Hackbornae384452011-06-28 12:33:48 -070012145 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012147 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012148 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012149 mStartPlatformVersion = in.readString();
12150 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012151 mUptime = in.readLong();
12152 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012153 mRealtime = in.readLong();
12154 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012155 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012156 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070012157 mMinLearnedBatteryCapacity = in.readInt();
12158 mMaxLearnedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012159 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012160 mOnBatteryTimeBase.readFromParcel(in);
12161 mOnBatteryScreenOffTimeBase.readFromParcel(in);
12162
Jeff Browne95c3cd2014-05-02 16:59:26 -070012163 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080012164 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012165 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012166 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
12167 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012168 }
Dianne Hackborn29325132014-05-21 15:01:03 -070012169 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012170 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012171 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012172 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
12173 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012174 mLongestLightIdleTime = in.readLong();
12175 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080012176 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
12177 mOnBatteryTimeBase, in);
12178 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
12179 mOnBatteryTimeBase, in);
12180 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
12181 mOnBatteryTimeBase, in);
12182 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
12183 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012184 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012185 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012186 null, mOnBatteryTimeBase, in);
12187 }
Joe Onoratoabded112016-02-08 16:49:39 -080012188 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
12189 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012190 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012191 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012192 null, mOnBatteryTimeBase, in);
12193 }
12194 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12195 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12196 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12197 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012198 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080012199 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
12200 mOnBatteryTimeBase, in);
12201 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
12202 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012203 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012204 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
12205 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012206 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012207 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012208 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012209 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012210 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
12211 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012212 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012213 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012214 null, mOnBatteryTimeBase, in);
12215 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012216 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012217 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012218 null, mOnBatteryTimeBase, in);
12219 }
12220 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012221 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012222 null, mOnBatteryTimeBase, in);
12223 }
Adam Lesinski33dac552015-03-09 15:24:48 -070012224
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012225 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12226 NUM_WIFI_TX_LEVELS, in);
12227 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12228 NUM_BT_TX_LEVELS, in);
12229 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12230 ModemActivityInfo.TX_POWER_LEVELS, in);
12231 mHasWifiReporting = in.readInt() != 0;
12232 mHasBluetoothReporting = in.readInt() != 0;
12233 mHasModemReporting = in.readInt() != 0;
12234
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012235 mNumConnectivityChange = in.readInt();
12236 mLoadedNumConnectivityChange = in.readInt();
12237 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012238 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012239 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012240 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012241 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012242 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012243 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012244 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012245 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012246 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012247 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012248 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012249 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070012250 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012251 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012252 mLowDischargeAmountSinceCharge = in.readInt();
12253 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012254 mDischargeAmountScreenOn = in.readInt();
12255 mDischargeAmountScreenOnSinceCharge = in.readInt();
12256 mDischargeAmountScreenOff = in.readInt();
12257 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012258 mDischargeStepTracker.readFromParcel(in);
12259 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012260 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
12261 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012262 mLastWriteTime = in.readLong();
12263
Evan Millarc64edde2009-04-18 12:26:32 -070012264 mKernelWakelockStats.clear();
12265 int NKW = in.readInt();
12266 for (int ikw = 0; ikw < NKW; ikw++) {
12267 if (in.readInt() != 0) {
12268 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012269 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070012270 mKernelWakelockStats.put(wakelockName, kwlt);
12271 }
12272 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012273
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012274 mWakeupReasonStats.clear();
12275 int NWR = in.readInt();
12276 for (int iwr = 0; iwr < NWR; iwr++) {
12277 if (in.readInt() != 0) {
12278 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012279 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012280 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012281 }
12282 }
12283
James Carr3a226052016-07-01 14:49:52 -070012284 mKernelMemoryStats.clear();
12285 int nmt = in.readInt();
12286 for (int imt = 0; imt < nmt; imt++) {
12287 if (in.readInt() != 0) {
12288 Long bucket = in.readLong();
12289 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
12290 mKernelMemoryStats.put(bucket, kmt);
12291 }
12292 }
12293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012294 mPartialTimers.clear();
12295 mFullTimers.clear();
12296 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012297 mWifiRunningTimers.clear();
12298 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070012299 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070012300 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012301 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012302 mAudioTurnedOnTimers.clear();
12303 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012304 mFlashlightTurnedOnTimers.clear();
12305 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012306
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012307 mCpuFreqs = in.createLongArray();
12308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012309 int numUids = in.readInt();
12310 mUidStats.clear();
12311 for (int i = 0; i < numUids; i++) {
12312 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080012313 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012314 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012315 mUidStats.append(uid, u);
12316 }
12317 }
12318
12319 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012320 writeToParcelLocked(out, true, flags);
12321 }
12322
12323 public void writeToParcelWithoutUids(Parcel out, int flags) {
12324 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012325 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012326
12327 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012328 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012329 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012330 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012331
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012332 // Pull the clock time. This may update the time and make a new history entry
12333 // if we had originally pulled a time before the RTC was set.
12334 long startClockTime = getStartClockTime();
12335
Joe Onoratoabded112016-02-08 16:49:39 -080012336 final long uSecUptime = mClocks.uptimeMillis() * 1000;
12337 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012338 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
12339 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012341 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012342
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012343 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012345 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012346 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012347 out.writeString(mStartPlatformVersion);
12348 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012349 out.writeLong(mUptime);
12350 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012351 out.writeLong(mRealtime);
12352 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012353 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012354 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070012355 out.writeInt(mMinLearnedBatteryCapacity);
12356 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012357 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12358 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12359
12360 mScreenOnTimer.writeToParcel(out, uSecRealtime);
12361 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
12362 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
12363 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012364 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012365 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012366 out.writeLong(mLongestLightIdleTime);
12367 out.writeLong(mLongestFullIdleTime);
12368 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
12369 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
12370 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012371 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012372 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
12373 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
12374 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
12375 }
12376 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
12377 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
12378 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
12379 }
12380 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12381 mNetworkByteActivityCounters[i].writeToParcel(out);
12382 mNetworkPacketActivityCounters[i].writeToParcel(out);
12383 }
12384 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
12385 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012386 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012387 mMobileRadioActiveUnknownTime.writeToParcel(out);
12388 mMobileRadioActiveUnknownCount.writeToParcel(out);
12389 mWifiOnTimer.writeToParcel(out, uSecRealtime);
12390 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
12391 for (int i=0; i<NUM_WIFI_STATES; i++) {
12392 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
12393 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012394 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12395 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
12396 }
12397 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12398 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
12399 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012400 mWifiActivity.writeToParcel(out, 0);
12401 mBluetoothActivity.writeToParcel(out, 0);
12402 mModemActivity.writeToParcel(out, 0);
12403 out.writeInt(mHasWifiReporting ? 1 : 0);
12404 out.writeInt(mHasBluetoothReporting ? 1 : 0);
12405 out.writeInt(mHasModemReporting ? 1 : 0);
12406
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012407 out.writeInt(mNumConnectivityChange);
12408 out.writeInt(mLoadedNumConnectivityChange);
12409 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012410 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012411 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012412 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012413 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012414 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070012415 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012416 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012417 out.writeInt(mLowDischargeAmountSinceCharge);
12418 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012419 out.writeInt(mDischargeAmountScreenOn);
12420 out.writeInt(mDischargeAmountScreenOnSinceCharge);
12421 out.writeInt(mDischargeAmountScreenOff);
12422 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012423 mDischargeStepTracker.writeToParcel(out);
12424 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012425 mDischargeCounter.writeToParcel(out);
12426 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012427 out.writeLong(mLastWriteTime);
12428
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012429 if (inclUids) {
12430 out.writeInt(mKernelWakelockStats.size());
12431 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
12432 SamplingTimer kwlt = ent.getValue();
12433 if (kwlt != null) {
12434 out.writeInt(1);
12435 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012436 kwlt.writeToParcel(out, uSecRealtime);
12437 } else {
12438 out.writeInt(0);
12439 }
12440 }
12441 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012442 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
12443 SamplingTimer timer = ent.getValue();
12444 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012445 out.writeInt(1);
12446 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012447 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012448 } else {
12449 out.writeInt(0);
12450 }
Evan Millarc64edde2009-04-18 12:26:32 -070012451 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012452 } else {
12453 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070012454 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070012455
James Carr3a226052016-07-01 14:49:52 -070012456 out.writeInt(mKernelMemoryStats.size());
12457 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
12458 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
12459 if (kmt != null) {
12460 out.writeInt(1);
12461 out.writeLong(mKernelMemoryStats.keyAt(i));
12462 kmt.writeToParcel(out, uSecRealtime);
12463 } else {
12464 out.writeInt(0);
12465 }
12466 }
12467
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012468 out.writeLongArray(mCpuFreqs);
12469
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012470 if (inclUids) {
12471 int size = mUidStats.size();
12472 out.writeInt(size);
12473 for (int i = 0; i < size; i++) {
12474 out.writeInt(mUidStats.keyAt(i));
12475 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476
Bookatz867c0d72017-03-07 18:23:42 -080012477 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012478 }
12479 } else {
12480 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012481 }
12482 }
12483
12484 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
12485 new Parcelable.Creator<BatteryStatsImpl>() {
12486 public BatteryStatsImpl createFromParcel(Parcel in) {
12487 return new BatteryStatsImpl(in);
12488 }
12489
12490 public BatteryStatsImpl[] newArray(int size) {
12491 return new BatteryStatsImpl[size];
12492 }
12493 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012494
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012495 public void prepareForDumpLocked() {
12496 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012497 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012498
12499 // Pull the clock time. This may update the time and make a new history entry
12500 // if we had originally pulled a time before the RTC was set.
12501 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012502 }
12503
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012504 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012505 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012506 pw.println("mOnBatteryTimeBase:");
12507 mOnBatteryTimeBase.dump(pw, " ");
12508 pw.println("mOnBatteryScreenOffTimeBase:");
12509 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012510 Printer pr = new PrintWriterPrinter(pw);
12511 pr.println("*** Screen timer:");
12512 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070012513 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012514 pr.println("*** Screen brightness #" + i + ":");
12515 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070012516 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012517 pr.println("*** Interactive timer:");
12518 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012519 pr.println("*** Power save mode timer:");
12520 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012521 pr.println("*** Device idle mode light timer:");
12522 mDeviceIdleModeLightTimer.logState(pr, " ");
12523 pr.println("*** Device idle mode full timer:");
12524 mDeviceIdleModeFullTimer.logState(pr, " ");
12525 pr.println("*** Device light idling timer:");
12526 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012527 pr.println("*** Device idling timer:");
12528 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012529 pr.println("*** Phone timer:");
12530 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080012531 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070012532 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012533 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012534 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070012535 pr.println("*** Signal scanning :");
12536 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012537 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012538 pr.println("*** Data connection type #" + i + ":");
12539 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012540 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012541 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080012542 pr.println("*** Mobile network active timer:");
12543 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012544 pr.println("*** Mobile network active adjusted timer:");
12545 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012546 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012547 pr.println("*** Wifi timer:");
12548 mWifiOnTimer.logState(pr, " ");
12549 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012550 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012551 for (int i=0; i<NUM_WIFI_STATES; i++) {
12552 pr.println("*** Wifi state #" + i + ":");
12553 mWifiStateTimer[i].logState(pr, " ");
12554 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012555 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12556 pr.println("*** Wifi suppl state #" + i + ":");
12557 mWifiSupplStateTimer[i].logState(pr, " ");
12558 }
12559 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12560 pr.println("*** Wifi signal strength #" + i + ":");
12561 mWifiSignalStrengthsTimer[i].logState(pr, " ");
12562 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012563 pr.println("*** Flashlight timer:");
12564 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012565 pr.println("*** Camera timer:");
12566 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012567 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012568 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012570}