The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.os; |
| 18 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 19 | import static android.text.format.DateUtils.SECOND_IN_MILLIS; |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 20 | import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 21 | |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 22 | import android.bluetooth.BluetoothDevice; |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 23 | import android.bluetooth.BluetoothHeadset; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
| 25 | import android.net.NetworkStats; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 26 | import android.os.BatteryManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.BatteryStats; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 28 | import android.os.FileUtils; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.Parcel; |
| 32 | import android.os.ParcelFormatException; |
| 33 | import android.os.Parcelable; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 34 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.SystemClock; |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 36 | import android.os.SystemProperties; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 37 | import android.os.WorkSource; |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 38 | import android.telephony.ServiceState; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 39 | import android.telephony.SignalStrength; |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 40 | import android.telephony.TelephonyManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 42 | import android.util.LogWriter; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 43 | import android.util.PrintWriterPrinter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.util.Printer; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 45 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.util.SparseArray; |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 47 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 49 | import com.android.internal.net.NetworkStatsFactory; |
| 50 | import com.android.internal.util.JournaledFile; |
| 51 | import com.google.android.collect.Sets; |
| 52 | |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 53 | import java.io.BufferedReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import java.io.File; |
| 55 | import java.io.FileInputStream; |
| 56 | import java.io.FileOutputStream; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 57 | import java.io.FileReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import java.io.IOException; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 59 | import java.io.PrintWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import java.util.ArrayList; |
| 61 | import java.util.HashMap; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 62 | import java.util.HashSet; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 63 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 64 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import java.util.Map; |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 66 | import java.util.concurrent.atomic.AtomicInteger; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 67 | import java.util.concurrent.locks.ReentrantLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * All information we are collecting about things that can happen that impact |
| 71 | * battery life. All times are represented in microseconds except where indicated |
| 72 | * otherwise. |
| 73 | */ |
| 74 | public final class BatteryStatsImpl extends BatteryStats { |
| 75 | private static final String TAG = "BatteryStatsImpl"; |
| 76 | private static final boolean DEBUG = false; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 77 | private static final boolean DEBUG_HISTORY = false; |
Dianne Hackborn | e8c88e6 | 2011-08-17 19:09:09 -0700 | [diff] [blame] | 78 | private static final boolean USE_OLD_HISTORY = false; // for debugging. |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 79 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 80 | // TODO: remove "tcp" from network methods, since we measure total stats. |
| 81 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | // In-memory Parcel magic number, used to detect attempts to unmarshall bad data |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 83 | private static final int MAGIC = 0xBA757475; // 'BATSTATS' |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | // Current on-disk Parcel version |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 86 | private static final int VERSION = 64 + (USE_OLD_HISTORY ? 1000 : 0); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 87 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 88 | // Maximum number of items we will record in the history. |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 89 | private static final int MAX_HISTORY_ITEMS = 2000; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 90 | |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 91 | // No, really, THIS is the maximum number of items we will record in the history. |
| 92 | private static final int MAX_MAX_HISTORY_ITEMS = 3000; |
| 93 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 94 | // The maximum number of names wakelocks we will keep track of |
| 95 | // per uid; once the limit is reached, we batch the remaining wakelocks |
| 96 | // in to one common name. |
Dianne Hackborn | af17baa | 2013-05-09 15:27:47 -0700 | [diff] [blame] | 97 | private static final int MAX_WAKELOCKS_PER_UID = 50; |
Dianne Hackborn | c24ab86 | 2011-10-18 15:55:03 -0700 | [diff] [blame] | 98 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 99 | private static final String BATCHED_WAKELOCK_NAME = "*overflow*"; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 100 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 101 | private static int sNumSpeedSteps; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 103 | private final JournaledFile mFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 105 | static final int MSG_UPDATE_WAKELOCKS = 1; |
| 106 | static final int MSG_REPORT_POWER_CHANGE = 2; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 107 | static final long DELAY_UPDATE_WAKELOCKS = 5*1000; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 108 | |
| 109 | public interface BatteryCallback { |
| 110 | public void batteryNeedsCpuUpdate(); |
| 111 | public void batteryPowerChanged(boolean onBattery); |
| 112 | } |
| 113 | |
| 114 | final class MyHandler extends Handler { |
| 115 | @Override |
| 116 | public void handleMessage(Message msg) { |
| 117 | BatteryCallback cb = mCallback; |
| 118 | switch (msg.what) { |
| 119 | case MSG_UPDATE_WAKELOCKS: |
| 120 | if (cb != null) { |
| 121 | cb.batteryNeedsCpuUpdate(); |
| 122 | } |
| 123 | break; |
| 124 | case MSG_REPORT_POWER_CHANGE: |
| 125 | if (cb != null) { |
| 126 | cb.batteryPowerChanged(msg.arg1 != 0); |
| 127 | } |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private final MyHandler mHandler; |
| 134 | |
| 135 | private BatteryCallback mCallback; |
| 136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | /** |
| 138 | * The statistics we have collected organized by uids. |
| 139 | */ |
| 140 | final SparseArray<BatteryStatsImpl.Uid> mUidStats = |
| 141 | new SparseArray<BatteryStatsImpl.Uid>(); |
| 142 | |
| 143 | // A set of pools of currently active timers. When a timer is queried, we will divide the |
| 144 | // elapsed time by the number of active timers to arrive at that timer's share of the time. |
| 145 | // In order to do this, we must refresh each timer whenever the number of active timers |
| 146 | // changes. |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 147 | final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<StopwatchTimer>(); |
| 148 | final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<StopwatchTimer>(); |
| 149 | final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<StopwatchTimer>(); |
| 150 | final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers |
| 151 | = new SparseArray<ArrayList<StopwatchTimer>>(); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 152 | final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<StopwatchTimer>(); |
| 153 | final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<StopwatchTimer>(); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 154 | final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<StopwatchTimer>(); |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 155 | final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<StopwatchTimer>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 157 | // Last partial timers we use for distributing CPU usage. |
| 158 | final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<StopwatchTimer>(); |
| 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | // These are the objects that will want to do something when the device |
| 161 | // is unplugged from power. |
| 162 | final ArrayList<Unpluggable> mUnpluggables = new ArrayList<Unpluggable>(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 163 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 164 | boolean mShuttingDown; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 165 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 166 | long mHistoryBaseTime; |
| 167 | boolean mHaveBatteryLevel = false; |
| 168 | boolean mRecordingHistory = true; |
| 169 | int mNumHistoryItems; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 170 | |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 171 | static final int MAX_HISTORY_BUFFER = 128*1024; // 128KB |
| 172 | static final int MAX_MAX_HISTORY_BUFFER = 144*1024; // 144KB |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 173 | final Parcel mHistoryBuffer = Parcel.obtain(); |
| 174 | final HistoryItem mHistoryLastWritten = new HistoryItem(); |
| 175 | final HistoryItem mHistoryLastLastWritten = new HistoryItem(); |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 176 | final HistoryItem mHistoryReadTmp = new HistoryItem(); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 177 | int mHistoryBufferLastPos = -1; |
| 178 | boolean mHistoryOverflow = false; |
| 179 | long mLastHistoryTime = 0; |
| 180 | |
| 181 | final HistoryItem mHistoryCur = new HistoryItem(); |
| 182 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 183 | HistoryItem mHistory; |
| 184 | HistoryItem mHistoryEnd; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 185 | HistoryItem mHistoryLastEnd; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 186 | HistoryItem mHistoryCache; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 187 | |
| 188 | private HistoryItem mHistoryIterator; |
| 189 | private boolean mReadOverflow; |
| 190 | private boolean mIteratingHistory; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | int mStartCount; |
| 193 | |
| 194 | long mBatteryUptime; |
| 195 | long mBatteryLastUptime; |
| 196 | long mBatteryRealtime; |
| 197 | long mBatteryLastRealtime; |
| 198 | |
| 199 | long mUptime; |
| 200 | long mUptimeStart; |
| 201 | long mLastUptime; |
| 202 | long mRealtime; |
| 203 | long mRealtimeStart; |
| 204 | long mLastRealtime; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | boolean mScreenOn; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 207 | StopwatchTimer mScreenOnTimer; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 208 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 209 | int mScreenBrightnessBin = -1; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 210 | final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS]; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 211 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 212 | Counter mInputEventCounter; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | boolean mPhoneOn; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 215 | StopwatchTimer mPhoneOnTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 216 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 217 | boolean mAudioOn; |
| 218 | StopwatchTimer mAudioOnTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 219 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 220 | boolean mVideoOn; |
| 221 | StopwatchTimer mVideoOnTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 222 | |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 223 | int mPhoneSignalStrengthBin = -1; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 224 | int mPhoneSignalStrengthBinRaw = -1; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 225 | final StopwatchTimer[] mPhoneSignalStrengthsTimer = |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 226 | new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS]; |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 227 | |
| 228 | StopwatchTimer mPhoneSignalScanningTimer; |
| 229 | |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 230 | int mPhoneDataConnectionType = -1; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 231 | final StopwatchTimer[] mPhoneDataConnectionsTimer = |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 232 | new StopwatchTimer[NUM_DATA_CONNECTION_TYPES]; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 233 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 234 | boolean mWifiOn; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 235 | StopwatchTimer mWifiOnTimer; |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 236 | int mWifiOnUid = -1; |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 237 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 238 | boolean mGlobalWifiRunning; |
| 239 | StopwatchTimer mGlobalWifiRunningTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 240 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 241 | boolean mBluetoothOn; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 242 | StopwatchTimer mBluetoothOnTimer; |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 243 | |
| 244 | /** Bluetooth headset object */ |
| 245 | BluetoothHeadset mBtHeadset; |
| 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | /** |
| 248 | * These provide time bases that discount the time the device is plugged |
| 249 | * in to power. |
| 250 | */ |
| 251 | boolean mOnBattery; |
| 252 | boolean mOnBatteryInternal; |
| 253 | long mTrackBatteryPastUptime; |
| 254 | long mTrackBatteryUptimeStart; |
| 255 | long mTrackBatteryPastRealtime; |
| 256 | long mTrackBatteryRealtimeStart; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | long mUnpluggedBatteryUptime; |
| 259 | long mUnpluggedBatteryRealtime; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 260 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 261 | /* |
| 262 | * These keep track of battery levels (1-100) at the last plug event and the last unplug event. |
| 263 | */ |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 264 | int mDischargeStartLevel; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 265 | int mDischargeUnplugLevel; |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 266 | int mDischargeCurrentLevel; |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 267 | int mLowDischargeAmountSinceCharge; |
| 268 | int mHighDischargeAmountSinceCharge; |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 269 | int mDischargeScreenOnUnplugLevel; |
| 270 | int mDischargeScreenOffUnplugLevel; |
| 271 | int mDischargeAmountScreenOn; |
| 272 | int mDischargeAmountScreenOnSinceCharge; |
| 273 | int mDischargeAmountScreenOff; |
| 274 | int mDischargeAmountScreenOffSinceCharge; |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | long mLastWriteTime = 0; // Milliseconds |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 277 | |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 278 | // Mobile data transferred while on battery |
| 279 | private long[] mMobileDataTx = new long[4]; |
| 280 | private long[] mMobileDataRx = new long[4]; |
| 281 | private long[] mTotalDataTx = new long[4]; |
| 282 | private long[] mTotalDataRx = new long[4]; |
| 283 | |
| 284 | private long mRadioDataUptime; |
| 285 | private long mRadioDataStart; |
| 286 | |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 287 | private int mBluetoothPingCount; |
| 288 | private int mBluetoothPingStart = -1; |
| 289 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 290 | private int mPhoneServiceState = -1; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 291 | private int mPhoneServiceStateRaw = -1; |
| 292 | private int mPhoneSimStateRaw = -1; |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 293 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 294 | /* |
| 295 | * Holds a SamplingTimer associated with each kernel wakelock name being tracked. |
| 296 | */ |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 297 | private final HashMap<String, SamplingTimer> mKernelWakelockStats = |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 298 | new HashMap<String, SamplingTimer>(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 299 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 300 | public Map<String, ? extends SamplingTimer> getKernelWakelockStats() { |
| 301 | return mKernelWakelockStats; |
| 302 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 303 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 304 | private static int sKernelWakelockUpdateVersion = 0; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 305 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 306 | private static final int[] PROC_WAKELOCKS_FORMAT = new int[] { |
| 307 | Process.PROC_TAB_TERM|Process.PROC_OUT_STRING, // 0: name |
| 308 | Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 1: count |
| 309 | Process.PROC_TAB_TERM, |
| 310 | Process.PROC_TAB_TERM, |
| 311 | Process.PROC_TAB_TERM, |
| 312 | Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 5: totalTime |
| 313 | }; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 314 | |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 315 | private static final int[] WAKEUP_SOURCES_FORMAT = new int[] { |
| 316 | Process.PROC_TAB_TERM|Process.PROC_OUT_STRING, // 0: name |
| 317 | Process.PROC_TAB_TERM|Process.PROC_COMBINE| |
| 318 | Process.PROC_OUT_LONG, // 1: count |
| 319 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 320 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 321 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 322 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 323 | Process.PROC_TAB_TERM|Process.PROC_COMBINE |
| 324 | |Process.PROC_OUT_LONG, // 6: totalTime |
| 325 | }; |
| 326 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 327 | private final String[] mProcWakelocksName = new String[3]; |
| 328 | private final long[] mProcWakelocksData = new long[3]; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 329 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 330 | /* |
| 331 | * Used as a buffer for reading in data from /proc/wakelocks before it is processed and added |
| 332 | * to mKernelWakelockStats. |
| 333 | */ |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 334 | private final Map<String, KernelWakelockStats> mProcWakelockFileStats = |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 335 | new HashMap<String, KernelWakelockStats>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 337 | private HashMap<String, Integer> mUidCache = new HashMap<String, Integer>(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 338 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 339 | private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory(); |
| 340 | |
| 341 | /** Network ifaces that {@link ConnectivityManager} has claimed as mobile. */ |
| 342 | private HashSet<String> mMobileIfaces = Sets.newHashSet(); |
| 343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | // For debugging |
| 345 | public BatteryStatsImpl() { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 346 | mFile = null; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 347 | mHandler = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | public static interface Unpluggable { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 351 | void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime); |
| 352 | void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 354 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | /** |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 356 | * State for keeping track of counting information. |
| 357 | */ |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 358 | public static class Counter extends BatteryStats.Counter implements Unpluggable { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 359 | final AtomicInteger mCount = new AtomicInteger(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 360 | final ArrayList<Unpluggable> mUnpluggables; |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 361 | int mLoadedCount; |
| 362 | int mLastCount; |
| 363 | int mUnpluggedCount; |
| 364 | int mPluggedCount; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 365 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 366 | Counter(ArrayList<Unpluggable> unpluggables, Parcel in) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 367 | mUnpluggables = unpluggables; |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 368 | mPluggedCount = in.readInt(); |
| 369 | mCount.set(mPluggedCount); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 370 | mLoadedCount = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 371 | mLastCount = 0; |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 372 | mUnpluggedCount = in.readInt(); |
| 373 | unpluggables.add(this); |
| 374 | } |
| 375 | |
| 376 | Counter(ArrayList<Unpluggable> unpluggables) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 377 | mUnpluggables = unpluggables; |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 378 | unpluggables.add(this); |
| 379 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 380 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 381 | public void writeToParcel(Parcel out) { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 382 | out.writeInt(mCount.get()); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 383 | out.writeInt(mLoadedCount); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 384 | out.writeInt(mUnpluggedCount); |
| 385 | } |
| 386 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 387 | public void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 388 | mUnpluggedCount = mPluggedCount; |
| 389 | mCount.set(mPluggedCount); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 392 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 393 | mPluggedCount = mCount.get(); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 394 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 395 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 396 | /** |
| 397 | * Writes a possibly null Counter to a Parcel. |
| 398 | * |
| 399 | * @param out the Parcel to be written to. |
| 400 | * @param counter a Counter, or null. |
| 401 | */ |
| 402 | public static void writeCounterToParcel(Parcel out, Counter counter) { |
| 403 | if (counter == null) { |
| 404 | out.writeInt(0); // indicates null |
| 405 | return; |
| 406 | } |
| 407 | out.writeInt(1); // indicates non-null |
| 408 | |
| 409 | counter.writeToParcel(out); |
| 410 | } |
| 411 | |
| 412 | @Override |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 413 | public int getCountLocked(int which) { |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 414 | int val; |
| 415 | if (which == STATS_LAST) { |
| 416 | val = mLastCount; |
| 417 | } else { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 418 | val = mCount.get(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 419 | if (which == STATS_SINCE_UNPLUGGED) { |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 420 | val -= mUnpluggedCount; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 421 | } else if (which != STATS_SINCE_CHARGED) { |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 422 | val -= mLoadedCount; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return val; |
| 427 | } |
| 428 | |
| 429 | public void logState(Printer pw, String prefix) { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 430 | pw.println(prefix + "mCount=" + mCount.get() |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 431 | + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount |
| 432 | + " mUnpluggedCount=" + mUnpluggedCount |
| 433 | + " mPluggedCount=" + mPluggedCount); |
| 434 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 435 | |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 436 | void stepAtomic() { |
| 437 | mCount.incrementAndGet(); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 440 | /** |
| 441 | * Clear state of this counter. |
| 442 | */ |
| 443 | void reset(boolean detachIfReset) { |
| 444 | mCount.set(0); |
| 445 | mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0; |
| 446 | if (detachIfReset) { |
| 447 | detach(); |
| 448 | } |
| 449 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 450 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 451 | void detach() { |
| 452 | mUnpluggables.remove(this); |
| 453 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 454 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 455 | void writeSummaryFromParcelLocked(Parcel out) { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 456 | int count = mCount.get(); |
| 457 | out.writeInt(count); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void readSummaryFromParcelLocked(Parcel in) { |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 461 | mLoadedCount = in.readInt(); |
| 462 | mCount.set(mLoadedCount); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 463 | mLastCount = 0; |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 464 | mUnpluggedCount = mPluggedCount = mLoadedCount; |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 467 | |
| 468 | public static class SamplingCounter extends Counter { |
| 469 | |
| 470 | SamplingCounter(ArrayList<Unpluggable> unpluggables, Parcel in) { |
| 471 | super(unpluggables, in); |
| 472 | } |
| 473 | |
| 474 | SamplingCounter(ArrayList<Unpluggable> unpluggables) { |
| 475 | super(unpluggables); |
| 476 | } |
| 477 | |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 478 | public void addCountAtomic(long count) { |
| 479 | mCount.addAndGet((int)count); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 483 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | * State for keeping track of timing information. |
| 485 | */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 486 | public static abstract class Timer extends BatteryStats.Timer implements Unpluggable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | final int mType; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 488 | final ArrayList<Unpluggable> mUnpluggables; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 489 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | int mCount; |
| 491 | int mLoadedCount; |
| 492 | int mLastCount; |
| 493 | int mUnpluggedCount; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 494 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | // Times are in microseconds for better accuracy when dividing by the |
| 496 | // lock count, and are in "battery realtime" units. |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | /** |
| 499 | * The total time we have accumulated since the start of the original |
| 500 | * boot, to the last time something interesting happened in the |
| 501 | * current run. |
| 502 | */ |
| 503 | long mTotalTime; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 504 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | /** |
| 506 | * The total time we loaded for the previous runs. Subtract this from |
| 507 | * mTotalTime to find the time for the current run of the system. |
| 508 | */ |
| 509 | long mLoadedTime; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 510 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | /** |
| 512 | * The run time of the last run of the system, as loaded from the |
| 513 | * saved data. |
| 514 | */ |
| 515 | long mLastTime; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | /** |
| 518 | * The value of mTotalTime when unplug() was last called. Subtract |
| 519 | * this from mTotalTime to find the time since the last unplug from |
| 520 | * power. |
| 521 | */ |
| 522 | long mUnpluggedTime; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 523 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 524 | /** |
| 525 | * Constructs from a parcel. |
| 526 | * @param type |
| 527 | * @param unpluggables |
| 528 | * @param powerType |
| 529 | * @param in |
| 530 | */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 531 | Timer(int type, ArrayList<Unpluggable> unpluggables, Parcel in) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | mType = type; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 533 | mUnpluggables = unpluggables; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | mCount = in.readInt(); |
| 536 | mLoadedCount = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 537 | mLastCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | mUnpluggedCount = in.readInt(); |
| 539 | mTotalTime = in.readLong(); |
| 540 | mLoadedTime = in.readLong(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 541 | mLastTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | mUnpluggedTime = in.readLong(); |
| 543 | unpluggables.add(this); |
| 544 | } |
| 545 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 546 | Timer(int type, ArrayList<Unpluggable> unpluggables) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | mType = type; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 548 | mUnpluggables = unpluggables; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | unpluggables.add(this); |
| 550 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 551 | |
| 552 | protected abstract long computeRunTimeLocked(long curBatteryRealtime); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 553 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 554 | protected abstract int computeCurrentCountLocked(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 555 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 556 | /** |
| 557 | * Clear state of this timer. Returns true if the timer is inactive |
| 558 | * so can be completely dropped. |
| 559 | */ |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 560 | boolean reset(BatteryStatsImpl stats, boolean detachIfReset) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 561 | mTotalTime = mLoadedTime = mLastTime = 0; |
| 562 | mCount = mLoadedCount = mLastCount = 0; |
| 563 | if (detachIfReset) { |
| 564 | detach(); |
| 565 | } |
| 566 | return true; |
| 567 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 568 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 569 | void detach() { |
| 570 | mUnpluggables.remove(this); |
| 571 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 572 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | public void writeToParcel(Parcel out, long batteryRealtime) { |
| 574 | out.writeInt(mCount); |
| 575 | out.writeInt(mLoadedCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | out.writeInt(mUnpluggedCount); |
| 577 | out.writeLong(computeRunTimeLocked(batteryRealtime)); |
| 578 | out.writeLong(mLoadedTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | out.writeLong(mUnpluggedTime); |
| 580 | } |
| 581 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 582 | public void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | if (DEBUG && mType < 0) { |
| 584 | Log.v(TAG, "unplug #" + mType + ": realtime=" + batteryRealtime |
| 585 | + " old mUnpluggedTime=" + mUnpluggedTime |
| 586 | + " old mUnpluggedCount=" + mUnpluggedCount); |
| 587 | } |
| 588 | mUnpluggedTime = computeRunTimeLocked(batteryRealtime); |
| 589 | mUnpluggedCount = mCount; |
| 590 | if (DEBUG && mType < 0) { |
| 591 | Log.v(TAG, "unplug #" + mType |
| 592 | + ": new mUnpluggedTime=" + mUnpluggedTime |
| 593 | + " new mUnpluggedCount=" + mUnpluggedCount); |
| 594 | } |
| 595 | } |
| 596 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 597 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 598 | if (DEBUG && mType < 0) { |
| 599 | Log.v(TAG, "plug #" + mType + ": realtime=" + batteryRealtime |
| 600 | + " old mTotalTime=" + mTotalTime); |
| 601 | } |
| 602 | mTotalTime = computeRunTimeLocked(batteryRealtime); |
| 603 | mCount = computeCurrentCountLocked(); |
| 604 | if (DEBUG && mType < 0) { |
| 605 | Log.v(TAG, "plug #" + mType |
| 606 | + ": new mTotalTime=" + mTotalTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | } |
| 608 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 609 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | /** |
| 611 | * Writes a possibly null Timer to a Parcel. |
| 612 | * |
| 613 | * @param out the Parcel to be written to. |
| 614 | * @param timer a Timer, or null. |
| 615 | */ |
| 616 | public static void writeTimerToParcel(Parcel out, Timer timer, |
| 617 | long batteryRealtime) { |
| 618 | if (timer == null) { |
| 619 | out.writeInt(0); // indicates null |
| 620 | return; |
| 621 | } |
| 622 | out.writeInt(1); // indicates non-null |
| 623 | |
| 624 | timer.writeToParcel(out, batteryRealtime); |
| 625 | } |
| 626 | |
| 627 | @Override |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 628 | public long getTotalTimeLocked(long batteryRealtime, int which) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | long val; |
| 630 | if (which == STATS_LAST) { |
| 631 | val = mLastTime; |
| 632 | } else { |
| 633 | val = computeRunTimeLocked(batteryRealtime); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 634 | if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | val -= mUnpluggedTime; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 636 | } else if (which != STATS_SINCE_CHARGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | val -= mLoadedTime; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return val; |
| 642 | } |
| 643 | |
| 644 | @Override |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 645 | public int getCountLocked(int which) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | int val; |
| 647 | if (which == STATS_LAST) { |
| 648 | val = mLastCount; |
| 649 | } else { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 650 | val = computeCurrentCountLocked(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 651 | if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | val -= mUnpluggedCount; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 653 | } else if (which != STATS_SINCE_CHARGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | val -= mLoadedCount; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return val; |
| 659 | } |
| 660 | |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 661 | public void logState(Printer pw, String prefix) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 662 | pw.println(prefix + " mCount=" + mCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount |
| 664 | + " mUnpluggedCount=" + mUnpluggedCount); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 665 | pw.println(prefix + "mTotalTime=" + mTotalTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | + " mLoadedTime=" + mLoadedTime); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 667 | pw.println(prefix + "mLastTime=" + mLastTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | + " mUnpluggedTime=" + mUnpluggedTime); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 669 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 670 | |
| 671 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 672 | void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) { |
| 673 | long runTime = computeRunTimeLocked(batteryRealtime); |
| 674 | // Divide by 1000 for backwards compatibility |
| 675 | out.writeLong((runTime + 500) / 1000); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 676 | out.writeInt(mCount); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void readSummaryFromParcelLocked(Parcel in) { |
| 680 | // Multiply by 1000 for backwards compatibility |
| 681 | mTotalTime = mLoadedTime = in.readLong() * 1000; |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 682 | mLastTime = 0; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 683 | mUnpluggedTime = mTotalTime; |
| 684 | mCount = mLoadedCount = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 685 | mLastCount = 0; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 686 | mUnpluggedCount = mCount; |
| 687 | } |
| 688 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 689 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 690 | public static final class SamplingTimer extends Timer { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 691 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 692 | /** |
| 693 | * The most recent reported count from /proc/wakelocks. |
| 694 | */ |
| 695 | int mCurrentReportedCount; |
| 696 | |
| 697 | /** |
| 698 | * The reported count from /proc/wakelocks when unplug() was last |
| 699 | * called. |
| 700 | */ |
| 701 | int mUnpluggedReportedCount; |
| 702 | |
| 703 | /** |
| 704 | * The most recent reported total_time from /proc/wakelocks. |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 705 | */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 706 | long mCurrentReportedTotalTime; |
| 707 | |
| 708 | |
| 709 | /** |
| 710 | * The reported total_time from /proc/wakelocks when unplug() was last |
| 711 | * called. |
| 712 | */ |
| 713 | long mUnpluggedReportedTotalTime; |
| 714 | |
| 715 | /** |
| 716 | * Whether we are currently in a discharge cycle. |
| 717 | */ |
| 718 | boolean mInDischarge; |
| 719 | |
| 720 | /** |
| 721 | * Whether we are currently recording reported values. |
| 722 | */ |
| 723 | boolean mTrackingReportedValues; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 724 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 725 | /* |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 726 | * A sequence counter, incremented once for each update of the stats. |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 727 | */ |
| 728 | int mUpdateVersion; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 729 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 730 | SamplingTimer(ArrayList<Unpluggable> unpluggables, boolean inDischarge, Parcel in) { |
| 731 | super(0, unpluggables, in); |
| 732 | mCurrentReportedCount = in.readInt(); |
| 733 | mUnpluggedReportedCount = in.readInt(); |
| 734 | mCurrentReportedTotalTime = in.readLong(); |
| 735 | mUnpluggedReportedTotalTime = in.readLong(); |
| 736 | mTrackingReportedValues = in.readInt() == 1; |
| 737 | mInDischarge = inDischarge; |
| 738 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 739 | |
| 740 | SamplingTimer(ArrayList<Unpluggable> unpluggables, boolean inDischarge, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 741 | boolean trackReportedValues) { |
| 742 | super(0, unpluggables); |
| 743 | mTrackingReportedValues = trackReportedValues; |
| 744 | mInDischarge = inDischarge; |
| 745 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 746 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 747 | public void setStale() { |
| 748 | mTrackingReportedValues = false; |
| 749 | mUnpluggedReportedTotalTime = 0; |
| 750 | mUnpluggedReportedCount = 0; |
| 751 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 752 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 753 | public void setUpdateVersion(int version) { |
| 754 | mUpdateVersion = version; |
| 755 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 756 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 757 | public int getUpdateVersion() { |
| 758 | return mUpdateVersion; |
| 759 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 760 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 761 | public void updateCurrentReportedCount(int count) { |
| 762 | if (mInDischarge && mUnpluggedReportedCount == 0) { |
| 763 | // Updating the reported value for the first time. |
| 764 | mUnpluggedReportedCount = count; |
| 765 | // If we are receiving an update update mTrackingReportedValues; |
| 766 | mTrackingReportedValues = true; |
| 767 | } |
| 768 | mCurrentReportedCount = count; |
| 769 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 770 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 771 | public void updateCurrentReportedTotalTime(long totalTime) { |
| 772 | if (mInDischarge && mUnpluggedReportedTotalTime == 0) { |
| 773 | // Updating the reported value for the first time. |
| 774 | mUnpluggedReportedTotalTime = totalTime; |
| 775 | // If we are receiving an update update mTrackingReportedValues; |
| 776 | mTrackingReportedValues = true; |
| 777 | } |
| 778 | mCurrentReportedTotalTime = totalTime; |
| 779 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 780 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 781 | public void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
| 782 | super.unplug(elapsedRealtime, batteryUptime, batteryRealtime); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 783 | if (mTrackingReportedValues) { |
| 784 | mUnpluggedReportedTotalTime = mCurrentReportedTotalTime; |
| 785 | mUnpluggedReportedCount = mCurrentReportedCount; |
| 786 | } |
| 787 | mInDischarge = true; |
| 788 | } |
| 789 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 790 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
| 791 | super.plug(elapsedRealtime, batteryUptime, batteryRealtime); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 792 | mInDischarge = false; |
| 793 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 794 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 795 | public void logState(Printer pw, String prefix) { |
| 796 | super.logState(pw, prefix); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 797 | pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 798 | + " mUnpluggedReportedCount=" + mUnpluggedReportedCount |
| 799 | + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime |
| 800 | + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime); |
| 801 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 802 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 803 | protected long computeRunTimeLocked(long curBatteryRealtime) { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 804 | return mTotalTime + (mInDischarge && mTrackingReportedValues |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 805 | ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0); |
| 806 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 807 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 808 | protected int computeCurrentCountLocked() { |
| 809 | return mCount + (mInDischarge && mTrackingReportedValues |
| 810 | ? mCurrentReportedCount - mUnpluggedReportedCount : 0); |
| 811 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 812 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 813 | public void writeToParcel(Parcel out, long batteryRealtime) { |
| 814 | super.writeToParcel(out, batteryRealtime); |
| 815 | out.writeInt(mCurrentReportedCount); |
| 816 | out.writeInt(mUnpluggedReportedCount); |
| 817 | out.writeLong(mCurrentReportedTotalTime); |
| 818 | out.writeLong(mUnpluggedReportedTotalTime); |
| 819 | out.writeInt(mTrackingReportedValues ? 1 : 0); |
| 820 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 821 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 822 | boolean reset(BatteryStatsImpl stats, boolean detachIfReset) { |
| 823 | super.reset(stats, detachIfReset); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 824 | setStale(); |
| 825 | return true; |
| 826 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 827 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 828 | void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) { |
| 829 | super.writeSummaryFromParcelLocked(out, batteryRealtime); |
| 830 | out.writeLong(mCurrentReportedTotalTime); |
| 831 | out.writeInt(mCurrentReportedCount); |
| 832 | out.writeInt(mTrackingReportedValues ? 1 : 0); |
| 833 | } |
| 834 | |
| 835 | void readSummaryFromParcelLocked(Parcel in) { |
| 836 | super.readSummaryFromParcelLocked(in); |
| 837 | mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong(); |
| 838 | mUnpluggedReportedCount = mCurrentReportedCount = in.readInt(); |
| 839 | mTrackingReportedValues = in.readInt() == 1; |
| 840 | } |
| 841 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 842 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 843 | /** |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 844 | * A timer that increments in batches. It does not run for durations, but just jumps |
| 845 | * for a pre-determined amount. |
| 846 | */ |
| 847 | public static final class BatchTimer extends Timer { |
| 848 | final Uid mUid; |
| 849 | |
| 850 | /** |
| 851 | * The last time at which we updated the timer. This is in elapsed realtime microseconds. |
| 852 | */ |
| 853 | long mLastAddedTime; |
| 854 | |
| 855 | /** |
| 856 | * The last duration that we added to the timer. This is in microseconds. |
| 857 | */ |
| 858 | long mLastAddedDuration; |
| 859 | |
| 860 | /** |
| 861 | * Whether we are currently in a discharge cycle. |
| 862 | */ |
| 863 | boolean mInDischarge; |
| 864 | |
| 865 | BatchTimer(Uid uid, int type, ArrayList<Unpluggable> unpluggables, |
| 866 | boolean inDischarge, Parcel in) { |
| 867 | super(type, unpluggables, in); |
| 868 | mUid = uid; |
| 869 | mLastAddedTime = in.readLong(); |
| 870 | mLastAddedDuration = in.readLong(); |
| 871 | mInDischarge = inDischarge; |
| 872 | } |
| 873 | |
| 874 | BatchTimer(Uid uid, int type, ArrayList<Unpluggable> unpluggables, |
| 875 | boolean inDischarge) { |
| 876 | super(type, unpluggables); |
| 877 | mUid = uid; |
| 878 | mInDischarge = inDischarge; |
| 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public void writeToParcel(Parcel out, long batteryRealtime) { |
| 883 | super.writeToParcel(out, batteryRealtime); |
| 884 | out.writeLong(mLastAddedTime); |
| 885 | out.writeLong(mLastAddedDuration); |
| 886 | } |
| 887 | |
| 888 | @Override |
| 889 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
| 890 | recomputeLastDuration(SystemClock.elapsedRealtime() * 1000, false); |
| 891 | mInDischarge = false; |
| 892 | super.plug(elapsedRealtime, batteryUptime, batteryRealtime); |
| 893 | } |
| 894 | |
| 895 | @Override |
| 896 | public void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
| 897 | recomputeLastDuration(elapsedRealtime, false); |
| 898 | mInDischarge = true; |
| 899 | // If we are still within the last added duration, then re-added whatever remains. |
| 900 | if (mLastAddedTime == elapsedRealtime) { |
| 901 | mTotalTime += mLastAddedDuration; |
| 902 | } |
| 903 | super.unplug(elapsedRealtime, batteryUptime, batteryRealtime); |
| 904 | } |
| 905 | |
| 906 | @Override |
| 907 | public void logState(Printer pw, String prefix) { |
| 908 | super.logState(pw, prefix); |
| 909 | pw.println(prefix + "mLastAddedTime=" + mLastAddedTime |
| 910 | + " mLastAddedDuration=" + mLastAddedDuration); |
| 911 | } |
| 912 | |
| 913 | private long computeOverage(long curTime) { |
| 914 | if (mLastAddedTime > 0) { |
| 915 | return mLastTime + mLastAddedDuration - curTime; |
| 916 | } |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | private void recomputeLastDuration(long curTime, boolean abort) { |
| 921 | final long overage = computeOverage(curTime); |
| 922 | if (overage > 0) { |
| 923 | // Aborting before the duration ran out -- roll back the remaining |
| 924 | // duration. Only do this if currently discharging; otherwise we didn't |
| 925 | // actually add the time. |
| 926 | if (mInDischarge) { |
| 927 | mTotalTime -= overage; |
| 928 | } |
| 929 | if (abort) { |
| 930 | mLastAddedTime = 0; |
| 931 | } else { |
| 932 | mLastAddedTime = curTime; |
| 933 | mLastAddedDuration -= overage; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | public void addDuration(BatteryStatsImpl stats, long durationMillis) { |
| 939 | final long now = SystemClock.elapsedRealtime() * 1000; |
| 940 | recomputeLastDuration(now, true); |
| 941 | mLastAddedTime = now; |
| 942 | mLastAddedDuration = durationMillis * 1000; |
| 943 | if (mInDischarge) { |
| 944 | mTotalTime += mLastAddedDuration; |
| 945 | mCount++; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | public void abortLastDuration(BatteryStatsImpl stats) { |
| 950 | final long now = SystemClock.elapsedRealtime() * 1000; |
| 951 | recomputeLastDuration(now, true); |
| 952 | } |
| 953 | |
| 954 | @Override |
| 955 | protected int computeCurrentCountLocked() { |
| 956 | return mCount; |
| 957 | } |
| 958 | |
| 959 | @Override |
| 960 | protected long computeRunTimeLocked(long curBatteryRealtime) { |
| 961 | final long overage = computeOverage(SystemClock.elapsedRealtime() * 1000); |
| 962 | if (overage > 0) { |
| 963 | return mTotalTime = overage; |
| 964 | } |
| 965 | return mTotalTime; |
| 966 | } |
| 967 | |
| 968 | @Override |
| 969 | boolean reset(BatteryStatsImpl stats, boolean detachIfReset) { |
| 970 | final long now = SystemClock.elapsedRealtime() * 1000; |
| 971 | recomputeLastDuration(now, true); |
| 972 | boolean stillActive = mLastAddedTime == now; |
| 973 | super.reset(stats, !stillActive && detachIfReset); |
| 974 | return !stillActive; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | /** |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 979 | * State for keeping track of timing information. |
| 980 | */ |
| 981 | public static final class StopwatchTimer extends Timer { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 982 | final Uid mUid; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 983 | final ArrayList<StopwatchTimer> mTimerPool; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 984 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 985 | int mNesting; |
| 986 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 987 | /** |
| 988 | * The last time at which we updated the timer. If mNesting is > 0, |
| 989 | * subtract this from the current battery time to find the amount of |
| 990 | * time we have been running since we last computed an update. |
| 991 | */ |
| 992 | long mUpdateTime; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 993 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 994 | /** |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 995 | * The total time at which the timer was acquired, to determine if it |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 996 | * was actually held for an interesting duration. |
| 997 | */ |
| 998 | long mAcquireTime; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 999 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 1000 | long mTimeout; |
| 1001 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1002 | /** |
| 1003 | * For partial wake locks, keep track of whether we are in the list |
| 1004 | * to consume CPU cycles. |
| 1005 | */ |
| 1006 | boolean mInList; |
| 1007 | |
| 1008 | StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1009 | ArrayList<Unpluggable> unpluggables, Parcel in) { |
| 1010 | super(type, unpluggables, in); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1011 | mUid = uid; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1012 | mTimerPool = timerPool; |
| 1013 | mUpdateTime = in.readLong(); |
| 1014 | } |
| 1015 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1016 | StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1017 | ArrayList<Unpluggable> unpluggables) { |
| 1018 | super(type, unpluggables); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1019 | mUid = uid; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1020 | mTimerPool = timerPool; |
| 1021 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1022 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 1023 | void setTimeout(long timeout) { |
| 1024 | mTimeout = timeout; |
| 1025 | } |
| 1026 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1027 | public void writeToParcel(Parcel out, long batteryRealtime) { |
| 1028 | super.writeToParcel(out, batteryRealtime); |
| 1029 | out.writeLong(mUpdateTime); |
| 1030 | } |
| 1031 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1032 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1033 | if (mNesting > 0) { |
| 1034 | if (DEBUG && mType < 0) { |
| 1035 | Log.v(TAG, "old mUpdateTime=" + mUpdateTime); |
| 1036 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1037 | super.plug(elapsedRealtime, batteryUptime, batteryRealtime); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1038 | mUpdateTime = batteryRealtime; |
| 1039 | if (DEBUG && mType < 0) { |
| 1040 | Log.v(TAG, "new mUpdateTime=" + mUpdateTime); |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | public void logState(Printer pw, String prefix) { |
| 1046 | super.logState(pw, prefix); |
| 1047 | pw.println(prefix + "mNesting=" + mNesting + "mUpdateTime=" + mUpdateTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | + " mAcquireTime=" + mAcquireTime); |
| 1049 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1050 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | void startRunningLocked(BatteryStatsImpl stats) { |
| 1052 | if (mNesting++ == 0) { |
| 1053 | mUpdateTime = stats.getBatteryRealtimeLocked( |
| 1054 | SystemClock.elapsedRealtime() * 1000); |
| 1055 | if (mTimerPool != null) { |
| 1056 | // Accumulate time to all currently active timers before adding |
| 1057 | // this new one to the pool. |
| 1058 | refreshTimersLocked(stats, mTimerPool); |
| 1059 | // Add this timer to the active pool |
| 1060 | mTimerPool.add(this); |
| 1061 | } |
| 1062 | // Increment the count |
| 1063 | mCount++; |
| 1064 | mAcquireTime = mTotalTime; |
| 1065 | if (DEBUG && mType < 0) { |
| 1066 | Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime |
| 1067 | + " mTotalTime=" + mTotalTime + " mCount=" + mCount |
| 1068 | + " mAcquireTime=" + mAcquireTime); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1073 | boolean isRunningLocked() { |
| 1074 | return mNesting > 0; |
| 1075 | } |
| 1076 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | void stopRunningLocked(BatteryStatsImpl stats) { |
| 1078 | // Ignore attempt to stop a timer that isn't running |
| 1079 | if (mNesting == 0) { |
| 1080 | return; |
| 1081 | } |
| 1082 | if (--mNesting == 0) { |
| 1083 | if (mTimerPool != null) { |
| 1084 | // Accumulate time to all active counters, scaled by the total |
| 1085 | // active in the pool, before taking this one out of the pool. |
| 1086 | refreshTimersLocked(stats, mTimerPool); |
| 1087 | // Remove this timer from the active pool |
| 1088 | mTimerPool.remove(this); |
| 1089 | } else { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1090 | final long realtime = SystemClock.elapsedRealtime() * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | final long batteryRealtime = stats.getBatteryRealtimeLocked(realtime); |
| 1092 | mNesting = 1; |
| 1093 | mTotalTime = computeRunTimeLocked(batteryRealtime); |
| 1094 | mNesting = 0; |
| 1095 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1096 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | if (DEBUG && mType < 0) { |
| 1098 | Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime |
| 1099 | + " mTotalTime=" + mTotalTime + " mCount=" + mCount |
| 1100 | + " mAcquireTime=" + mAcquireTime); |
| 1101 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | if (mTotalTime == mAcquireTime) { |
| 1104 | // If there was no change in the time, then discard this |
| 1105 | // count. A somewhat cheezy strategy, but hey. |
| 1106 | mCount--; |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | // Update the total time for all other running Timers with the same type as this Timer |
| 1112 | // due to a change in timer count |
| 1113 | private static void refreshTimersLocked(final BatteryStatsImpl stats, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1114 | final ArrayList<StopwatchTimer> pool) { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1115 | final long realtime = SystemClock.elapsedRealtime() * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | final long batteryRealtime = stats.getBatteryRealtimeLocked(realtime); |
| 1117 | final int N = pool.size(); |
| 1118 | for (int i=N-1; i>= 0; i--) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1119 | final StopwatchTimer t = pool.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | long heldTime = batteryRealtime - t.mUpdateTime; |
| 1121 | if (heldTime > 0) { |
| 1122 | t.mTotalTime += heldTime / N; |
| 1123 | } |
| 1124 | t.mUpdateTime = batteryRealtime; |
| 1125 | } |
| 1126 | } |
| 1127 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1128 | @Override |
| 1129 | protected long computeRunTimeLocked(long curBatteryRealtime) { |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 1130 | if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) { |
| 1131 | curBatteryRealtime = mUpdateTime + mTimeout; |
| 1132 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | return mTotalTime + (mNesting > 0 |
| 1134 | ? (curBatteryRealtime - mUpdateTime) |
| 1135 | / (mTimerPool != null ? mTimerPool.size() : 1) |
| 1136 | : 0); |
| 1137 | } |
| 1138 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1139 | @Override |
| 1140 | protected int computeCurrentCountLocked() { |
| 1141 | return mCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1144 | boolean reset(BatteryStatsImpl stats, boolean detachIfReset) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1145 | boolean canDetach = mNesting <= 0; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1146 | super.reset(stats, canDetach && detachIfReset); |
| 1147 | if (mNesting > 0) { |
| 1148 | mUpdateTime = stats.getBatteryRealtimeLocked( |
| 1149 | SystemClock.elapsedRealtime() * 1000); |
| 1150 | } |
| 1151 | mAcquireTime = mTotalTime; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1152 | return canDetach; |
| 1153 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1154 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1155 | void detach() { |
| 1156 | super.detach(); |
| 1157 | if (mTimerPool != null) { |
| 1158 | mTimerPool.remove(this); |
| 1159 | } |
| 1160 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | void readSummaryFromParcelLocked(Parcel in) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1163 | super.readSummaryFromParcelLocked(in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | mNesting = 0; |
| 1165 | } |
| 1166 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1167 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1168 | private final Map<String, KernelWakelockStats> readKernelWakelockStats() { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1169 | |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1170 | FileInputStream is; |
Johannes Carlsson | 3372f2e | 2010-06-30 08:45:55 +0200 | [diff] [blame] | 1171 | byte[] buffer = new byte[8192]; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1172 | int len; |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1173 | boolean wakeup_sources = false; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1174 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1175 | try { |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1176 | try { |
| 1177 | is = new FileInputStream("/proc/wakelocks"); |
| 1178 | } catch (java.io.FileNotFoundException e) { |
| 1179 | try { |
| 1180 | is = new FileInputStream("/d/wakeup_sources"); |
| 1181 | wakeup_sources = true; |
| 1182 | } catch (java.io.FileNotFoundException e2) { |
| 1183 | return null; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1184 | } |
| 1185 | } |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1186 | |
| 1187 | len = is.read(buffer); |
| 1188 | is.close(); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1189 | } catch (java.io.IOException e) { |
| 1190 | return null; |
| 1191 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1192 | |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1193 | if (len > 0) { |
| 1194 | int i; |
| 1195 | for (i=0; i<len; i++) { |
| 1196 | if (buffer[i] == '\0') { |
| 1197 | len = i; |
| 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | return parseProcWakelocks(buffer, len, wakeup_sources); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1204 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1205 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1206 | private final Map<String, KernelWakelockStats> parseProcWakelocks( |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1207 | byte[] wlBuffer, int len, boolean wakeup_sources) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1208 | String name; |
| 1209 | int count; |
| 1210 | long totalTime; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1211 | int startIndex; |
| 1212 | int endIndex; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1213 | int numUpdatedWlNames = 0; |
| 1214 | |
| 1215 | // Advance past the first line. |
| 1216 | int i; |
| 1217 | for (i = 0; i < len && wlBuffer[i] != '\n' && wlBuffer[i] != '\0'; i++); |
| 1218 | startIndex = endIndex = i + 1; |
| 1219 | |
| 1220 | synchronized(this) { |
| 1221 | Map<String, KernelWakelockStats> m = mProcWakelockFileStats; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1222 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1223 | sKernelWakelockUpdateVersion++; |
| 1224 | while (endIndex < len) { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1225 | for (endIndex=startIndex; |
| 1226 | endIndex < len && wlBuffer[endIndex] != '\n' && wlBuffer[endIndex] != '\0'; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1227 | endIndex++); |
Johannes Carlsson | 3372f2e | 2010-06-30 08:45:55 +0200 | [diff] [blame] | 1228 | endIndex++; // endIndex is an exclusive upper bound. |
| 1229 | // Don't go over the end of the buffer, Process.parseProcLine might |
| 1230 | // write to wlBuffer[endIndex] |
| 1231 | if (endIndex >= (len - 1) ) { |
| 1232 | return m; |
Amith Yamasani | e579561 | 2010-04-05 12:43:44 -0700 | [diff] [blame] | 1233 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1234 | |
| 1235 | String[] nameStringArray = mProcWakelocksName; |
| 1236 | long[] wlData = mProcWakelocksData; |
Amith Yamasani | 2098ead | 2010-04-02 13:46:49 -0700 | [diff] [blame] | 1237 | // Stomp out any bad characters since this is from a circular buffer |
| 1238 | // A corruption is seen sometimes that results in the vm crashing |
| 1239 | // This should prevent crashes and the line will probably fail to parse |
| 1240 | for (int j = startIndex; j < endIndex; j++) { |
| 1241 | if ((wlBuffer[j] & 0x80) != 0) wlBuffer[j] = (byte) '?'; |
| 1242 | } |
Amith Yamasani | 53b707b | 2009-09-30 11:05:30 -0700 | [diff] [blame] | 1243 | boolean parsed = Process.parseProcLine(wlBuffer, startIndex, endIndex, |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1244 | wakeup_sources ? WAKEUP_SOURCES_FORMAT : |
| 1245 | PROC_WAKELOCKS_FORMAT, |
| 1246 | nameStringArray, wlData, null); |
Amith Yamasani | 2098ead | 2010-04-02 13:46:49 -0700 | [diff] [blame] | 1247 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1248 | name = nameStringArray[0]; |
| 1249 | count = (int) wlData[1]; |
Todd Poynor | 73f534a | 2012-06-19 11:07:26 -0700 | [diff] [blame] | 1250 | |
| 1251 | if (wakeup_sources) { |
| 1252 | // convert milliseconds to microseconds |
| 1253 | totalTime = wlData[2] * 1000; |
| 1254 | } else { |
| 1255 | // convert nanoseconds to microseconds with rounding. |
| 1256 | totalTime = (wlData[2] + 500) / 1000; |
| 1257 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1258 | |
Amith Yamasani | 53b707b | 2009-09-30 11:05:30 -0700 | [diff] [blame] | 1259 | if (parsed && name.length() > 0) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1260 | if (!m.containsKey(name)) { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1261 | m.put(name, new KernelWakelockStats(count, totalTime, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1262 | sKernelWakelockUpdateVersion)); |
| 1263 | numUpdatedWlNames++; |
| 1264 | } else { |
| 1265 | KernelWakelockStats kwlStats = m.get(name); |
| 1266 | if (kwlStats.mVersion == sKernelWakelockUpdateVersion) { |
| 1267 | kwlStats.mCount += count; |
| 1268 | kwlStats.mTotalTime += totalTime; |
| 1269 | } else { |
| 1270 | kwlStats.mCount = count; |
| 1271 | kwlStats.mTotalTime = totalTime; |
| 1272 | kwlStats.mVersion = sKernelWakelockUpdateVersion; |
| 1273 | numUpdatedWlNames++; |
| 1274 | } |
| 1275 | } |
Amith Yamasani | 53b707b | 2009-09-30 11:05:30 -0700 | [diff] [blame] | 1276 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1277 | startIndex = endIndex; |
| 1278 | } |
| 1279 | |
| 1280 | if (m.size() != numUpdatedWlNames) { |
| 1281 | // Don't report old data. |
| 1282 | Iterator<KernelWakelockStats> itr = m.values().iterator(); |
| 1283 | while (itr.hasNext()) { |
| 1284 | if (itr.next().mVersion != sKernelWakelockUpdateVersion) { |
| 1285 | itr.remove(); |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | return m; |
| 1290 | } |
| 1291 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1292 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1293 | private class KernelWakelockStats { |
| 1294 | public int mCount; |
| 1295 | public long mTotalTime; |
| 1296 | public int mVersion; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1297 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1298 | KernelWakelockStats(int count, long totalTime, int version) { |
| 1299 | mCount = count; |
| 1300 | mTotalTime = totalTime; |
| 1301 | mVersion = version; |
| 1302 | } |
| 1303 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1304 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1305 | /* |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1306 | * Get the KernelWakelockTimer associated with name, and create a new one if one |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1307 | * doesn't already exist. |
| 1308 | */ |
| 1309 | public SamplingTimer getKernelWakelockTimerLocked(String name) { |
| 1310 | SamplingTimer kwlt = mKernelWakelockStats.get(name); |
| 1311 | if (kwlt == null) { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1312 | kwlt = new SamplingTimer(mUnpluggables, mOnBatteryInternal, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1313 | true /* track reported values */); |
| 1314 | mKernelWakelockStats.put(name, kwlt); |
| 1315 | } |
| 1316 | return kwlt; |
| 1317 | } |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1318 | |
| 1319 | private void doDataPlug(long[] dataTransfer, long currentBytes) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1320 | dataTransfer[STATS_LAST] = dataTransfer[STATS_SINCE_UNPLUGGED]; |
| 1321 | dataTransfer[STATS_SINCE_UNPLUGGED] = -1; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | private void doDataUnplug(long[] dataTransfer, long currentBytes) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1325 | dataTransfer[STATS_SINCE_UNPLUGGED] = currentBytes; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1328 | /** |
| 1329 | * Radio uptime in microseconds when transferring data. This value is very approximate. |
| 1330 | * @return |
| 1331 | */ |
| 1332 | private long getCurrentRadioDataUptime() { |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1333 | try { |
| 1334 | File awakeTimeFile = new File("/sys/devices/virtual/net/rmnet0/awake_time_ms"); |
| 1335 | if (!awakeTimeFile.exists()) return 0; |
| 1336 | BufferedReader br = new BufferedReader(new FileReader(awakeTimeFile)); |
| 1337 | String line = br.readLine(); |
| 1338 | br.close(); |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1339 | return Long.parseLong(line) * 1000; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1340 | } catch (NumberFormatException nfe) { |
| 1341 | // Nothing |
| 1342 | } catch (IOException ioe) { |
| 1343 | // Nothing |
| 1344 | } |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1348 | /** |
| 1349 | * @deprecated use getRadioDataUptime |
| 1350 | */ |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1351 | public long getRadioDataUptimeMs() { |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1352 | return getRadioDataUptime() / 1000; |
| 1353 | } |
| 1354 | |
| 1355 | /** |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1356 | * Returns the duration that the cell radio was up for data transfers. |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1357 | */ |
| 1358 | public long getRadioDataUptime() { |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1359 | if (mRadioDataStart == -1) { |
| 1360 | return mRadioDataUptime; |
| 1361 | } else { |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1362 | return getCurrentRadioDataUptime() - mRadioDataStart; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1366 | private int getCurrentBluetoothPingCount() { |
| 1367 | if (mBtHeadset != null) { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1368 | List<BluetoothDevice> deviceList = mBtHeadset.getConnectedDevices(); |
| 1369 | if (deviceList.size() > 0) { |
| 1370 | return mBtHeadset.getBatteryUsageHint(deviceList.get(0)); |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 1371 | } |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1372 | } |
| 1373 | return -1; |
| 1374 | } |
| 1375 | |
| 1376 | public int getBluetoothPingCount() { |
| 1377 | if (mBluetoothPingStart == -1) { |
| 1378 | return mBluetoothPingCount; |
| 1379 | } else if (mBtHeadset != null) { |
| 1380 | return getCurrentBluetoothPingCount() - mBluetoothPingStart; |
| 1381 | } |
Amith Yamasani | 82cb029 | 2009-08-18 11:29:28 -0700 | [diff] [blame] | 1382 | return 0; |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | public void setBtHeadset(BluetoothHeadset headset) { |
Amith Yamasani | 82cb029 | 2009-08-18 11:29:28 -0700 | [diff] [blame] | 1386 | if (headset != null && mBtHeadset == null && isOnBattery() && mBluetoothPingStart == -1) { |
| 1387 | mBluetoothPingStart = getCurrentBluetoothPingCount(); |
| 1388 | } |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1389 | mBtHeadset = headset; |
| 1390 | } |
| 1391 | |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1392 | int mChangedBufferStates = 0; |
| 1393 | |
| 1394 | void addHistoryBufferLocked(long curTime) { |
| 1395 | if (!mHaveBatteryLevel || !mRecordingHistory) { |
| 1396 | return; |
| 1397 | } |
| 1398 | |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 1399 | final long timeDiff = (mHistoryBaseTime+curTime) - mHistoryLastWritten.time; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1400 | if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 1401 | && timeDiff < 2000 |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1402 | && ((mHistoryLastWritten.states^mHistoryCur.states)&mChangedBufferStates) == 0) { |
| 1403 | // If the current is the same as the one before, then we no |
| 1404 | // longer need the entry. |
| 1405 | mHistoryBuffer.setDataSize(mHistoryBufferLastPos); |
| 1406 | mHistoryBuffer.setDataPosition(mHistoryBufferLastPos); |
| 1407 | mHistoryBufferLastPos = -1; |
| 1408 | if (mHistoryLastLastWritten.cmd == HistoryItem.CMD_UPDATE |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 1409 | && timeDiff < 500 && mHistoryLastLastWritten.same(mHistoryCur)) { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1410 | // If this results in us returning to the state written |
| 1411 | // prior to the last one, then we can just delete the last |
| 1412 | // written one and drop the new one. Nothing more to do. |
| 1413 | mHistoryLastWritten.setTo(mHistoryLastLastWritten); |
| 1414 | mHistoryLastLastWritten.cmd = HistoryItem.CMD_NULL; |
| 1415 | return; |
| 1416 | } |
| 1417 | mChangedBufferStates |= mHistoryLastWritten.states^mHistoryCur.states; |
| 1418 | curTime = mHistoryLastWritten.time - mHistoryBaseTime; |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 1419 | mHistoryLastWritten.setTo(mHistoryLastLastWritten); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1420 | } else { |
| 1421 | mChangedBufferStates = 0; |
| 1422 | } |
| 1423 | |
| 1424 | final int dataSize = mHistoryBuffer.dataSize(); |
| 1425 | if (dataSize >= MAX_HISTORY_BUFFER) { |
| 1426 | if (!mHistoryOverflow) { |
| 1427 | mHistoryOverflow = true; |
| 1428 | addHistoryBufferLocked(curTime, HistoryItem.CMD_OVERFLOW); |
| 1429 | } |
| 1430 | |
| 1431 | // Once we've reached the maximum number of items, we only |
| 1432 | // record changes to the battery level and the most interesting states. |
| 1433 | // Once we've reached the maximum maximum number of items, we only |
| 1434 | // record changes to the battery level. |
| 1435 | if (mHistoryLastWritten.batteryLevel == mHistoryCur.batteryLevel && |
| 1436 | (dataSize >= MAX_MAX_HISTORY_BUFFER |
Amith Yamasani | 45f0646 | 2011-11-21 16:08:34 -0800 | [diff] [blame] | 1437 | || ((mHistoryLastWritten.states^mHistoryCur.states) |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1438 | & HistoryItem.MOST_INTERESTING_STATES) == 0)) { |
| 1439 | return; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | addHistoryBufferLocked(curTime, HistoryItem.CMD_UPDATE); |
| 1444 | } |
| 1445 | |
| 1446 | void addHistoryBufferLocked(long curTime, byte cmd) { |
| 1447 | int origPos = 0; |
| 1448 | if (mIteratingHistory) { |
| 1449 | origPos = mHistoryBuffer.dataPosition(); |
| 1450 | mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize()); |
| 1451 | } |
| 1452 | mHistoryBufferLastPos = mHistoryBuffer.dataPosition(); |
| 1453 | mHistoryLastLastWritten.setTo(mHistoryLastWritten); |
| 1454 | mHistoryLastWritten.setTo(mHistoryBaseTime + curTime, cmd, mHistoryCur); |
| 1455 | mHistoryLastWritten.writeDelta(mHistoryBuffer, mHistoryLastLastWritten); |
| 1456 | mLastHistoryTime = curTime; |
| 1457 | if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos |
| 1458 | + " now " + mHistoryBuffer.dataPosition() |
| 1459 | + " size is now " + mHistoryBuffer.dataSize()); |
| 1460 | if (mIteratingHistory) { |
| 1461 | mHistoryBuffer.setDataPosition(origPos); |
| 1462 | } |
| 1463 | } |
| 1464 | |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 1465 | int mChangedStates = 0; |
| 1466 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1467 | void addHistoryRecordLocked(long curTime) { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1468 | addHistoryBufferLocked(curTime); |
| 1469 | |
Dianne Hackborn | e8c88e6 | 2011-08-17 19:09:09 -0700 | [diff] [blame] | 1470 | if (!USE_OLD_HISTORY) { |
| 1471 | return; |
| 1472 | } |
| 1473 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1474 | if (!mHaveBatteryLevel || !mRecordingHistory) { |
| 1475 | return; |
| 1476 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1477 | |
| 1478 | // If the current time is basically the same as the last time, |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 1479 | // and no states have since the last recorded entry changed and |
| 1480 | // are now resetting back to their original value, then just collapse |
| 1481 | // into one record. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1482 | if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 1483 | && (mHistoryBaseTime+curTime) < (mHistoryEnd.time+2000) |
| 1484 | && ((mHistoryEnd.states^mHistoryCur.states)&mChangedStates) == 0) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1485 | // If the current is the same as the one before, then we no |
| 1486 | // longer need the entry. |
| 1487 | if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 1488 | && (mHistoryBaseTime+curTime) < (mHistoryEnd.time+500) |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1489 | && mHistoryLastEnd.same(mHistoryCur)) { |
| 1490 | mHistoryLastEnd.next = null; |
| 1491 | mHistoryEnd.next = mHistoryCache; |
| 1492 | mHistoryCache = mHistoryEnd; |
| 1493 | mHistoryEnd = mHistoryLastEnd; |
| 1494 | mHistoryLastEnd = null; |
| 1495 | } else { |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 1496 | mChangedStates |= mHistoryEnd.states^mHistoryCur.states; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1497 | mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, mHistoryCur); |
| 1498 | } |
| 1499 | return; |
| 1500 | } |
| 1501 | |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 1502 | mChangedStates = 0; |
| 1503 | |
| 1504 | if (mNumHistoryItems == MAX_HISTORY_ITEMS |
| 1505 | || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1506 | addHistoryRecordLocked(curTime, HistoryItem.CMD_OVERFLOW); |
| 1507 | } |
| 1508 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1509 | if (mNumHistoryItems >= MAX_HISTORY_ITEMS) { |
| 1510 | // Once we've reached the maximum number of items, we only |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 1511 | // record changes to the battery level and the most interesting states. |
| 1512 | // Once we've reached the maximum maximum number of items, we only |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1513 | // record changes to the battery level. |
| 1514 | if (mHistoryEnd != null && mHistoryEnd.batteryLevel |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 1515 | == mHistoryCur.batteryLevel && |
| 1516 | (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS |
| 1517 | || ((mHistoryEnd.states^mHistoryCur.states) |
| 1518 | & HistoryItem.MOST_INTERESTING_STATES) == 0)) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1519 | return; |
| 1520 | } |
| 1521 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1522 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1523 | addHistoryRecordLocked(curTime, HistoryItem.CMD_UPDATE); |
| 1524 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1525 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1526 | void addHistoryRecordLocked(long curTime, byte cmd) { |
| 1527 | HistoryItem rec = mHistoryCache; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1528 | if (rec != null) { |
| 1529 | mHistoryCache = rec.next; |
| 1530 | } else { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1531 | rec = new HistoryItem(); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1532 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1533 | rec.setTo(mHistoryBaseTime + curTime, cmd, mHistoryCur); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1534 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1535 | addHistoryRecordLocked(rec); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1536 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1537 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1538 | void addHistoryRecordLocked(HistoryItem rec) { |
| 1539 | mNumHistoryItems++; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1540 | rec.next = null; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1541 | mHistoryLastEnd = mHistoryEnd; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1542 | if (mHistoryEnd != null) { |
| 1543 | mHistoryEnd.next = rec; |
| 1544 | mHistoryEnd = rec; |
| 1545 | } else { |
| 1546 | mHistory = mHistoryEnd = rec; |
| 1547 | } |
| 1548 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1549 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1550 | void clearHistoryLocked() { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1551 | if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!"); |
Dianne Hackborn | e8c88e6 | 2011-08-17 19:09:09 -0700 | [diff] [blame] | 1552 | if (USE_OLD_HISTORY) { |
| 1553 | if (mHistory != null) { |
| 1554 | mHistoryEnd.next = mHistoryCache; |
| 1555 | mHistoryCache = mHistory; |
| 1556 | mHistory = mHistoryLastEnd = mHistoryEnd = null; |
| 1557 | } |
| 1558 | mNumHistoryItems = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1559 | } |
Dianne Hackborn | e8c88e6 | 2011-08-17 19:09:09 -0700 | [diff] [blame] | 1560 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1561 | mHistoryBaseTime = 0; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1562 | mLastHistoryTime = 0; |
| 1563 | |
| 1564 | mHistoryBuffer.setDataSize(0); |
| 1565 | mHistoryBuffer.setDataPosition(0); |
| 1566 | mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER/2); |
| 1567 | mHistoryLastLastWritten.cmd = HistoryItem.CMD_NULL; |
| 1568 | mHistoryLastWritten.cmd = HistoryItem.CMD_NULL; |
| 1569 | mHistoryBufferLastPos = -1; |
| 1570 | mHistoryOverflow = false; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1571 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1572 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1573 | public void doUnplugLocked(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1574 | NetworkStats.Entry entry = null; |
| 1575 | |
| 1576 | // Track UID data usage |
| 1577 | final NetworkStats uidStats = getNetworkStatsDetailGroupedByUid(); |
| 1578 | final int size = uidStats.size(); |
| 1579 | for (int i = 0; i < size; i++) { |
| 1580 | entry = uidStats.getValues(i, entry); |
| 1581 | |
Jeff Sharkey | 51797ed | 2013-06-04 14:09:31 -0700 | [diff] [blame] | 1582 | final Uid u = getUidStatsLocked(entry.uid); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1583 | u.mStartedTcpBytesReceived = entry.rxBytes; |
| 1584 | u.mStartedTcpBytesSent = entry.txBytes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | u.mTcpBytesReceivedAtLastUnplug = u.mCurrentTcpBytesReceived; |
| 1586 | u.mTcpBytesSentAtLastUnplug = u.mCurrentTcpBytesSent; |
| 1587 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | for (int i = mUnpluggables.size() - 1; i >= 0; i--) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1590 | mUnpluggables.get(i).unplug(elapsedRealtime, batteryUptime, batteryRealtime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1592 | |
| 1593 | // Track both mobile and total overall data |
| 1594 | final NetworkStats ifaceStats = getNetworkStatsSummary(); |
| 1595 | entry = ifaceStats.getTotal(entry, mMobileIfaces); |
| 1596 | doDataUnplug(mMobileDataRx, entry.rxBytes); |
| 1597 | doDataUnplug(mMobileDataTx, entry.txBytes); |
| 1598 | entry = ifaceStats.getTotal(entry); |
| 1599 | doDataUnplug(mTotalDataRx, entry.rxBytes); |
| 1600 | doDataUnplug(mTotalDataTx, entry.txBytes); |
| 1601 | |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1602 | // Track radio awake time |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1603 | mRadioDataStart = getCurrentRadioDataUptime(); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1604 | mRadioDataUptime = 0; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1605 | |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1606 | // Track bt headset ping count |
| 1607 | mBluetoothPingStart = getCurrentBluetoothPingCount(); |
| 1608 | mBluetoothPingCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1610 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1611 | public void doPlugLocked(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1612 | NetworkStats.Entry entry = null; |
| 1613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | for (int iu = mUidStats.size() - 1; iu >= 0; iu--) { |
| 1615 | Uid u = mUidStats.valueAt(iu); |
| 1616 | if (u.mStartedTcpBytesReceived >= 0) { |
| 1617 | u.mCurrentTcpBytesReceived = u.computeCurrentTcpBytesReceived(); |
| 1618 | u.mStartedTcpBytesReceived = -1; |
| 1619 | } |
| 1620 | if (u.mStartedTcpBytesSent >= 0) { |
| 1621 | u.mCurrentTcpBytesSent = u.computeCurrentTcpBytesSent(); |
| 1622 | u.mStartedTcpBytesSent = -1; |
| 1623 | } |
| 1624 | } |
| 1625 | for (int i = mUnpluggables.size() - 1; i >= 0; i--) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1626 | mUnpluggables.get(i).plug(elapsedRealtime, batteryUptime, batteryRealtime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1628 | |
| 1629 | // Track both mobile and total overall data |
| 1630 | final NetworkStats ifaceStats = getNetworkStatsSummary(); |
| 1631 | entry = ifaceStats.getTotal(entry, mMobileIfaces); |
| 1632 | doDataPlug(mMobileDataRx, entry.rxBytes); |
| 1633 | doDataPlug(mMobileDataTx, entry.txBytes); |
| 1634 | entry = ifaceStats.getTotal(entry); |
| 1635 | doDataPlug(mTotalDataRx, entry.rxBytes); |
| 1636 | doDataPlug(mTotalDataTx, entry.txBytes); |
| 1637 | |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1638 | // Track radio awake time |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1639 | mRadioDataUptime = getRadioDataUptime(); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1640 | mRadioDataStart = -1; |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 1641 | |
| 1642 | // Track bt headset ping count |
| 1643 | mBluetoothPingCount = getBluetoothPingCount(); |
| 1644 | mBluetoothPingStart = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | } |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1646 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1647 | int mWakeLockNesting; |
| 1648 | |
| 1649 | public void noteStartWakeLocked(int uid, int pid, String name, int type) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1650 | if (type == WAKE_TYPE_PARTIAL) { |
| 1651 | // Only care about partial wake locks, since full wake locks |
| 1652 | // will be canceled when the user puts the screen to sleep. |
| 1653 | if (mWakeLockNesting == 0) { |
| 1654 | mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG; |
| 1655 | if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: " |
| 1656 | + Integer.toHexString(mHistoryCur.states)); |
| 1657 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
| 1658 | } |
| 1659 | mWakeLockNesting++; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1660 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1661 | if (uid >= 0) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1662 | if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) { |
| 1663 | Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS); |
| 1664 | mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS); |
| 1665 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1666 | getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | public void noteStopWakeLocked(int uid, int pid, String name, int type) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1671 | if (type == WAKE_TYPE_PARTIAL) { |
| 1672 | mWakeLockNesting--; |
| 1673 | if (mWakeLockNesting == 0) { |
| 1674 | mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG; |
| 1675 | if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: " |
| 1676 | + Integer.toHexString(mHistoryCur.states)); |
| 1677 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
| 1678 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1679 | } |
| 1680 | if (uid >= 0) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1681 | if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) { |
| 1682 | Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS); |
| 1683 | mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS); |
| 1684 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1685 | getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type); |
| 1686 | } |
| 1687 | } |
| 1688 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1689 | public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name, int type) { |
| 1690 | int N = ws.size(); |
| 1691 | for (int i=0; i<N; i++) { |
| 1692 | noteStartWakeLocked(ws.get(i), pid, name, type); |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name, int type) { |
| 1697 | int N = ws.size(); |
| 1698 | for (int i=0; i<N; i++) { |
| 1699 | noteStopWakeLocked(ws.get(i), pid, name, type); |
| 1700 | } |
| 1701 | } |
| 1702 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1703 | public int startAddingCpuLocked() { |
| 1704 | mHandler.removeMessages(MSG_UPDATE_WAKELOCKS); |
| 1705 | |
| 1706 | if (mScreenOn) { |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | final int N = mPartialTimers.size(); |
| 1711 | if (N == 0) { |
| 1712 | mLastPartialTimers.clear(); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | // How many timers should consume CPU? Only want to include ones |
| 1717 | // that have already been in the list. |
| 1718 | for (int i=0; i<N; i++) { |
| 1719 | StopwatchTimer st = mPartialTimers.get(i); |
| 1720 | if (st.mInList) { |
| 1721 | Uid uid = st.mUid; |
| 1722 | // We don't include the system UID, because it so often |
| 1723 | // holds wake locks at one request or another of an app. |
| 1724 | if (uid != null && uid.mUid != Process.SYSTEM_UID) { |
| 1725 | return 50; |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | public void finishAddingCpuLocked(int perc, int utime, int stime, long[] cpuSpeedTimes) { |
| 1734 | final int N = mPartialTimers.size(); |
| 1735 | if (perc != 0) { |
| 1736 | int num = 0; |
| 1737 | for (int i=0; i<N; i++) { |
| 1738 | StopwatchTimer st = mPartialTimers.get(i); |
| 1739 | if (st.mInList) { |
| 1740 | Uid uid = st.mUid; |
| 1741 | // We don't include the system UID, because it so often |
| 1742 | // holds wake locks at one request or another of an app. |
| 1743 | if (uid != null && uid.mUid != Process.SYSTEM_UID) { |
| 1744 | num++; |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | if (num != 0) { |
| 1749 | for (int i=0; i<N; i++) { |
| 1750 | StopwatchTimer st = mPartialTimers.get(i); |
| 1751 | if (st.mInList) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1752 | Uid uid = st.mUid; |
| 1753 | if (uid != null && uid.mUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 618b8c1 | 2010-09-09 23:10:38 -0700 | [diff] [blame] | 1754 | int myUTime = utime/num; |
| 1755 | int mySTime = stime/num; |
| 1756 | utime -= myUTime; |
| 1757 | stime -= mySTime; |
| 1758 | num--; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1759 | Uid.Proc proc = uid.getProcessStatsLocked("*wakelock*"); |
| 1760 | proc.addCpuTimeLocked(myUTime, mySTime); |
| 1761 | proc.addSpeedStepTimes(cpuSpeedTimes); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | // Just in case, collect any lost CPU time. |
| 1768 | if (utime != 0 || stime != 0) { |
| 1769 | Uid uid = getUidStatsLocked(Process.SYSTEM_UID); |
| 1770 | if (uid != null) { |
| 1771 | Uid.Proc proc = uid.getProcessStatsLocked("*lost*"); |
| 1772 | proc.addCpuTimeLocked(utime, stime); |
| 1773 | proc.addSpeedStepTimes(cpuSpeedTimes); |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | final int NL = mLastPartialTimers.size(); |
| 1779 | boolean diff = N != NL; |
| 1780 | for (int i=0; i<NL && !diff; i++) { |
| 1781 | diff |= mPartialTimers.get(i) != mLastPartialTimers.get(i); |
| 1782 | } |
| 1783 | if (!diff) { |
| 1784 | for (int i=0; i<NL; i++) { |
| 1785 | mPartialTimers.get(i).mInList = true; |
| 1786 | } |
| 1787 | return; |
| 1788 | } |
| 1789 | |
| 1790 | for (int i=0; i<NL; i++) { |
| 1791 | mLastPartialTimers.get(i).mInList = false; |
| 1792 | } |
| 1793 | mLastPartialTimers.clear(); |
| 1794 | for (int i=0; i<N; i++) { |
| 1795 | StopwatchTimer st = mPartialTimers.get(i); |
| 1796 | st.mInList = true; |
| 1797 | mLastPartialTimers.add(st); |
| 1798 | } |
| 1799 | } |
| 1800 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1801 | public void noteProcessDiedLocked(int uid, int pid) { |
| 1802 | Uid u = mUidStats.get(uid); |
| 1803 | if (u != null) { |
| 1804 | u.mPids.remove(pid); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | public long getProcessWakeTime(int uid, int pid, long realtime) { |
| 1809 | Uid u = mUidStats.get(uid); |
| 1810 | if (u != null) { |
| 1811 | Uid.Pid p = u.mPids.get(pid); |
| 1812 | if (p != null) { |
| 1813 | return p.mWakeSum + (p.mWakeStart != 0 ? (realtime - p.mWakeStart) : 0); |
| 1814 | } |
| 1815 | } |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
| 1819 | public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) { |
| 1820 | Uid u = mUidStats.get(uid); |
| 1821 | if (u != null) { |
| 1822 | u.reportExcessiveWakeLocked(proc, overTime, usedTime); |
| 1823 | } |
| 1824 | } |
| 1825 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1826 | public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) { |
| 1827 | Uid u = mUidStats.get(uid); |
| 1828 | if (u != null) { |
| 1829 | u.reportExcessiveCpuLocked(proc, overTime, usedTime); |
| 1830 | } |
| 1831 | } |
| 1832 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1833 | int mSensorNesting; |
| 1834 | |
| 1835 | public void noteStartSensorLocked(int uid, int sensor) { |
| 1836 | if (mSensorNesting == 0) { |
| 1837 | mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG; |
| 1838 | if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: " |
| 1839 | + Integer.toHexString(mHistoryCur.states)); |
| 1840 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
| 1841 | } |
| 1842 | mSensorNesting++; |
| 1843 | getUidStatsLocked(uid).noteStartSensor(sensor); |
| 1844 | } |
| 1845 | |
| 1846 | public void noteStopSensorLocked(int uid, int sensor) { |
| 1847 | mSensorNesting--; |
| 1848 | if (mSensorNesting == 0) { |
| 1849 | mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG; |
| 1850 | if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: " |
| 1851 | + Integer.toHexString(mHistoryCur.states)); |
| 1852 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
| 1853 | } |
| 1854 | getUidStatsLocked(uid).noteStopSensor(sensor); |
| 1855 | } |
| 1856 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1857 | int mGpsNesting; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1858 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1859 | public void noteStartGpsLocked(int uid) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1860 | if (mGpsNesting == 0) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1861 | mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1862 | if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: " |
| 1863 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1864 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1865 | } |
| 1866 | mGpsNesting++; |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 1867 | getUidStatsLocked(uid).noteStartGps(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1868 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1869 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1870 | public void noteStopGpsLocked(int uid) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1871 | mGpsNesting--; |
| 1872 | if (mGpsNesting == 0) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1873 | mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1874 | if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: " |
| 1875 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1876 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1877 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 1878 | getUidStatsLocked(uid).noteStopGps(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1879 | } |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 1880 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1881 | public void noteScreenOnLocked() { |
| 1882 | if (!mScreenOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1883 | mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1884 | if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: " |
| 1885 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1886 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1887 | mScreenOn = true; |
| 1888 | mScreenOnTimer.startRunningLocked(this); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1889 | if (mScreenBrightnessBin >= 0) { |
| 1890 | mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(this); |
| 1891 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1892 | |
| 1893 | // Fake a wake lock, so we consider the device waked as long |
| 1894 | // as the screen is on. |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1895 | noteStartWakeLocked(-1, -1, "dummy", WAKE_TYPE_PARTIAL); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 1896 | |
| 1897 | // Update discharge amounts. |
| 1898 | if (mOnBatteryInternal) { |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 1899 | updateDischargeScreenLevelsLocked(false, true); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 1900 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | } |
| 1902 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1903 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1904 | public void noteScreenOffLocked() { |
| 1905 | if (mScreenOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1906 | mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1907 | if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: " |
| 1908 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1909 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | mScreenOn = false; |
| 1911 | mScreenOnTimer.stopRunningLocked(this); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1912 | if (mScreenBrightnessBin >= 0) { |
| 1913 | mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(this); |
| 1914 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1915 | |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1916 | noteStopWakeLocked(-1, -1, "dummy", WAKE_TYPE_PARTIAL); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 1917 | |
| 1918 | // Update discharge amounts. |
| 1919 | if (mOnBatteryInternal) { |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 1920 | updateDischargeScreenLevelsLocked(true, false); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 1921 | } |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1922 | } |
| 1923 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1924 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1925 | public void noteScreenBrightnessLocked(int brightness) { |
| 1926 | // Bin the brightness. |
| 1927 | int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS); |
| 1928 | if (bin < 0) bin = 0; |
| 1929 | else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1; |
| 1930 | if (mScreenBrightnessBin != bin) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1931 | mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK) |
| 1932 | | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1933 | if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: " |
| 1934 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1935 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1936 | if (mScreenOn) { |
| 1937 | if (mScreenBrightnessBin >= 0) { |
| 1938 | mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(this); |
| 1939 | } |
| 1940 | mScreenBrightnessTimer[bin].startRunningLocked(this); |
| 1941 | } |
| 1942 | mScreenBrightnessBin = bin; |
| 1943 | } |
| 1944 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1945 | |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 1946 | public void noteInputEventAtomic() { |
| 1947 | mInputEventCounter.stepAtomic(); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1948 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1949 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1950 | public void noteUserActivityLocked(int uid, int event) { |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 1951 | getUidStatsLocked(uid).noteUserActivityLocked(event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1952 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1953 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1954 | public void notePhoneOnLocked() { |
| 1955 | if (!mPhoneOn) { |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 1956 | mHistoryCur.states |= HistoryItem.STATE_PHONE_IN_CALL_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1957 | if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: " |
| 1958 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1959 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | mPhoneOn = true; |
| 1961 | mPhoneOnTimer.startRunningLocked(this); |
| 1962 | } |
| 1963 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | public void notePhoneOffLocked() { |
| 1966 | if (mPhoneOn) { |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 1967 | mHistoryCur.states &= ~HistoryItem.STATE_PHONE_IN_CALL_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 1968 | if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: " |
| 1969 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 1970 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1971 | mPhoneOn = false; |
| 1972 | mPhoneOnTimer.stopRunningLocked(this); |
| 1973 | } |
| 1974 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1975 | |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 1976 | void stopAllSignalStrengthTimersLocked(int except) { |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 1977 | for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 1978 | if (i == except) { |
| 1979 | continue; |
| 1980 | } |
| 1981 | while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) { |
| 1982 | mPhoneSignalStrengthsTimer[i].stopRunningLocked(this); |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1987 | private int fixPhoneServiceState(int state, int signalBin) { |
| 1988 | if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) { |
| 1989 | // In this case we will always be STATE_OUT_OF_SERVICE, so need |
| 1990 | // to infer that we are scanning from other data. |
| 1991 | if (state == ServiceState.STATE_OUT_OF_SERVICE |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 1992 | && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1993 | state = ServiceState.STATE_IN_SERVICE; |
| 1994 | } |
| 1995 | } |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 1996 | |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1997 | return state; |
| 1998 | } |
| 1999 | |
| 2000 | private void updateAllPhoneStateLocked(int state, int simState, int bin) { |
| 2001 | boolean scanning = false; |
| 2002 | boolean newHistory = false; |
| 2003 | |
| 2004 | mPhoneServiceStateRaw = state; |
| 2005 | mPhoneSimStateRaw = simState; |
| 2006 | mPhoneSignalStrengthBinRaw = bin; |
| 2007 | |
| 2008 | if (simState == TelephonyManager.SIM_STATE_ABSENT) { |
| 2009 | // In this case we will always be STATE_OUT_OF_SERVICE, so need |
| 2010 | // to infer that we are scanning from other data. |
| 2011 | if (state == ServiceState.STATE_OUT_OF_SERVICE |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 2012 | && bin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2013 | state = ServiceState.STATE_IN_SERVICE; |
| 2014 | } |
| 2015 | } |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 2016 | |
| 2017 | // If the phone is powered off, stop all timers. |
| 2018 | if (state == ServiceState.STATE_POWER_OFF) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2019 | bin = -1; |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 2020 | |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2021 | // If we are in service, make sure the correct signal string timer is running. |
| 2022 | } else if (state == ServiceState.STATE_IN_SERVICE) { |
| 2023 | // Bin will be changed below. |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 2024 | |
| 2025 | // If we're out of service, we are in the lowest signal strength |
| 2026 | // bin and have the scanning bit set. |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 2027 | } else if (state == ServiceState.STATE_OUT_OF_SERVICE) { |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 2028 | scanning = true; |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 2029 | bin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN; |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 2030 | if (!mPhoneSignalScanningTimer.isRunningLocked()) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2031 | mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2032 | newHistory = true; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2033 | if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: " |
| 2034 | + Integer.toHexString(mHistoryCur.states)); |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 2035 | mPhoneSignalScanningTimer.startRunningLocked(this); |
| 2036 | } |
| 2037 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2038 | |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 2039 | if (!scanning) { |
| 2040 | // If we are no longer scanning, then stop the scanning timer. |
| 2041 | if (mPhoneSignalScanningTimer.isRunningLocked()) { |
| 2042 | mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG; |
| 2043 | if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: " |
| 2044 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2045 | newHistory = true; |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 2046 | mPhoneSignalScanningTimer.stopRunningLocked(this); |
| 2047 | } |
| 2048 | } |
| 2049 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2050 | if (mPhoneServiceState != state) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2051 | mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK) |
| 2052 | | (state << HistoryItem.STATE_PHONE_STATE_SHIFT); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2053 | if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: " |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2054 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2055 | newHistory = true; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2056 | mPhoneServiceState = state; |
| 2057 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2058 | |
| 2059 | if (mPhoneSignalStrengthBin != bin) { |
| 2060 | if (mPhoneSignalStrengthBin >= 0) { |
| 2061 | mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(this); |
| 2062 | } |
| 2063 | if (bin >= 0) { |
| 2064 | if (!mPhoneSignalStrengthsTimer[bin].isRunningLocked()) { |
| 2065 | mPhoneSignalStrengthsTimer[bin].startRunningLocked(this); |
| 2066 | } |
| 2067 | mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_SIGNAL_STRENGTH_MASK) |
| 2068 | | (bin << HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT); |
| 2069 | if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + bin + " to: " |
| 2070 | + Integer.toHexString(mHistoryCur.states)); |
| 2071 | newHistory = true; |
| 2072 | } else { |
| 2073 | stopAllSignalStrengthTimersLocked(-1); |
| 2074 | } |
| 2075 | mPhoneSignalStrengthBin = bin; |
| 2076 | } |
| 2077 | |
| 2078 | if (newHistory) { |
| 2079 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | /** |
| 2084 | * Telephony stack updates the phone state. |
| 2085 | * @param state phone state from ServiceState.getState() |
| 2086 | */ |
| 2087 | public void notePhoneStateLocked(int state, int simState) { |
| 2088 | updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 2091 | public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2092 | // Bin the strength. |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 2093 | int bin = signalStrength.getLevel(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 2094 | updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2095 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2096 | |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2097 | public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) { |
| 2098 | int bin = DATA_CONNECTION_NONE; |
| 2099 | if (hasData) { |
| 2100 | switch (dataType) { |
| 2101 | case TelephonyManager.NETWORK_TYPE_EDGE: |
| 2102 | bin = DATA_CONNECTION_EDGE; |
| 2103 | break; |
| 2104 | case TelephonyManager.NETWORK_TYPE_GPRS: |
| 2105 | bin = DATA_CONNECTION_GPRS; |
| 2106 | break; |
| 2107 | case TelephonyManager.NETWORK_TYPE_UMTS: |
| 2108 | bin = DATA_CONNECTION_UMTS; |
| 2109 | break; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2110 | case TelephonyManager.NETWORK_TYPE_CDMA: |
| 2111 | bin = DATA_CONNECTION_CDMA; |
| 2112 | break; |
| 2113 | case TelephonyManager.NETWORK_TYPE_EVDO_0: |
| 2114 | bin = DATA_CONNECTION_EVDO_0; |
| 2115 | break; |
| 2116 | case TelephonyManager.NETWORK_TYPE_EVDO_A: |
| 2117 | bin = DATA_CONNECTION_EVDO_A; |
| 2118 | break; |
| 2119 | case TelephonyManager.NETWORK_TYPE_1xRTT: |
| 2120 | bin = DATA_CONNECTION_1xRTT; |
| 2121 | break; |
| 2122 | case TelephonyManager.NETWORK_TYPE_HSDPA: |
| 2123 | bin = DATA_CONNECTION_HSDPA; |
| 2124 | break; |
| 2125 | case TelephonyManager.NETWORK_TYPE_HSUPA: |
| 2126 | bin = DATA_CONNECTION_HSUPA; |
| 2127 | break; |
| 2128 | case TelephonyManager.NETWORK_TYPE_HSPA: |
| 2129 | bin = DATA_CONNECTION_HSPA; |
| 2130 | break; |
| 2131 | case TelephonyManager.NETWORK_TYPE_IDEN: |
| 2132 | bin = DATA_CONNECTION_IDEN; |
| 2133 | break; |
| 2134 | case TelephonyManager.NETWORK_TYPE_EVDO_B: |
| 2135 | bin = DATA_CONNECTION_EVDO_B; |
| 2136 | break; |
Robert Greenwalt | 962a990 | 2010-11-02 11:10:25 -0700 | [diff] [blame] | 2137 | case TelephonyManager.NETWORK_TYPE_LTE: |
| 2138 | bin = DATA_CONNECTION_LTE; |
| 2139 | break; |
| 2140 | case TelephonyManager.NETWORK_TYPE_EHRPD: |
| 2141 | bin = DATA_CONNECTION_EHRPD; |
| 2142 | break; |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2143 | default: |
| 2144 | bin = DATA_CONNECTION_OTHER; |
| 2145 | break; |
| 2146 | } |
| 2147 | } |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 2148 | if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2149 | if (mPhoneDataConnectionType != bin) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2150 | mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK) |
| 2151 | | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2152 | if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: " |
| 2153 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2154 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2155 | if (mPhoneDataConnectionType >= 0) { |
| 2156 | mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(this); |
| 2157 | } |
| 2158 | mPhoneDataConnectionType = bin; |
| 2159 | mPhoneDataConnectionsTimer[bin].startRunningLocked(this); |
| 2160 | } |
| 2161 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2162 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2163 | public void noteWifiOnLocked() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2164 | if (!mWifiOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2165 | mHistoryCur.states |= HistoryItem.STATE_WIFI_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2166 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: " |
| 2167 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2168 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2169 | mWifiOn = true; |
| 2170 | mWifiOnTimer.startRunningLocked(this); |
| 2171 | } |
| 2172 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2173 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2174 | public void noteWifiOffLocked() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2175 | if (mWifiOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2176 | mHistoryCur.states &= ~HistoryItem.STATE_WIFI_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2177 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: " |
| 2178 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2179 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2180 | mWifiOn = false; |
| 2181 | mWifiOnTimer.stopRunningLocked(this); |
| 2182 | } |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2183 | if (mWifiOnUid >= 0) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2184 | getUidStatsLocked(mWifiOnUid).noteWifiStoppedLocked(); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2185 | mWifiOnUid = -1; |
| 2186 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2187 | } |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2188 | |
| 2189 | public void noteAudioOnLocked(int uid) { |
| 2190 | if (!mAudioOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2191 | mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2192 | if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: " |
| 2193 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2194 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2195 | mAudioOn = true; |
| 2196 | mAudioOnTimer.startRunningLocked(this); |
| 2197 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2198 | getUidStatsLocked(uid).noteAudioTurnedOnLocked(); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2199 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2200 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2201 | public void noteAudioOffLocked(int uid) { |
| 2202 | if (mAudioOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2203 | mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2204 | if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: " |
| 2205 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2206 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2207 | mAudioOn = false; |
| 2208 | mAudioOnTimer.stopRunningLocked(this); |
| 2209 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2210 | getUidStatsLocked(uid).noteAudioTurnedOffLocked(); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | public void noteVideoOnLocked(int uid) { |
| 2214 | if (!mVideoOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2215 | mHistoryCur.states |= HistoryItem.STATE_VIDEO_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2216 | if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: " |
| 2217 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2218 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2219 | mVideoOn = true; |
| 2220 | mVideoOnTimer.startRunningLocked(this); |
| 2221 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2222 | getUidStatsLocked(uid).noteVideoTurnedOnLocked(); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2223 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2224 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2225 | public void noteVideoOffLocked(int uid) { |
| 2226 | if (mVideoOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2227 | mHistoryCur.states &= ~HistoryItem.STATE_VIDEO_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2228 | if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: " |
| 2229 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2230 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2231 | mVideoOn = false; |
| 2232 | mVideoOnTimer.stopRunningLocked(this); |
| 2233 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2234 | getUidStatsLocked(uid).noteVideoTurnedOffLocked(); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2237 | public void noteVibratorOnLocked(int uid, long durationMillis) { |
| 2238 | getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis); |
| 2239 | } |
| 2240 | |
| 2241 | public void noteVibratorOffLocked(int uid) { |
| 2242 | getUidStatsLocked(uid).noteVibratorOffLocked(); |
| 2243 | } |
| 2244 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2245 | public void noteWifiRunningLocked(WorkSource ws) { |
| 2246 | if (!mGlobalWifiRunning) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2247 | mHistoryCur.states |= HistoryItem.STATE_WIFI_RUNNING_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2248 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: " |
| 2249 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2250 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2251 | mGlobalWifiRunning = true; |
| 2252 | mGlobalWifiRunningTimer.startRunningLocked(this); |
| 2253 | int N = ws.size(); |
| 2254 | for (int i=0; i<N; i++) { |
| 2255 | getUidStatsLocked(ws.get(i)).noteWifiRunningLocked(); |
| 2256 | } |
| 2257 | } else { |
| 2258 | Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running"); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2262 | public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) { |
| 2263 | if (mGlobalWifiRunning) { |
| 2264 | int N = oldWs.size(); |
| 2265 | for (int i=0; i<N; i++) { |
| 2266 | getUidStatsLocked(oldWs.get(i)).noteWifiStoppedLocked(); |
| 2267 | } |
| 2268 | N = newWs.size(); |
| 2269 | for (int i=0; i<N; i++) { |
| 2270 | getUidStatsLocked(newWs.get(i)).noteWifiRunningLocked(); |
| 2271 | } |
| 2272 | } else { |
| 2273 | Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running"); |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | public void noteWifiStoppedLocked(WorkSource ws) { |
| 2278 | if (mGlobalWifiRunning) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2279 | mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RUNNING_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2280 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: " |
| 2281 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2282 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2283 | mGlobalWifiRunning = false; |
| 2284 | mGlobalWifiRunningTimer.stopRunningLocked(this); |
| 2285 | int N = ws.size(); |
| 2286 | for (int i=0; i<N; i++) { |
| 2287 | getUidStatsLocked(ws.get(i)).noteWifiStoppedLocked(); |
| 2288 | } |
| 2289 | } else { |
| 2290 | Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running"); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2294 | public void noteBluetoothOnLocked() { |
| 2295 | if (!mBluetoothOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2296 | mHistoryCur.states |= HistoryItem.STATE_BLUETOOTH_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2297 | if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth on to: " |
| 2298 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2299 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2300 | mBluetoothOn = true; |
| 2301 | mBluetoothOnTimer.startRunningLocked(this); |
| 2302 | } |
| 2303 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2304 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2305 | public void noteBluetoothOffLocked() { |
| 2306 | if (mBluetoothOn) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2307 | mHistoryCur.states &= ~HistoryItem.STATE_BLUETOOTH_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2308 | if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth off to: " |
| 2309 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2310 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2311 | mBluetoothOn = false; |
| 2312 | mBluetoothOnTimer.stopRunningLocked(this); |
| 2313 | } |
| 2314 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2315 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2316 | int mWifiFullLockNesting = 0; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2317 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2318 | public void noteFullWifiLockAcquiredLocked(int uid) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2319 | if (mWifiFullLockNesting == 0) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2320 | mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2321 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: " |
| 2322 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2323 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2324 | } |
| 2325 | mWifiFullLockNesting++; |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2326 | getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | public void noteFullWifiLockReleasedLocked(int uid) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2330 | mWifiFullLockNesting--; |
| 2331 | if (mWifiFullLockNesting == 0) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2332 | mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2333 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: " |
| 2334 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2335 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2336 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2337 | getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2338 | } |
| 2339 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2340 | int mWifiScanNesting = 0; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2341 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2342 | public void noteWifiScanStartedLocked(int uid) { |
| 2343 | if (mWifiScanNesting == 0) { |
| 2344 | mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG; |
| 2345 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: " |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2346 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2347 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2348 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2349 | mWifiScanNesting++; |
| 2350 | getUidStatsLocked(uid).noteWifiScanStartedLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2353 | public void noteWifiScanStoppedLocked(int uid) { |
| 2354 | mWifiScanNesting--; |
| 2355 | if (mWifiScanNesting == 0) { |
| 2356 | mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG; |
| 2357 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: " |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2358 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2359 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2360 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2361 | getUidStatsLocked(uid).noteWifiScanStoppedLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2362 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2363 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2364 | int mWifiMulticastNesting = 0; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2365 | |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2366 | public void noteWifiMulticastEnabledLocked(int uid) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2367 | if (mWifiMulticastNesting == 0) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2368 | mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2369 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: " |
| 2370 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2371 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2372 | } |
| 2373 | mWifiMulticastNesting++; |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2374 | getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | public void noteWifiMulticastDisabledLocked(int uid) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2378 | mWifiMulticastNesting--; |
| 2379 | if (mWifiMulticastNesting == 0) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2380 | mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2381 | if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: " |
| 2382 | + Integer.toHexString(mHistoryCur.states)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2383 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2384 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 2385 | getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 2388 | public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) { |
| 2389 | int N = ws.size(); |
| 2390 | for (int i=0; i<N; i++) { |
| 2391 | noteFullWifiLockAcquiredLocked(ws.get(i)); |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) { |
| 2396 | int N = ws.size(); |
| 2397 | for (int i=0; i<N; i++) { |
| 2398 | noteFullWifiLockReleasedLocked(ws.get(i)); |
| 2399 | } |
| 2400 | } |
| 2401 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2402 | public void noteWifiScanStartedFromSourceLocked(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 2403 | int N = ws.size(); |
| 2404 | for (int i=0; i<N; i++) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2405 | noteWifiScanStartedLocked(ws.get(i)); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2409 | public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 2410 | int N = ws.size(); |
| 2411 | for (int i=0; i<N; i++) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2412 | noteWifiScanStoppedLocked(ws.get(i)); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) { |
| 2417 | int N = ws.size(); |
| 2418 | for (int i=0; i<N; i++) { |
| 2419 | noteWifiMulticastEnabledLocked(ws.get(i)); |
| 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) { |
| 2424 | int N = ws.size(); |
| 2425 | for (int i=0; i<N; i++) { |
| 2426 | noteWifiMulticastDisabledLocked(ws.get(i)); |
| 2427 | } |
| 2428 | } |
| 2429 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2430 | public void noteNetworkInterfaceTypeLocked(String iface, int networkType) { |
| 2431 | if (ConnectivityManager.isNetworkTypeMobile(networkType)) { |
| 2432 | mMobileIfaces.add(iface); |
| 2433 | } else { |
| 2434 | mMobileIfaces.remove(iface); |
| 2435 | } |
| 2436 | } |
| 2437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2438 | @Override public long getScreenOnTime(long batteryRealtime, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2439 | return mScreenOnTimer.getTotalTimeLocked(batteryRealtime, which); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2440 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2441 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2442 | @Override public long getScreenBrightnessTime(int brightnessBin, |
| 2443 | long batteryRealtime, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2444 | return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked( |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2445 | batteryRealtime, which); |
| 2446 | } |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2447 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2448 | @Override public int getInputEventCount(int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2449 | return mInputEventCounter.getCountLocked(which); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2450 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2452 | @Override public long getPhoneOnTime(long batteryRealtime, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2453 | return mPhoneOnTimer.getTotalTimeLocked(batteryRealtime, which); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2454 | } |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2455 | |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2456 | @Override public long getPhoneSignalStrengthTime(int strengthBin, |
| 2457 | long batteryRealtime, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2458 | return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked( |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2459 | batteryRealtime, which); |
| 2460 | } |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 2461 | |
| 2462 | @Override public long getPhoneSignalScanningTime( |
| 2463 | long batteryRealtime, int which) { |
| 2464 | return mPhoneSignalScanningTimer.getTotalTimeLocked( |
| 2465 | batteryRealtime, which); |
| 2466 | } |
| 2467 | |
Catherine Liu | fb90081 | 2012-07-17 14:12:56 -0500 | [diff] [blame] | 2468 | @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) { |
| 2469 | return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2470 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2471 | |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2472 | @Override public long getPhoneDataConnectionTime(int dataType, |
| 2473 | long batteryRealtime, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2474 | return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked( |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 2475 | batteryRealtime, which); |
| 2476 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2477 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2478 | @Override public int getPhoneDataConnectionCount(int dataType, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2479 | return mPhoneDataConnectionsTimer[dataType].getCountLocked(which); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2480 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2481 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2482 | @Override public long getWifiOnTime(long batteryRealtime, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2483 | return mWifiOnTimer.getTotalTimeLocked(batteryRealtime, which); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2484 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2485 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2486 | @Override public long getGlobalWifiRunningTime(long batteryRealtime, int which) { |
| 2487 | return mGlobalWifiRunningTimer.getTotalTimeLocked(batteryRealtime, which); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2490 | @Override public long getBluetoothOnTime(long batteryRealtime, int which) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2491 | return mBluetoothOnTimer.getTotalTimeLocked(batteryRealtime, which); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2492 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | @Override public boolean getIsOnBattery() { |
| 2495 | return mOnBattery; |
| 2496 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2498 | @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() { |
| 2499 | return mUidStats; |
| 2500 | } |
| 2501 | |
| 2502 | /** |
| 2503 | * The statistics associated with a particular uid. |
| 2504 | */ |
| 2505 | public final class Uid extends BatteryStats.Uid { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2506 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2507 | final int mUid; |
| 2508 | long mLoadedTcpBytesReceived; |
| 2509 | long mLoadedTcpBytesSent; |
| 2510 | long mCurrentTcpBytesReceived; |
| 2511 | long mCurrentTcpBytesSent; |
| 2512 | long mTcpBytesReceivedAtLastUnplug; |
| 2513 | long mTcpBytesSentAtLastUnplug; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2514 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2515 | // These are not saved/restored when parcelling, since we want |
| 2516 | // to return from the parcel with a snapshot of the state. |
| 2517 | long mStartedTcpBytesReceived = -1; |
| 2518 | long mStartedTcpBytesSent = -1; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2519 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2520 | boolean mWifiRunning; |
| 2521 | StopwatchTimer mWifiRunningTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2522 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2523 | boolean mFullWifiLockOut; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2524 | StopwatchTimer mFullWifiLockTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2525 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2526 | boolean mWifiScanStarted; |
| 2527 | StopwatchTimer mWifiScanTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2528 | |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2529 | boolean mWifiMulticastEnabled; |
| 2530 | StopwatchTimer mWifiMulticastTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2531 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2532 | boolean mAudioTurnedOn; |
| 2533 | StopwatchTimer mAudioTurnedOnTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2534 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2535 | boolean mVideoTurnedOn; |
| 2536 | StopwatchTimer mVideoTurnedOnTimer; |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2537 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2538 | BatchTimer mVibratorOnTimer; |
| 2539 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2540 | Counter[] mUserActivityCounters; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2541 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2542 | /** |
| 2543 | * The statistics we have collected for this uid's wake locks. |
| 2544 | */ |
| 2545 | final HashMap<String, Wakelock> mWakelockStats = new HashMap<String, Wakelock>(); |
| 2546 | |
| 2547 | /** |
| 2548 | * The statistics we have collected for this uid's sensor activations. |
| 2549 | */ |
| 2550 | final HashMap<Integer, Sensor> mSensorStats = new HashMap<Integer, Sensor>(); |
| 2551 | |
| 2552 | /** |
| 2553 | * The statistics we have collected for this uid's processes. |
| 2554 | */ |
| 2555 | final HashMap<String, Proc> mProcessStats = new HashMap<String, Proc>(); |
| 2556 | |
| 2557 | /** |
| 2558 | * The statistics we have collected for this uid's processes. |
| 2559 | */ |
| 2560 | final HashMap<String, Pkg> mPackageStats = new HashMap<String, Pkg>(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2561 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2562 | /** |
| 2563 | * The transient wake stats we have collected for this uid's pids. |
| 2564 | */ |
| 2565 | final SparseArray<Pid> mPids = new SparseArray<Pid>(); |
| 2566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2567 | public Uid(int uid) { |
| 2568 | mUid = uid; |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2569 | mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING, |
| 2570 | mWifiRunningTimers, mUnpluggables); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 2571 | mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK, |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2572 | mFullWifiLockTimers, mUnpluggables); |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2573 | mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN, |
| 2574 | mWifiScanTimers, mUnpluggables); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 2575 | mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED, |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2576 | mWifiMulticastTimers, mUnpluggables); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | @Override |
| 2580 | public Map<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() { |
| 2581 | return mWakelockStats; |
| 2582 | } |
| 2583 | |
| 2584 | @Override |
| 2585 | public Map<Integer, ? extends BatteryStats.Uid.Sensor> getSensorStats() { |
| 2586 | return mSensorStats; |
| 2587 | } |
| 2588 | |
| 2589 | @Override |
| 2590 | public Map<String, ? extends BatteryStats.Uid.Proc> getProcessStats() { |
| 2591 | return mProcessStats; |
| 2592 | } |
| 2593 | |
| 2594 | @Override |
| 2595 | public Map<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() { |
| 2596 | return mPackageStats; |
| 2597 | } |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 2598 | |
| 2599 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2600 | public int getUid() { |
| 2601 | return mUid; |
| 2602 | } |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 2603 | |
| 2604 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2605 | public long getTcpBytesReceived(int which) { |
| 2606 | if (which == STATS_LAST) { |
| 2607 | return mLoadedTcpBytesReceived; |
| 2608 | } else { |
| 2609 | long current = computeCurrentTcpBytesReceived(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2610 | if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2611 | current -= mTcpBytesReceivedAtLastUnplug; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2612 | } else if (which == STATS_SINCE_CHARGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2613 | current += mLoadedTcpBytesReceived; |
| 2614 | } |
| 2615 | return current; |
| 2616 | } |
| 2617 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | public long computeCurrentTcpBytesReceived() { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2620 | final long uidRxBytes = getNetworkStatsDetailGroupedByUid().getTotal( |
| 2621 | null, mUid).rxBytes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2622 | return mCurrentTcpBytesReceived + (mStartedTcpBytesReceived >= 0 |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2623 | ? (uidRxBytes - mStartedTcpBytesReceived) : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2624 | } |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 2625 | |
| 2626 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2627 | public long getTcpBytesSent(int which) { |
| 2628 | if (which == STATS_LAST) { |
| 2629 | return mLoadedTcpBytesSent; |
| 2630 | } else { |
| 2631 | long current = computeCurrentTcpBytesSent(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2632 | if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2633 | current -= mTcpBytesSentAtLastUnplug; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2634 | } else if (which == STATS_SINCE_CHARGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2635 | current += mLoadedTcpBytesSent; |
| 2636 | } |
| 2637 | return current; |
| 2638 | } |
| 2639 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2640 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2641 | @Override |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2642 | public void noteWifiRunningLocked() { |
| 2643 | if (!mWifiRunning) { |
| 2644 | mWifiRunning = true; |
| 2645 | if (mWifiRunningTimer == null) { |
| 2646 | mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING, |
| 2647 | mWifiRunningTimers, mUnpluggables); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2648 | } |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2649 | mWifiRunningTimer.startRunningLocked(BatteryStatsImpl.this); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2650 | } |
| 2651 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2652 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2653 | @Override |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2654 | public void noteWifiStoppedLocked() { |
| 2655 | if (mWifiRunning) { |
| 2656 | mWifiRunning = false; |
| 2657 | mWifiRunningTimer.stopRunningLocked(BatteryStatsImpl.this); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2658 | } |
| 2659 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2660 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2661 | @Override |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2662 | public void noteFullWifiLockAcquiredLocked() { |
| 2663 | if (!mFullWifiLockOut) { |
| 2664 | mFullWifiLockOut = true; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2665 | if (mFullWifiLockTimer == null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 2666 | mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK, |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2667 | mFullWifiLockTimers, mUnpluggables); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2668 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2669 | mFullWifiLockTimer.startRunningLocked(BatteryStatsImpl.this); |
| 2670 | } |
| 2671 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2672 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2673 | @Override |
| 2674 | public void noteFullWifiLockReleasedLocked() { |
| 2675 | if (mFullWifiLockOut) { |
| 2676 | mFullWifiLockOut = false; |
| 2677 | mFullWifiLockTimer.stopRunningLocked(BatteryStatsImpl.this); |
| 2678 | } |
| 2679 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2680 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2681 | @Override |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2682 | public void noteWifiScanStartedLocked() { |
| 2683 | if (!mWifiScanStarted) { |
| 2684 | mWifiScanStarted = true; |
| 2685 | if (mWifiScanTimer == null) { |
| 2686 | mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN, |
| 2687 | mWifiScanTimers, mUnpluggables); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2688 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2689 | mWifiScanTimer.startRunningLocked(BatteryStatsImpl.this); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2690 | } |
| 2691 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2692 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2693 | @Override |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2694 | public void noteWifiScanStoppedLocked() { |
| 2695 | if (mWifiScanStarted) { |
| 2696 | mWifiScanStarted = false; |
| 2697 | mWifiScanTimer.stopRunningLocked(BatteryStatsImpl.this); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2698 | } |
| 2699 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2700 | |
| 2701 | @Override |
| 2702 | public void noteWifiMulticastEnabledLocked() { |
| 2703 | if (!mWifiMulticastEnabled) { |
| 2704 | mWifiMulticastEnabled = true; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2705 | if (mWifiMulticastTimer == null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 2706 | mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED, |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2707 | mWifiMulticastTimers, mUnpluggables); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2708 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2709 | mWifiMulticastTimer.startRunningLocked(BatteryStatsImpl.this); |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | @Override |
| 2714 | public void noteWifiMulticastDisabledLocked() { |
| 2715 | if (mWifiMulticastEnabled) { |
| 2716 | mWifiMulticastEnabled = false; |
| 2717 | mWifiMulticastTimer.stopRunningLocked(BatteryStatsImpl.this); |
| 2718 | } |
| 2719 | } |
| 2720 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2721 | public StopwatchTimer createAudioTurnedOnTimerLocked() { |
| 2722 | if (mAudioTurnedOnTimer == null) { |
| 2723 | mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON, |
| 2724 | null, mUnpluggables); |
| 2725 | } |
| 2726 | return mAudioTurnedOnTimer; |
| 2727 | } |
| 2728 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2729 | @Override |
| 2730 | public void noteAudioTurnedOnLocked() { |
| 2731 | if (!mAudioTurnedOn) { |
| 2732 | mAudioTurnedOn = true; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2733 | createAudioTurnedOnTimerLocked().startRunningLocked(BatteryStatsImpl.this); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | @Override |
| 2738 | public void noteAudioTurnedOffLocked() { |
| 2739 | if (mAudioTurnedOn) { |
| 2740 | mAudioTurnedOn = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2741 | if (mAudioTurnedOnTimer != null) { |
| 2742 | mAudioTurnedOnTimer.stopRunningLocked(BatteryStatsImpl.this); |
| 2743 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2744 | } |
| 2745 | } |
| 2746 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2747 | public StopwatchTimer createVideoTurnedOnTimerLocked() { |
| 2748 | if (mVideoTurnedOnTimer == null) { |
| 2749 | mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON, |
| 2750 | null, mUnpluggables); |
| 2751 | } |
| 2752 | return mVideoTurnedOnTimer; |
| 2753 | } |
| 2754 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2755 | @Override |
| 2756 | public void noteVideoTurnedOnLocked() { |
| 2757 | if (!mVideoTurnedOn) { |
| 2758 | mVideoTurnedOn = true; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2759 | createVideoTurnedOnTimerLocked().startRunningLocked(BatteryStatsImpl.this); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | @Override |
| 2764 | public void noteVideoTurnedOffLocked() { |
| 2765 | if (mVideoTurnedOn) { |
| 2766 | mVideoTurnedOn = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2767 | if (mVideoTurnedOnTimer != null) { |
| 2768 | mVideoTurnedOnTimer.stopRunningLocked(BatteryStatsImpl.this); |
| 2769 | } |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | public BatchTimer createVibratorOnTimerLocked() { |
| 2774 | if (mVibratorOnTimer == null) { |
| 2775 | mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, |
| 2776 | mUnpluggables, BatteryStatsImpl.this.mOnBatteryInternal); |
| 2777 | } |
| 2778 | return mVibratorOnTimer; |
| 2779 | } |
| 2780 | |
| 2781 | public void noteVibratorOnLocked(long durationMillis) { |
| 2782 | createVibratorOnTimerLocked().addDuration(BatteryStatsImpl.this, durationMillis); |
| 2783 | } |
| 2784 | |
| 2785 | public void noteVibratorOffLocked() { |
| 2786 | if (mVibratorOnTimer != null) { |
| 2787 | mVibratorOnTimer.abortLastDuration(BatteryStatsImpl.this); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2788 | } |
| 2789 | } |
| 2790 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2791 | @Override |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2792 | public long getWifiRunningTime(long batteryRealtime, int which) { |
| 2793 | if (mWifiRunningTimer == null) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2794 | return 0; |
| 2795 | } |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2796 | return mWifiRunningTimer.getTotalTimeLocked(batteryRealtime, which); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2797 | } |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2798 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2799 | @Override |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2800 | public long getFullWifiLockTime(long batteryRealtime, int which) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2801 | if (mFullWifiLockTimer == null) { |
| 2802 | return 0; |
| 2803 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2804 | return mFullWifiLockTimer.getTotalTimeLocked(batteryRealtime, which); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2805 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2806 | |
| 2807 | @Override |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2808 | public long getWifiScanTime(long batteryRealtime, int which) { |
| 2809 | if (mWifiScanTimer == null) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2810 | return 0; |
| 2811 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2812 | return mWifiScanTimer.getTotalTimeLocked(batteryRealtime, which); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2813 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2814 | |
| 2815 | @Override |
| 2816 | public long getWifiMulticastTime(long batteryRealtime, int which) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2817 | if (mWifiMulticastTimer == null) { |
| 2818 | return 0; |
| 2819 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2820 | return mWifiMulticastTimer.getTotalTimeLocked(batteryRealtime, |
| 2821 | which); |
| 2822 | } |
| 2823 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2824 | @Override |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2825 | public long getAudioTurnedOnTime(long batteryRealtime, int which) { |
| 2826 | if (mAudioTurnedOnTimer == null) { |
| 2827 | return 0; |
| 2828 | } |
| 2829 | return mAudioTurnedOnTimer.getTotalTimeLocked(batteryRealtime, which); |
| 2830 | } |
| 2831 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2832 | @Override |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2833 | public long getVideoTurnedOnTime(long batteryRealtime, int which) { |
| 2834 | if (mVideoTurnedOnTimer == null) { |
| 2835 | return 0; |
| 2836 | } |
| 2837 | return mVideoTurnedOnTimer.getTotalTimeLocked(batteryRealtime, which); |
| 2838 | } |
| 2839 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2840 | @Override |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2841 | public Timer getVibratorOnTimer() { |
| 2842 | return mVibratorOnTimer; |
| 2843 | } |
| 2844 | |
| 2845 | @Override |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2846 | public void noteUserActivityLocked(int type) { |
| 2847 | if (mUserActivityCounters == null) { |
| 2848 | initUserActivityLocked(); |
| 2849 | } |
Jeff Brown | df693de | 2012-07-27 12:03:38 -0700 | [diff] [blame] | 2850 | if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) { |
| 2851 | mUserActivityCounters[type].stepAtomic(); |
| 2852 | } else { |
| 2853 | Slog.w(TAG, "Unknown user activity type " + type + " was specified.", |
| 2854 | new Throwable()); |
| 2855 | } |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2856 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2857 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2858 | @Override |
| 2859 | public boolean hasUserActivity() { |
| 2860 | return mUserActivityCounters != null; |
| 2861 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2862 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2863 | @Override |
| 2864 | public int getUserActivityCount(int type, int which) { |
| 2865 | if (mUserActivityCounters == null) { |
| 2866 | return 0; |
| 2867 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 2868 | return mUserActivityCounters[type].getCountLocked(which); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2869 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2870 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2871 | void initUserActivityLocked() { |
| 2872 | mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES]; |
| 2873 | for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) { |
| 2874 | mUserActivityCounters[i] = new Counter(mUnpluggables); |
| 2875 | } |
| 2876 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2878 | public long computeCurrentTcpBytesSent() { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2879 | final long uidTxBytes = getNetworkStatsDetailGroupedByUid().getTotal( |
| 2880 | null, mUid).txBytes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2881 | return mCurrentTcpBytesSent + (mStartedTcpBytesSent >= 0 |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2882 | ? (uidTxBytes - mStartedTcpBytesSent) : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2883 | } |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 2884 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2885 | /** |
| 2886 | * Clear all stats for this uid. Returns true if the uid is completely |
| 2887 | * inactive so can be dropped. |
| 2888 | */ |
| 2889 | boolean reset() { |
| 2890 | boolean active = false; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2891 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2892 | if (mWifiRunningTimer != null) { |
| 2893 | active |= !mWifiRunningTimer.reset(BatteryStatsImpl.this, false); |
| 2894 | active |= mWifiRunning; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2895 | } |
| 2896 | if (mFullWifiLockTimer != null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2897 | active |= !mFullWifiLockTimer.reset(BatteryStatsImpl.this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2898 | active |= mFullWifiLockOut; |
| 2899 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 2900 | if (mWifiScanTimer != null) { |
| 2901 | active |= !mWifiScanTimer.reset(BatteryStatsImpl.this, false); |
| 2902 | active |= mWifiScanStarted; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2903 | } |
| 2904 | if (mWifiMulticastTimer != null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2905 | active |= !mWifiMulticastTimer.reset(BatteryStatsImpl.this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2906 | active |= mWifiMulticastEnabled; |
| 2907 | } |
| 2908 | if (mAudioTurnedOnTimer != null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2909 | active |= !mAudioTurnedOnTimer.reset(BatteryStatsImpl.this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2910 | active |= mAudioTurnedOn; |
| 2911 | } |
| 2912 | if (mVideoTurnedOnTimer != null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2913 | active |= !mVideoTurnedOnTimer.reset(BatteryStatsImpl.this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2914 | active |= mVideoTurnedOn; |
| 2915 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2916 | if (mVibratorOnTimer != null) { |
| 2917 | if (mVibratorOnTimer.reset(BatteryStatsImpl.this, false)) { |
| 2918 | mVibratorOnTimer.detach(); |
| 2919 | mVibratorOnTimer = null; |
| 2920 | } else { |
| 2921 | active = true; |
| 2922 | } |
| 2923 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2924 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2925 | mLoadedTcpBytesReceived = mLoadedTcpBytesSent = 0; |
| 2926 | mCurrentTcpBytesReceived = mCurrentTcpBytesSent = 0; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2927 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2928 | if (mUserActivityCounters != null) { |
| 2929 | for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) { |
| 2930 | mUserActivityCounters[i].reset(false); |
| 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | if (mWakelockStats.size() > 0) { |
| 2935 | Iterator<Map.Entry<String, Wakelock>> it = mWakelockStats.entrySet().iterator(); |
| 2936 | while (it.hasNext()) { |
| 2937 | Map.Entry<String, Wakelock> wakelockEntry = it.next(); |
| 2938 | Wakelock wl = wakelockEntry.getValue(); |
| 2939 | if (wl.reset()) { |
| 2940 | it.remove(); |
| 2941 | } else { |
| 2942 | active = true; |
| 2943 | } |
| 2944 | } |
| 2945 | } |
| 2946 | if (mSensorStats.size() > 0) { |
| 2947 | Iterator<Map.Entry<Integer, Sensor>> it = mSensorStats.entrySet().iterator(); |
| 2948 | while (it.hasNext()) { |
| 2949 | Map.Entry<Integer, Sensor> sensorEntry = it.next(); |
| 2950 | Sensor s = sensorEntry.getValue(); |
| 2951 | if (s.reset()) { |
| 2952 | it.remove(); |
| 2953 | } else { |
| 2954 | active = true; |
| 2955 | } |
| 2956 | } |
| 2957 | } |
| 2958 | if (mProcessStats.size() > 0) { |
| 2959 | Iterator<Map.Entry<String, Proc>> it = mProcessStats.entrySet().iterator(); |
| 2960 | while (it.hasNext()) { |
| 2961 | Map.Entry<String, Proc> procEntry = it.next(); |
| 2962 | procEntry.getValue().detach(); |
| 2963 | } |
| 2964 | mProcessStats.clear(); |
| 2965 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2966 | if (mPids.size() > 0) { |
| 2967 | for (int i=0; !active && i<mPids.size(); i++) { |
| 2968 | Pid pid = mPids.valueAt(i); |
| 2969 | if (pid.mWakeStart != 0) { |
| 2970 | active = true; |
| 2971 | } |
| 2972 | } |
| 2973 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2974 | if (mPackageStats.size() > 0) { |
| 2975 | Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator(); |
| 2976 | while (it.hasNext()) { |
| 2977 | Map.Entry<String, Pkg> pkgEntry = it.next(); |
| 2978 | Pkg p = pkgEntry.getValue(); |
| 2979 | p.detach(); |
| 2980 | if (p.mServiceStats.size() > 0) { |
| 2981 | Iterator<Map.Entry<String, Pkg.Serv>> it2 |
| 2982 | = p.mServiceStats.entrySet().iterator(); |
| 2983 | while (it2.hasNext()) { |
| 2984 | Map.Entry<String, Pkg.Serv> servEntry = it2.next(); |
| 2985 | servEntry.getValue().detach(); |
| 2986 | } |
| 2987 | } |
| 2988 | } |
| 2989 | mPackageStats.clear(); |
| 2990 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 2991 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2992 | mPids.clear(); |
| 2993 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2994 | if (!active) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 2995 | if (mWifiRunningTimer != null) { |
| 2996 | mWifiRunningTimer.detach(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 2997 | } |
| 2998 | if (mFullWifiLockTimer != null) { |
| 2999 | mFullWifiLockTimer.detach(); |
| 3000 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 3001 | if (mWifiScanTimer != null) { |
| 3002 | mWifiScanTimer.detach(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3003 | } |
| 3004 | if (mWifiMulticastTimer != null) { |
| 3005 | mWifiMulticastTimer.detach(); |
| 3006 | } |
| 3007 | if (mAudioTurnedOnTimer != null) { |
| 3008 | mAudioTurnedOnTimer.detach(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3009 | mAudioTurnedOnTimer = null; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3010 | } |
| 3011 | if (mVideoTurnedOnTimer != null) { |
| 3012 | mVideoTurnedOnTimer.detach(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3013 | mVideoTurnedOnTimer = null; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3014 | } |
| 3015 | if (mUserActivityCounters != null) { |
| 3016 | for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) { |
| 3017 | mUserActivityCounters[i].detach(); |
| 3018 | } |
| 3019 | } |
| 3020 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3021 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3022 | return !active; |
| 3023 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3024 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3025 | void writeToParcelLocked(Parcel out, long batteryRealtime) { |
| 3026 | out.writeInt(mWakelockStats.size()); |
| 3027 | for (Map.Entry<String, Uid.Wakelock> wakelockEntry : mWakelockStats.entrySet()) { |
| 3028 | out.writeString(wakelockEntry.getKey()); |
| 3029 | Uid.Wakelock wakelock = wakelockEntry.getValue(); |
| 3030 | wakelock.writeToParcelLocked(out, batteryRealtime); |
| 3031 | } |
| 3032 | |
| 3033 | out.writeInt(mSensorStats.size()); |
| 3034 | for (Map.Entry<Integer, Uid.Sensor> sensorEntry : mSensorStats.entrySet()) { |
| 3035 | out.writeInt(sensorEntry.getKey()); |
| 3036 | Uid.Sensor sensor = sensorEntry.getValue(); |
| 3037 | sensor.writeToParcelLocked(out, batteryRealtime); |
| 3038 | } |
| 3039 | |
| 3040 | out.writeInt(mProcessStats.size()); |
| 3041 | for (Map.Entry<String, Uid.Proc> procEntry : mProcessStats.entrySet()) { |
| 3042 | out.writeString(procEntry.getKey()); |
| 3043 | Uid.Proc proc = procEntry.getValue(); |
| 3044 | proc.writeToParcelLocked(out); |
| 3045 | } |
| 3046 | |
| 3047 | out.writeInt(mPackageStats.size()); |
| 3048 | for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) { |
| 3049 | out.writeString(pkgEntry.getKey()); |
| 3050 | Uid.Pkg pkg = pkgEntry.getValue(); |
| 3051 | pkg.writeToParcelLocked(out); |
| 3052 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3053 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3054 | out.writeLong(mLoadedTcpBytesReceived); |
| 3055 | out.writeLong(mLoadedTcpBytesSent); |
| 3056 | out.writeLong(computeCurrentTcpBytesReceived()); |
| 3057 | out.writeLong(computeCurrentTcpBytesSent()); |
| 3058 | out.writeLong(mTcpBytesReceivedAtLastUnplug); |
| 3059 | out.writeLong(mTcpBytesSentAtLastUnplug); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 3060 | if (mWifiRunningTimer != null) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3061 | out.writeInt(1); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 3062 | mWifiRunningTimer.writeToParcel(out, batteryRealtime); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 3063 | } else { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3064 | out.writeInt(0); |
| 3065 | } |
| 3066 | if (mFullWifiLockTimer != null) { |
| 3067 | out.writeInt(1); |
| 3068 | mFullWifiLockTimer.writeToParcel(out, batteryRealtime); |
| 3069 | } else { |
| 3070 | out.writeInt(0); |
| 3071 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 3072 | if (mWifiScanTimer != null) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3073 | out.writeInt(1); |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 3074 | mWifiScanTimer.writeToParcel(out, batteryRealtime); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3075 | } else { |
| 3076 | out.writeInt(0); |
| 3077 | } |
| 3078 | if (mWifiMulticastTimer != null) { |
| 3079 | out.writeInt(1); |
| 3080 | mWifiMulticastTimer.writeToParcel(out, batteryRealtime); |
| 3081 | } else { |
| 3082 | out.writeInt(0); |
| 3083 | } |
| 3084 | if (mAudioTurnedOnTimer != null) { |
| 3085 | out.writeInt(1); |
| 3086 | mAudioTurnedOnTimer.writeToParcel(out, batteryRealtime); |
| 3087 | } else { |
| 3088 | out.writeInt(0); |
| 3089 | } |
| 3090 | if (mVideoTurnedOnTimer != null) { |
| 3091 | out.writeInt(1); |
| 3092 | mVideoTurnedOnTimer.writeToParcel(out, batteryRealtime); |
| 3093 | } else { |
| 3094 | out.writeInt(0); |
| 3095 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3096 | if (mVibratorOnTimer != null) { |
| 3097 | out.writeInt(1); |
| 3098 | mVibratorOnTimer.writeToParcel(out, batteryRealtime); |
| 3099 | } else { |
| 3100 | out.writeInt(0); |
| 3101 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3102 | if (mUserActivityCounters != null) { |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 3103 | out.writeInt(1); |
| 3104 | for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) { |
| 3105 | mUserActivityCounters[i].writeToParcel(out); |
| 3106 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3107 | } else { |
| 3108 | out.writeInt(0); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 3109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | void readFromParcelLocked(ArrayList<Unpluggable> unpluggables, Parcel in) { |
| 3113 | int numWakelocks = in.readInt(); |
| 3114 | mWakelockStats.clear(); |
| 3115 | for (int j = 0; j < numWakelocks; j++) { |
| 3116 | String wakelockName = in.readString(); |
| 3117 | Uid.Wakelock wakelock = new Wakelock(); |
| 3118 | wakelock.readFromParcelLocked(unpluggables, in); |
Dianne Hackborn | c24ab86 | 2011-10-18 15:55:03 -0700 | [diff] [blame] | 3119 | // We will just drop some random set of wakelocks if |
| 3120 | // the previous run of the system was an older version |
| 3121 | // that didn't impose a limit. |
| 3122 | mWakelockStats.put(wakelockName, wakelock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | int numSensors = in.readInt(); |
| 3126 | mSensorStats.clear(); |
| 3127 | for (int k = 0; k < numSensors; k++) { |
| 3128 | int sensorNumber = in.readInt(); |
| 3129 | Uid.Sensor sensor = new Sensor(sensorNumber); |
| 3130 | sensor.readFromParcelLocked(mUnpluggables, in); |
| 3131 | mSensorStats.put(sensorNumber, sensor); |
| 3132 | } |
| 3133 | |
| 3134 | int numProcs = in.readInt(); |
| 3135 | mProcessStats.clear(); |
| 3136 | for (int k = 0; k < numProcs; k++) { |
| 3137 | String processName = in.readString(); |
| 3138 | Uid.Proc proc = new Proc(); |
| 3139 | proc.readFromParcelLocked(in); |
| 3140 | mProcessStats.put(processName, proc); |
| 3141 | } |
| 3142 | |
| 3143 | int numPkgs = in.readInt(); |
| 3144 | mPackageStats.clear(); |
| 3145 | for (int l = 0; l < numPkgs; l++) { |
| 3146 | String packageName = in.readString(); |
| 3147 | Uid.Pkg pkg = new Pkg(); |
| 3148 | pkg.readFromParcelLocked(in); |
| 3149 | mPackageStats.put(packageName, pkg); |
| 3150 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3152 | mLoadedTcpBytesReceived = in.readLong(); |
| 3153 | mLoadedTcpBytesSent = in.readLong(); |
| 3154 | mCurrentTcpBytesReceived = in.readLong(); |
| 3155 | mCurrentTcpBytesSent = in.readLong(); |
| 3156 | mTcpBytesReceivedAtLastUnplug = in.readLong(); |
| 3157 | mTcpBytesSentAtLastUnplug = in.readLong(); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 3158 | mWifiRunning = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3159 | if (in.readInt() != 0) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 3160 | mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING, |
| 3161 | mWifiRunningTimers, mUnpluggables, in); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 3162 | } else { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 3163 | mWifiRunningTimer = null; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3164 | } |
| 3165 | mFullWifiLockOut = false; |
| 3166 | if (in.readInt() != 0) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 3167 | mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK, |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 3168 | mFullWifiLockTimers, mUnpluggables, in); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3169 | } else { |
| 3170 | mFullWifiLockTimer = null; |
| 3171 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 3172 | mWifiScanStarted = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3173 | if (in.readInt() != 0) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 3174 | mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN, |
| 3175 | mWifiScanTimers, mUnpluggables, in); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3176 | } else { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 3177 | mWifiScanTimer = null; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3178 | } |
| 3179 | mWifiMulticastEnabled = false; |
| 3180 | if (in.readInt() != 0) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 3181 | mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED, |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 3182 | mWifiMulticastTimers, mUnpluggables, in); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3183 | } else { |
| 3184 | mWifiMulticastTimer = null; |
| 3185 | } |
| 3186 | mAudioTurnedOn = false; |
| 3187 | if (in.readInt() != 0) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 3188 | mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON, |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3189 | null, mUnpluggables, in); |
| 3190 | } else { |
| 3191 | mAudioTurnedOnTimer = null; |
| 3192 | } |
| 3193 | mVideoTurnedOn = false; |
| 3194 | if (in.readInt() != 0) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 3195 | mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON, |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3196 | null, mUnpluggables, in); |
| 3197 | } else { |
| 3198 | mVideoTurnedOnTimer = null; |
| 3199 | } |
| 3200 | if (in.readInt() != 0) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3201 | mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, |
| 3202 | mUnpluggables, BatteryStatsImpl.this.mOnBatteryInternal, in); |
| 3203 | } else { |
| 3204 | mVibratorOnTimer = null; |
| 3205 | } |
| 3206 | if (in.readInt() != 0) { |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 3207 | mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES]; |
| 3208 | for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) { |
| 3209 | mUserActivityCounters[i] = new Counter(mUnpluggables, in); |
| 3210 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3211 | } else { |
| 3212 | mUserActivityCounters = null; |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 3213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | /** |
| 3217 | * The statistics associated with a particular wake lock. |
| 3218 | */ |
| 3219 | public final class Wakelock extends BatteryStats.Uid.Wakelock { |
| 3220 | /** |
| 3221 | * How long (in ms) this uid has been keeping the device partially awake. |
| 3222 | */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3223 | StopwatchTimer mTimerPartial; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3224 | |
| 3225 | /** |
| 3226 | * How long (in ms) this uid has been keeping the device fully awake. |
| 3227 | */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3228 | StopwatchTimer mTimerFull; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3229 | |
| 3230 | /** |
| 3231 | * How long (in ms) this uid has had a window keeping the device awake. |
| 3232 | */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3233 | StopwatchTimer mTimerWindow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3234 | |
| 3235 | /** |
| 3236 | * Reads a possibly null Timer from a Parcel. The timer is associated with the |
| 3237 | * proper timer pool from the given BatteryStatsImpl object. |
| 3238 | * |
| 3239 | * @param in the Parcel to be read from. |
| 3240 | * return a new Timer, or null. |
| 3241 | */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3242 | private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3243 | ArrayList<Unpluggable> unpluggables, Parcel in) { |
| 3244 | if (in.readInt() == 0) { |
| 3245 | return null; |
| 3246 | } |
| 3247 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 3248 | return new StopwatchTimer(Uid.this, type, pool, unpluggables, in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3249 | } |
| 3250 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3251 | boolean reset() { |
| 3252 | boolean wlactive = false; |
| 3253 | if (mTimerFull != null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3254 | wlactive |= !mTimerFull.reset(BatteryStatsImpl.this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3255 | } |
| 3256 | if (mTimerPartial != null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3257 | wlactive |= !mTimerPartial.reset(BatteryStatsImpl.this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3258 | } |
| 3259 | if (mTimerWindow != null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3260 | wlactive |= !mTimerWindow.reset(BatteryStatsImpl.this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3261 | } |
| 3262 | if (!wlactive) { |
| 3263 | if (mTimerFull != null) { |
| 3264 | mTimerFull.detach(); |
| 3265 | mTimerFull = null; |
| 3266 | } |
| 3267 | if (mTimerPartial != null) { |
| 3268 | mTimerPartial.detach(); |
| 3269 | mTimerPartial = null; |
| 3270 | } |
| 3271 | if (mTimerWindow != null) { |
| 3272 | mTimerWindow.detach(); |
| 3273 | mTimerWindow = null; |
| 3274 | } |
| 3275 | } |
| 3276 | return !wlactive; |
| 3277 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3279 | void readFromParcelLocked(ArrayList<Unpluggable> unpluggables, Parcel in) { |
| 3280 | mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL, |
| 3281 | mPartialTimers, unpluggables, in); |
| 3282 | mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, |
| 3283 | mFullTimers, unpluggables, in); |
| 3284 | mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, |
| 3285 | mWindowTimers, unpluggables, in); |
| 3286 | } |
| 3287 | |
| 3288 | void writeToParcelLocked(Parcel out, long batteryRealtime) { |
| 3289 | Timer.writeTimerToParcel(out, mTimerPartial, batteryRealtime); |
| 3290 | Timer.writeTimerToParcel(out, mTimerFull, batteryRealtime); |
| 3291 | Timer.writeTimerToParcel(out, mTimerWindow, batteryRealtime); |
| 3292 | } |
| 3293 | |
| 3294 | @Override |
| 3295 | public Timer getWakeTime(int type) { |
| 3296 | switch (type) { |
| 3297 | case WAKE_TYPE_FULL: return mTimerFull; |
| 3298 | case WAKE_TYPE_PARTIAL: return mTimerPartial; |
| 3299 | case WAKE_TYPE_WINDOW: return mTimerWindow; |
| 3300 | default: throw new IllegalArgumentException("type = " + type); |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | public final class Sensor extends BatteryStats.Uid.Sensor { |
| 3306 | final int mHandle; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3307 | StopwatchTimer mTimer; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3309 | public Sensor(int handle) { |
| 3310 | mHandle = handle; |
| 3311 | } |
| 3312 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3313 | private StopwatchTimer readTimerFromParcel(ArrayList<Unpluggable> unpluggables, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3314 | Parcel in) { |
| 3315 | if (in.readInt() == 0) { |
| 3316 | return null; |
| 3317 | } |
| 3318 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3319 | ArrayList<StopwatchTimer> pool = mSensorTimers.get(mHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3320 | if (pool == null) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 3321 | pool = new ArrayList<StopwatchTimer>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3322 | mSensorTimers.put(mHandle, pool); |
| 3323 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 3324 | return new StopwatchTimer(Uid.this, 0, pool, unpluggables, in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3325 | } |
| 3326 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3327 | boolean reset() { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3328 | if (mTimer.reset(BatteryStatsImpl.this, true)) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3329 | mTimer = null; |
| 3330 | return true; |
| 3331 | } |
| 3332 | return false; |
| 3333 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3335 | void readFromParcelLocked(ArrayList<Unpluggable> unpluggables, Parcel in) { |
| 3336 | mTimer = readTimerFromParcel(unpluggables, in); |
| 3337 | } |
| 3338 | |
| 3339 | void writeToParcelLocked(Parcel out, long batteryRealtime) { |
| 3340 | Timer.writeTimerToParcel(out, mTimer, batteryRealtime); |
| 3341 | } |
| 3342 | |
| 3343 | @Override |
| 3344 | public Timer getSensorTime() { |
| 3345 | return mTimer; |
| 3346 | } |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3347 | |
| 3348 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3349 | public int getHandle() { |
| 3350 | return mHandle; |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | /** |
| 3355 | * The statistics associated with a particular process. |
| 3356 | */ |
| 3357 | public final class Proc extends BatteryStats.Uid.Proc implements Unpluggable { |
| 3358 | /** |
| 3359 | * Total time (in 1/100 sec) spent executing in user code. |
| 3360 | */ |
| 3361 | long mUserTime; |
| 3362 | |
| 3363 | /** |
| 3364 | * Total time (in 1/100 sec) spent executing in kernel code. |
| 3365 | */ |
| 3366 | long mSystemTime; |
| 3367 | |
| 3368 | /** |
| 3369 | * Number of times the process has been started. |
| 3370 | */ |
| 3371 | int mStarts; |
| 3372 | |
| 3373 | /** |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3374 | * Amount of time the process was running in the foreground. |
| 3375 | */ |
| 3376 | long mForegroundTime; |
| 3377 | |
| 3378 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3379 | * The amount of user time loaded from a previous save. |
| 3380 | */ |
| 3381 | long mLoadedUserTime; |
| 3382 | |
| 3383 | /** |
| 3384 | * The amount of system time loaded from a previous save. |
| 3385 | */ |
| 3386 | long mLoadedSystemTime; |
| 3387 | |
| 3388 | /** |
| 3389 | * The number of times the process has started from a previous save. |
| 3390 | */ |
| 3391 | int mLoadedStarts; |
| 3392 | |
| 3393 | /** |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3394 | * The amount of foreground time loaded from a previous save. |
| 3395 | */ |
| 3396 | long mLoadedForegroundTime; |
| 3397 | |
| 3398 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3399 | * The amount of user time loaded from the previous run. |
| 3400 | */ |
| 3401 | long mLastUserTime; |
| 3402 | |
| 3403 | /** |
| 3404 | * The amount of system time loaded from the previous run. |
| 3405 | */ |
| 3406 | long mLastSystemTime; |
| 3407 | |
| 3408 | /** |
| 3409 | * The number of times the process has started from the previous run. |
| 3410 | */ |
| 3411 | int mLastStarts; |
| 3412 | |
| 3413 | /** |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3414 | * The amount of foreground time loaded from the previous run |
| 3415 | */ |
| 3416 | long mLastForegroundTime; |
| 3417 | |
| 3418 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3419 | * The amount of user time when last unplugged. |
| 3420 | */ |
| 3421 | long mUnpluggedUserTime; |
| 3422 | |
| 3423 | /** |
| 3424 | * The amount of system time when last unplugged. |
| 3425 | */ |
| 3426 | long mUnpluggedSystemTime; |
| 3427 | |
| 3428 | /** |
| 3429 | * The number of times the process has started before unplugged. |
| 3430 | */ |
| 3431 | int mUnpluggedStarts; |
| 3432 | |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3433 | /** |
| 3434 | * The amount of foreground time since unplugged. |
| 3435 | */ |
| 3436 | long mUnpluggedForegroundTime; |
| 3437 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3438 | SamplingCounter[] mSpeedBins; |
| 3439 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3440 | ArrayList<ExcessivePower> mExcessivePower; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3442 | Proc() { |
| 3443 | mUnpluggables.add(this); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3444 | mSpeedBins = new SamplingCounter[getCpuSpeedSteps()]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3445 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3446 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3447 | public void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3448 | mUnpluggedUserTime = mUserTime; |
| 3449 | mUnpluggedSystemTime = mSystemTime; |
| 3450 | mUnpluggedStarts = mStarts; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3451 | mUnpluggedForegroundTime = mForegroundTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3452 | } |
| 3453 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3454 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3455 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3456 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3457 | void detach() { |
| 3458 | mUnpluggables.remove(this); |
| 3459 | for (int i = 0; i < mSpeedBins.length; i++) { |
| 3460 | SamplingCounter c = mSpeedBins[i]; |
| 3461 | if (c != null) { |
| 3462 | mUnpluggables.remove(c); |
| 3463 | mSpeedBins[i] = null; |
| 3464 | } |
| 3465 | } |
| 3466 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3467 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3468 | public int countExcessivePowers() { |
| 3469 | return mExcessivePower != null ? mExcessivePower.size() : 0; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3472 | public ExcessivePower getExcessivePower(int i) { |
| 3473 | if (mExcessivePower != null) { |
| 3474 | return mExcessivePower.get(i); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3475 | } |
| 3476 | return null; |
| 3477 | } |
| 3478 | |
| 3479 | public void addExcessiveWake(long overTime, long usedTime) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3480 | if (mExcessivePower == null) { |
| 3481 | mExcessivePower = new ArrayList<ExcessivePower>(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3482 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3483 | ExcessivePower ew = new ExcessivePower(); |
| 3484 | ew.type = ExcessivePower.TYPE_WAKE; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3485 | ew.overTime = overTime; |
| 3486 | ew.usedTime = usedTime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3487 | mExcessivePower.add(ew); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3488 | } |
| 3489 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3490 | public void addExcessiveCpu(long overTime, long usedTime) { |
| 3491 | if (mExcessivePower == null) { |
| 3492 | mExcessivePower = new ArrayList<ExcessivePower>(); |
| 3493 | } |
| 3494 | ExcessivePower ew = new ExcessivePower(); |
| 3495 | ew.type = ExcessivePower.TYPE_CPU; |
| 3496 | ew.overTime = overTime; |
| 3497 | ew.usedTime = usedTime; |
| 3498 | mExcessivePower.add(ew); |
| 3499 | } |
| 3500 | |
| 3501 | void writeExcessivePowerToParcelLocked(Parcel out) { |
| 3502 | if (mExcessivePower == null) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3503 | out.writeInt(0); |
| 3504 | return; |
| 3505 | } |
| 3506 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3507 | final int N = mExcessivePower.size(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3508 | out.writeInt(N); |
| 3509 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3510 | ExcessivePower ew = mExcessivePower.get(i); |
| 3511 | out.writeInt(ew.type); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3512 | out.writeLong(ew.overTime); |
| 3513 | out.writeLong(ew.usedTime); |
| 3514 | } |
| 3515 | } |
| 3516 | |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 3517 | boolean readExcessivePowerFromParcelLocked(Parcel in) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3518 | final int N = in.readInt(); |
| 3519 | if (N == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3520 | mExcessivePower = null; |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 3521 | return true; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 3524 | if (N > 10000) { |
| 3525 | Slog.w(TAG, "File corrupt: too many excessive power entries " + N); |
| 3526 | return false; |
| 3527 | } |
| 3528 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3529 | mExcessivePower = new ArrayList<ExcessivePower>(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3530 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3531 | ExcessivePower ew = new ExcessivePower(); |
| 3532 | ew.type = in.readInt(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3533 | ew.overTime = in.readLong(); |
| 3534 | ew.usedTime = in.readLong(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3535 | mExcessivePower.add(ew); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3536 | } |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 3537 | return true; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3538 | } |
| 3539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3540 | void writeToParcelLocked(Parcel out) { |
| 3541 | out.writeLong(mUserTime); |
| 3542 | out.writeLong(mSystemTime); |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3543 | out.writeLong(mForegroundTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3544 | out.writeInt(mStarts); |
| 3545 | out.writeLong(mLoadedUserTime); |
| 3546 | out.writeLong(mLoadedSystemTime); |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3547 | out.writeLong(mLoadedForegroundTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3548 | out.writeInt(mLoadedStarts); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3549 | out.writeLong(mUnpluggedUserTime); |
| 3550 | out.writeLong(mUnpluggedSystemTime); |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3551 | out.writeLong(mUnpluggedForegroundTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3552 | out.writeInt(mUnpluggedStarts); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3553 | |
| 3554 | out.writeInt(mSpeedBins.length); |
| 3555 | for (int i = 0; i < mSpeedBins.length; i++) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3556 | SamplingCounter c = mSpeedBins[i]; |
| 3557 | if (c != null) { |
| 3558 | out.writeInt(1); |
| 3559 | c.writeToParcel(out); |
| 3560 | } else { |
| 3561 | out.writeInt(0); |
| 3562 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3563 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3564 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3565 | writeExcessivePowerToParcelLocked(out); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | void readFromParcelLocked(Parcel in) { |
| 3569 | mUserTime = in.readLong(); |
| 3570 | mSystemTime = in.readLong(); |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3571 | mForegroundTime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3572 | mStarts = in.readInt(); |
| 3573 | mLoadedUserTime = in.readLong(); |
| 3574 | mLoadedSystemTime = in.readLong(); |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3575 | mLoadedForegroundTime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3576 | mLoadedStarts = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 3577 | mLastUserTime = 0; |
| 3578 | mLastSystemTime = 0; |
| 3579 | mLastForegroundTime = 0; |
| 3580 | mLastStarts = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3581 | mUnpluggedUserTime = in.readLong(); |
| 3582 | mUnpluggedSystemTime = in.readLong(); |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3583 | mUnpluggedForegroundTime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3584 | mUnpluggedStarts = in.readInt(); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3585 | |
| 3586 | int bins = in.readInt(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3587 | int steps = getCpuSpeedSteps(); |
| 3588 | mSpeedBins = new SamplingCounter[bins >= steps ? bins : steps]; |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3589 | for (int i = 0; i < bins; i++) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3590 | if (in.readInt() != 0) { |
| 3591 | mSpeedBins[i] = new SamplingCounter(mUnpluggables, in); |
| 3592 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3593 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3594 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3595 | readExcessivePowerFromParcelLocked(in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | public BatteryStatsImpl getBatteryStats() { |
| 3599 | return BatteryStatsImpl.this; |
| 3600 | } |
| 3601 | |
| 3602 | public void addCpuTimeLocked(int utime, int stime) { |
| 3603 | mUserTime += utime; |
| 3604 | mSystemTime += stime; |
| 3605 | } |
| 3606 | |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3607 | public void addForegroundTimeLocked(long ttime) { |
| 3608 | mForegroundTime += ttime; |
| 3609 | } |
| 3610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3611 | public void incStartsLocked() { |
| 3612 | mStarts++; |
| 3613 | } |
| 3614 | |
| 3615 | @Override |
| 3616 | public long getUserTime(int which) { |
| 3617 | long val; |
| 3618 | if (which == STATS_LAST) { |
| 3619 | val = mLastUserTime; |
| 3620 | } else { |
| 3621 | val = mUserTime; |
| 3622 | if (which == STATS_CURRENT) { |
| 3623 | val -= mLoadedUserTime; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3624 | } else if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3625 | val -= mUnpluggedUserTime; |
| 3626 | } |
| 3627 | } |
| 3628 | return val; |
| 3629 | } |
| 3630 | |
| 3631 | @Override |
| 3632 | public long getSystemTime(int which) { |
| 3633 | long val; |
| 3634 | if (which == STATS_LAST) { |
| 3635 | val = mLastSystemTime; |
| 3636 | } else { |
| 3637 | val = mSystemTime; |
| 3638 | if (which == STATS_CURRENT) { |
| 3639 | val -= mLoadedSystemTime; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3640 | } else if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3641 | val -= mUnpluggedSystemTime; |
| 3642 | } |
| 3643 | } |
| 3644 | return val; |
| 3645 | } |
| 3646 | |
| 3647 | @Override |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3648 | public long getForegroundTime(int which) { |
| 3649 | long val; |
| 3650 | if (which == STATS_LAST) { |
| 3651 | val = mLastForegroundTime; |
| 3652 | } else { |
| 3653 | val = mForegroundTime; |
| 3654 | if (which == STATS_CURRENT) { |
| 3655 | val -= mLoadedForegroundTime; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3656 | } else if (which == STATS_SINCE_UNPLUGGED) { |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 3657 | val -= mUnpluggedForegroundTime; |
| 3658 | } |
| 3659 | } |
| 3660 | return val; |
| 3661 | } |
| 3662 | |
| 3663 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3664 | public int getStarts(int which) { |
| 3665 | int val; |
| 3666 | if (which == STATS_LAST) { |
| 3667 | val = mLastStarts; |
| 3668 | } else { |
| 3669 | val = mStarts; |
| 3670 | if (which == STATS_CURRENT) { |
| 3671 | val -= mLoadedStarts; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3672 | } else if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3673 | val -= mUnpluggedStarts; |
| 3674 | } |
| 3675 | } |
| 3676 | return val; |
| 3677 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3678 | |
| 3679 | /* Called by ActivityManagerService when CPU times are updated. */ |
| 3680 | public void addSpeedStepTimes(long[] values) { |
| 3681 | for (int i = 0; i < mSpeedBins.length && i < values.length; i++) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3682 | long amt = values[i]; |
| 3683 | if (amt != 0) { |
| 3684 | SamplingCounter c = mSpeedBins[i]; |
| 3685 | if (c == null) { |
| 3686 | mSpeedBins[i] = c = new SamplingCounter(mUnpluggables); |
| 3687 | } |
| 3688 | c.addCountAtomic(values[i]); |
| 3689 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | @Override |
| 3694 | public long getTimeAtCpuSpeedStep(int speedStep, int which) { |
| 3695 | if (speedStep < mSpeedBins.length) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3696 | SamplingCounter c = mSpeedBins[speedStep]; |
| 3697 | return c != null ? c.getCountLocked(which) : 0; |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 3698 | } else { |
| 3699 | return 0; |
| 3700 | } |
| 3701 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /** |
| 3705 | * The statistics associated with a particular package. |
| 3706 | */ |
| 3707 | public final class Pkg extends BatteryStats.Uid.Pkg implements Unpluggable { |
| 3708 | /** |
| 3709 | * Number of times this package has done something that could wake up the |
| 3710 | * device from sleep. |
| 3711 | */ |
| 3712 | int mWakeups; |
| 3713 | |
| 3714 | /** |
| 3715 | * Number of things that could wake up the device loaded from a |
| 3716 | * previous save. |
| 3717 | */ |
| 3718 | int mLoadedWakeups; |
| 3719 | |
| 3720 | /** |
| 3721 | * Number of things that could wake up the device as of the |
| 3722 | * last run. |
| 3723 | */ |
| 3724 | int mLastWakeups; |
| 3725 | |
| 3726 | /** |
| 3727 | * Number of things that could wake up the device as of the |
| 3728 | * last run. |
| 3729 | */ |
| 3730 | int mUnpluggedWakeups; |
| 3731 | |
| 3732 | /** |
| 3733 | * The statics we have collected for this package's services. |
| 3734 | */ |
| 3735 | final HashMap<String, Serv> mServiceStats = new HashMap<String, Serv>(); |
| 3736 | |
| 3737 | Pkg() { |
| 3738 | mUnpluggables.add(this); |
| 3739 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3740 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3741 | public void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3742 | mUnpluggedWakeups = mWakeups; |
| 3743 | } |
| 3744 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3745 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3746 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3747 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3748 | void detach() { |
| 3749 | mUnpluggables.remove(this); |
| 3750 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3752 | void readFromParcelLocked(Parcel in) { |
| 3753 | mWakeups = in.readInt(); |
| 3754 | mLoadedWakeups = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 3755 | mLastWakeups = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3756 | mUnpluggedWakeups = in.readInt(); |
| 3757 | |
| 3758 | int numServs = in.readInt(); |
| 3759 | mServiceStats.clear(); |
| 3760 | for (int m = 0; m < numServs; m++) { |
| 3761 | String serviceName = in.readString(); |
| 3762 | Uid.Pkg.Serv serv = new Serv(); |
| 3763 | mServiceStats.put(serviceName, serv); |
| 3764 | |
| 3765 | serv.readFromParcelLocked(in); |
| 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | void writeToParcelLocked(Parcel out) { |
| 3770 | out.writeInt(mWakeups); |
| 3771 | out.writeInt(mLoadedWakeups); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3772 | out.writeInt(mUnpluggedWakeups); |
| 3773 | |
| 3774 | out.writeInt(mServiceStats.size()); |
| 3775 | for (Map.Entry<String, Uid.Pkg.Serv> servEntry : mServiceStats.entrySet()) { |
| 3776 | out.writeString(servEntry.getKey()); |
| 3777 | Uid.Pkg.Serv serv = servEntry.getValue(); |
| 3778 | |
| 3779 | serv.writeToParcelLocked(out); |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | @Override |
| 3784 | public Map<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() { |
| 3785 | return mServiceStats; |
| 3786 | } |
| 3787 | |
| 3788 | @Override |
| 3789 | public int getWakeups(int which) { |
| 3790 | int val; |
| 3791 | if (which == STATS_LAST) { |
| 3792 | val = mLastWakeups; |
| 3793 | } else { |
| 3794 | val = mWakeups; |
| 3795 | if (which == STATS_CURRENT) { |
| 3796 | val -= mLoadedWakeups; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3797 | } else if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3798 | val -= mUnpluggedWakeups; |
| 3799 | } |
| 3800 | } |
| 3801 | |
| 3802 | return val; |
| 3803 | } |
| 3804 | |
| 3805 | /** |
| 3806 | * The statistics associated with a particular service. |
| 3807 | */ |
| 3808 | public final class Serv extends BatteryStats.Uid.Pkg.Serv implements Unpluggable { |
| 3809 | /** |
| 3810 | * Total time (ms in battery uptime) the service has been left started. |
| 3811 | */ |
| 3812 | long mStartTime; |
| 3813 | |
| 3814 | /** |
| 3815 | * If service has been started and not yet stopped, this is |
| 3816 | * when it was started. |
| 3817 | */ |
| 3818 | long mRunningSince; |
| 3819 | |
| 3820 | /** |
| 3821 | * True if we are currently running. |
| 3822 | */ |
| 3823 | boolean mRunning; |
| 3824 | |
| 3825 | /** |
| 3826 | * Total number of times startService() has been called. |
| 3827 | */ |
| 3828 | int mStarts; |
| 3829 | |
| 3830 | /** |
| 3831 | * Total time (ms in battery uptime) the service has been left launched. |
| 3832 | */ |
| 3833 | long mLaunchedTime; |
| 3834 | |
| 3835 | /** |
| 3836 | * If service has been launched and not yet exited, this is |
| 3837 | * when it was launched (ms in battery uptime). |
| 3838 | */ |
| 3839 | long mLaunchedSince; |
| 3840 | |
| 3841 | /** |
| 3842 | * True if we are currently launched. |
| 3843 | */ |
| 3844 | boolean mLaunched; |
| 3845 | |
| 3846 | /** |
| 3847 | * Total number times the service has been launched. |
| 3848 | */ |
| 3849 | int mLaunches; |
| 3850 | |
| 3851 | /** |
| 3852 | * The amount of time spent started loaded from a previous save |
| 3853 | * (ms in battery uptime). |
| 3854 | */ |
| 3855 | long mLoadedStartTime; |
| 3856 | |
| 3857 | /** |
| 3858 | * The number of starts loaded from a previous save. |
| 3859 | */ |
| 3860 | int mLoadedStarts; |
| 3861 | |
| 3862 | /** |
| 3863 | * The number of launches loaded from a previous save. |
| 3864 | */ |
| 3865 | int mLoadedLaunches; |
| 3866 | |
| 3867 | /** |
| 3868 | * The amount of time spent started as of the last run (ms |
| 3869 | * in battery uptime). |
| 3870 | */ |
| 3871 | long mLastStartTime; |
| 3872 | |
| 3873 | /** |
| 3874 | * The number of starts as of the last run. |
| 3875 | */ |
| 3876 | int mLastStarts; |
| 3877 | |
| 3878 | /** |
| 3879 | * The number of launches as of the last run. |
| 3880 | */ |
| 3881 | int mLastLaunches; |
| 3882 | |
| 3883 | /** |
| 3884 | * The amount of time spent started when last unplugged (ms |
| 3885 | * in battery uptime). |
| 3886 | */ |
| 3887 | long mUnpluggedStartTime; |
| 3888 | |
| 3889 | /** |
| 3890 | * The number of starts when last unplugged. |
| 3891 | */ |
| 3892 | int mUnpluggedStarts; |
| 3893 | |
| 3894 | /** |
| 3895 | * The number of launches when last unplugged. |
| 3896 | */ |
| 3897 | int mUnpluggedLaunches; |
| 3898 | |
| 3899 | Serv() { |
| 3900 | mUnpluggables.add(this); |
| 3901 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3902 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3903 | public void unplug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3904 | mUnpluggedStartTime = getStartTimeToNowLocked(batteryUptime); |
| 3905 | mUnpluggedStarts = mStarts; |
| 3906 | mUnpluggedLaunches = mLaunches; |
| 3907 | } |
| 3908 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3909 | public void plug(long elapsedRealtime, long batteryUptime, long batteryRealtime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3910 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3911 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 3912 | void detach() { |
| 3913 | mUnpluggables.remove(this); |
| 3914 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 3915 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3916 | void readFromParcelLocked(Parcel in) { |
| 3917 | mStartTime = in.readLong(); |
| 3918 | mRunningSince = in.readLong(); |
| 3919 | mRunning = in.readInt() != 0; |
| 3920 | mStarts = in.readInt(); |
| 3921 | mLaunchedTime = in.readLong(); |
| 3922 | mLaunchedSince = in.readLong(); |
| 3923 | mLaunched = in.readInt() != 0; |
| 3924 | mLaunches = in.readInt(); |
| 3925 | mLoadedStartTime = in.readLong(); |
| 3926 | mLoadedStarts = in.readInt(); |
| 3927 | mLoadedLaunches = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 3928 | mLastStartTime = 0; |
| 3929 | mLastStarts = 0; |
| 3930 | mLastLaunches = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3931 | mUnpluggedStartTime = in.readLong(); |
| 3932 | mUnpluggedStarts = in.readInt(); |
| 3933 | mUnpluggedLaunches = in.readInt(); |
| 3934 | } |
| 3935 | |
| 3936 | void writeToParcelLocked(Parcel out) { |
| 3937 | out.writeLong(mStartTime); |
| 3938 | out.writeLong(mRunningSince); |
| 3939 | out.writeInt(mRunning ? 1 : 0); |
| 3940 | out.writeInt(mStarts); |
| 3941 | out.writeLong(mLaunchedTime); |
| 3942 | out.writeLong(mLaunchedSince); |
| 3943 | out.writeInt(mLaunched ? 1 : 0); |
| 3944 | out.writeInt(mLaunches); |
| 3945 | out.writeLong(mLoadedStartTime); |
| 3946 | out.writeInt(mLoadedStarts); |
| 3947 | out.writeInt(mLoadedLaunches); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3948 | out.writeLong(mUnpluggedStartTime); |
| 3949 | out.writeInt(mUnpluggedStarts); |
| 3950 | out.writeInt(mUnpluggedLaunches); |
| 3951 | } |
| 3952 | |
| 3953 | long getLaunchTimeToNowLocked(long batteryUptime) { |
| 3954 | if (!mLaunched) return mLaunchedTime; |
| 3955 | return mLaunchedTime + batteryUptime - mLaunchedSince; |
| 3956 | } |
| 3957 | |
| 3958 | long getStartTimeToNowLocked(long batteryUptime) { |
| 3959 | if (!mRunning) return mStartTime; |
| 3960 | return mStartTime + batteryUptime - mRunningSince; |
| 3961 | } |
| 3962 | |
| 3963 | public void startLaunchedLocked() { |
| 3964 | if (!mLaunched) { |
| 3965 | mLaunches++; |
| 3966 | mLaunchedSince = getBatteryUptimeLocked(); |
| 3967 | mLaunched = true; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | public void stopLaunchedLocked() { |
| 3972 | if (mLaunched) { |
| 3973 | long time = getBatteryUptimeLocked() - mLaunchedSince; |
| 3974 | if (time > 0) { |
| 3975 | mLaunchedTime += time; |
| 3976 | } else { |
| 3977 | mLaunches--; |
| 3978 | } |
| 3979 | mLaunched = false; |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | public void startRunningLocked() { |
| 3984 | if (!mRunning) { |
| 3985 | mStarts++; |
| 3986 | mRunningSince = getBatteryUptimeLocked(); |
| 3987 | mRunning = true; |
| 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | public void stopRunningLocked() { |
| 3992 | if (mRunning) { |
| 3993 | long time = getBatteryUptimeLocked() - mRunningSince; |
| 3994 | if (time > 0) { |
| 3995 | mStartTime += time; |
| 3996 | } else { |
| 3997 | mStarts--; |
| 3998 | } |
| 3999 | mRunning = false; |
| 4000 | } |
| 4001 | } |
| 4002 | |
| 4003 | public BatteryStatsImpl getBatteryStats() { |
| 4004 | return BatteryStatsImpl.this; |
| 4005 | } |
| 4006 | |
| 4007 | @Override |
| 4008 | public int getLaunches(int which) { |
| 4009 | int val; |
| 4010 | |
| 4011 | if (which == STATS_LAST) { |
| 4012 | val = mLastLaunches; |
| 4013 | } else { |
| 4014 | val = mLaunches; |
| 4015 | if (which == STATS_CURRENT) { |
| 4016 | val -= mLoadedLaunches; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4017 | } else if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4018 | val -= mUnpluggedLaunches; |
| 4019 | } |
| 4020 | } |
| 4021 | |
| 4022 | return val; |
| 4023 | } |
| 4024 | |
| 4025 | @Override |
| 4026 | public long getStartTime(long now, int which) { |
| 4027 | long val; |
| 4028 | if (which == STATS_LAST) { |
| 4029 | val = mLastStartTime; |
| 4030 | } else { |
| 4031 | val = getStartTimeToNowLocked(now); |
| 4032 | if (which == STATS_CURRENT) { |
| 4033 | val -= mLoadedStartTime; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4034 | } else if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4035 | val -= mUnpluggedStartTime; |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | return val; |
| 4040 | } |
| 4041 | |
| 4042 | @Override |
| 4043 | public int getStarts(int which) { |
| 4044 | int val; |
| 4045 | if (which == STATS_LAST) { |
| 4046 | val = mLastStarts; |
| 4047 | } else { |
| 4048 | val = mStarts; |
| 4049 | if (which == STATS_CURRENT) { |
| 4050 | val -= mLoadedStarts; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4051 | } else if (which == STATS_SINCE_UNPLUGGED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4052 | val -= mUnpluggedStarts; |
| 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | return val; |
| 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | public BatteryStatsImpl getBatteryStats() { |
| 4061 | return BatteryStatsImpl.this; |
| 4062 | } |
| 4063 | |
| 4064 | public void incWakeupsLocked() { |
| 4065 | mWakeups++; |
| 4066 | } |
| 4067 | |
| 4068 | final Serv newServiceStatsLocked() { |
| 4069 | return new Serv(); |
| 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | /** |
| 4074 | * Retrieve the statistics object for a particular process, creating |
| 4075 | * if needed. |
| 4076 | */ |
| 4077 | public Proc getProcessStatsLocked(String name) { |
| 4078 | Proc ps = mProcessStats.get(name); |
| 4079 | if (ps == null) { |
| 4080 | ps = new Proc(); |
| 4081 | mProcessStats.put(name, ps); |
| 4082 | } |
| 4083 | |
| 4084 | return ps; |
| 4085 | } |
| 4086 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 4087 | public SparseArray<? extends Pid> getPidStats() { |
| 4088 | return mPids; |
| 4089 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4090 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4091 | public Pid getPidStatsLocked(int pid) { |
| 4092 | Pid p = mPids.get(pid); |
| 4093 | if (p == null) { |
| 4094 | p = new Pid(); |
| 4095 | mPids.put(pid, p); |
| 4096 | } |
| 4097 | return p; |
| 4098 | } |
| 4099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4100 | /** |
| 4101 | * Retrieve the statistics object for a particular service, creating |
| 4102 | * if needed. |
| 4103 | */ |
| 4104 | public Pkg getPackageStatsLocked(String name) { |
| 4105 | Pkg ps = mPackageStats.get(name); |
| 4106 | if (ps == null) { |
| 4107 | ps = new Pkg(); |
| 4108 | mPackageStats.put(name, ps); |
| 4109 | } |
| 4110 | |
| 4111 | return ps; |
| 4112 | } |
| 4113 | |
| 4114 | /** |
| 4115 | * Retrieve the statistics object for a particular service, creating |
| 4116 | * if needed. |
| 4117 | */ |
| 4118 | public Pkg.Serv getServiceStatsLocked(String pkg, String serv) { |
| 4119 | Pkg ps = getPackageStatsLocked(pkg); |
| 4120 | Pkg.Serv ss = ps.mServiceStats.get(serv); |
| 4121 | if (ss == null) { |
| 4122 | ss = ps.newServiceStatsLocked(); |
| 4123 | ps.mServiceStats.put(serv, ss); |
| 4124 | } |
| 4125 | |
| 4126 | return ss; |
| 4127 | } |
| 4128 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4129 | public StopwatchTimer getWakeTimerLocked(String name, int type) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4130 | Wakelock wl = mWakelockStats.get(name); |
| 4131 | if (wl == null) { |
Dianne Hackborn | c24ab86 | 2011-10-18 15:55:03 -0700 | [diff] [blame] | 4132 | final int N = mWakelockStats.size(); |
Dianne Hackborn | af17baa | 2013-05-09 15:27:47 -0700 | [diff] [blame] | 4133 | if (N > MAX_WAKELOCKS_PER_UID) { |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4134 | name = BATCHED_WAKELOCK_NAME; |
| 4135 | wl = mWakelockStats.get(name); |
| 4136 | } |
| 4137 | if (wl == null) { |
| 4138 | wl = new Wakelock(); |
| 4139 | mWakelockStats.put(name, wl); |
| 4140 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4141 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4142 | StopwatchTimer t = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4143 | switch (type) { |
| 4144 | case WAKE_TYPE_PARTIAL: |
| 4145 | t = wl.mTimerPartial; |
| 4146 | if (t == null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4147 | t = new StopwatchTimer(Uid.this, WAKE_TYPE_PARTIAL, |
| 4148 | mPartialTimers, mUnpluggables); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4149 | wl.mTimerPartial = t; |
| 4150 | } |
| 4151 | return t; |
| 4152 | case WAKE_TYPE_FULL: |
| 4153 | t = wl.mTimerFull; |
| 4154 | if (t == null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4155 | t = new StopwatchTimer(Uid.this, WAKE_TYPE_FULL, |
| 4156 | mFullTimers, mUnpluggables); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4157 | wl.mTimerFull = t; |
| 4158 | } |
| 4159 | return t; |
| 4160 | case WAKE_TYPE_WINDOW: |
| 4161 | t = wl.mTimerWindow; |
| 4162 | if (t == null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4163 | t = new StopwatchTimer(Uid.this, WAKE_TYPE_WINDOW, |
| 4164 | mWindowTimers, mUnpluggables); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4165 | wl.mTimerWindow = t; |
| 4166 | } |
| 4167 | return t; |
| 4168 | default: |
| 4169 | throw new IllegalArgumentException("type=" + type); |
| 4170 | } |
| 4171 | } |
| 4172 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4173 | public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4174 | Sensor se = mSensorStats.get(sensor); |
| 4175 | if (se == null) { |
| 4176 | if (!create) { |
| 4177 | return null; |
| 4178 | } |
| 4179 | se = new Sensor(sensor); |
| 4180 | mSensorStats.put(sensor, se); |
| 4181 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4182 | StopwatchTimer t = se.mTimer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4183 | if (t != null) { |
| 4184 | return t; |
| 4185 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4186 | ArrayList<StopwatchTimer> timers = mSensorTimers.get(sensor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4187 | if (timers == null) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4188 | timers = new ArrayList<StopwatchTimer>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4189 | mSensorTimers.put(sensor, timers); |
| 4190 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4191 | t = new StopwatchTimer(Uid.this, BatteryStats.SENSOR, timers, mUnpluggables); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4192 | se.mTimer = t; |
| 4193 | return t; |
| 4194 | } |
| 4195 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4196 | public void noteStartWakeLocked(int pid, String name, int type) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4197 | StopwatchTimer t = getWakeTimerLocked(name, type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4198 | if (t != null) { |
| 4199 | t.startRunningLocked(BatteryStatsImpl.this); |
| 4200 | } |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 4201 | if (pid >= 0 && type == WAKE_TYPE_PARTIAL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4202 | Pid p = getPidStatsLocked(pid); |
Dianne Hackborn | b8071d79 | 2010-09-09 16:45:15 -0700 | [diff] [blame] | 4203 | if (p.mWakeStart == 0) { |
| 4204 | p.mWakeStart = SystemClock.elapsedRealtime(); |
| 4205 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4206 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4207 | } |
| 4208 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4209 | public void noteStopWakeLocked(int pid, String name, int type) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4210 | StopwatchTimer t = getWakeTimerLocked(name, type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4211 | if (t != null) { |
| 4212 | t.stopRunningLocked(BatteryStatsImpl.this); |
| 4213 | } |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 4214 | if (pid >= 0 && type == WAKE_TYPE_PARTIAL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4215 | Pid p = mPids.get(pid); |
Dianne Hackborn | b8071d79 | 2010-09-09 16:45:15 -0700 | [diff] [blame] | 4216 | if (p != null && p.mWakeStart != 0) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4217 | p.mWakeSum += SystemClock.elapsedRealtime() - p.mWakeStart; |
| 4218 | p.mWakeStart = 0; |
| 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) { |
| 4224 | Proc p = getProcessStatsLocked(proc); |
| 4225 | if (p != null) { |
| 4226 | p.addExcessiveWake(overTime, usedTime); |
| 4227 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4228 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4229 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4230 | public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) { |
| 4231 | Proc p = getProcessStatsLocked(proc); |
| 4232 | if (p != null) { |
| 4233 | p.addExcessiveCpu(overTime, usedTime); |
| 4234 | } |
| 4235 | } |
| 4236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4237 | public void noteStartSensor(int sensor) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4238 | StopwatchTimer t = getSensorTimerLocked(sensor, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4239 | if (t != null) { |
| 4240 | t.startRunningLocked(BatteryStatsImpl.this); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | public void noteStopSensor(int sensor) { |
| 4245 | // Don't create a timer if one doesn't already exist |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4246 | StopwatchTimer t = getSensorTimerLocked(sensor, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4247 | if (t != null) { |
| 4248 | t.stopRunningLocked(BatteryStatsImpl.this); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4249 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4250 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4252 | public void noteStartGps() { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4253 | StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4254 | if (t != null) { |
| 4255 | t.startRunningLocked(BatteryStatsImpl.this); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4257 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4259 | public void noteStopGps() { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4260 | StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4261 | if (t != null) { |
| 4262 | t.stopRunningLocked(BatteryStatsImpl.this); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 4263 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | public BatteryStatsImpl getBatteryStats() { |
| 4267 | return BatteryStatsImpl.this; |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | public BatteryStatsImpl(String filename) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 4272 | mFile = new JournaledFile(new File(filename), new File(filename + ".tmp")); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4273 | mHandler = new MyHandler(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4274 | mStartCount++; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4275 | mScreenOnTimer = new StopwatchTimer(null, -1, null, mUnpluggables); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 4276 | for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4277 | mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mUnpluggables); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 4278 | } |
| 4279 | mInputEventCounter = new Counter(mUnpluggables); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4280 | mPhoneOnTimer = new StopwatchTimer(null, -2, null, mUnpluggables); |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 4281 | for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4282 | mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i, null, mUnpluggables); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 4283 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4284 | mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mUnpluggables); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 4285 | for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4286 | mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i, null, mUnpluggables); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 4287 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4288 | mWifiOnTimer = new StopwatchTimer(null, -3, null, mUnpluggables); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 4289 | mGlobalWifiRunningTimer = new StopwatchTimer(null, -4, null, mUnpluggables); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4290 | mBluetoothOnTimer = new StopwatchTimer(null, -5, null, mUnpluggables); |
| 4291 | mAudioOnTimer = new StopwatchTimer(null, -6, null, mUnpluggables); |
| 4292 | mVideoOnTimer = new StopwatchTimer(null, -7, null, mUnpluggables); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4293 | mOnBattery = mOnBatteryInternal = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4294 | initTimes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4295 | mTrackBatteryPastUptime = 0; |
| 4296 | mTrackBatteryPastRealtime = 0; |
| 4297 | mUptimeStart = mTrackBatteryUptimeStart = SystemClock.uptimeMillis() * 1000; |
| 4298 | mRealtimeStart = mTrackBatteryRealtimeStart = SystemClock.elapsedRealtime() * 1000; |
| 4299 | mUnpluggedBatteryUptime = getBatteryUptimeLocked(mUptimeStart); |
| 4300 | mUnpluggedBatteryRealtime = getBatteryRealtimeLocked(mRealtimeStart); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4301 | mDischargeStartLevel = 0; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4302 | mDischargeUnplugLevel = 0; |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4303 | mDischargeCurrentLevel = 0; |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 4304 | initDischarge(); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4305 | clearHistoryLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | public BatteryStatsImpl(Parcel p) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 4309 | mFile = null; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4310 | mHandler = null; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4311 | clearHistoryLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4312 | readFromParcel(p); |
| 4313 | } |
| 4314 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 4315 | public void setCallback(BatteryCallback cb) { |
| 4316 | mCallback = cb; |
| 4317 | } |
| 4318 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 4319 | public void setNumSpeedSteps(int steps) { |
| 4320 | if (sNumSpeedSteps == 0) sNumSpeedSteps = steps; |
| 4321 | } |
| 4322 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 4323 | public void setRadioScanningTimeout(long timeout) { |
| 4324 | if (mPhoneSignalScanningTimer != null) { |
| 4325 | mPhoneSignalScanningTimer.setTimeout(timeout); |
| 4326 | } |
| 4327 | } |
| 4328 | |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4329 | @Override |
| 4330 | public boolean startIteratingOldHistoryLocked() { |
| 4331 | if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize() |
| 4332 | + " pos=" + mHistoryBuffer.dataPosition()); |
| 4333 | mHistoryBuffer.setDataPosition(0); |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 4334 | mHistoryReadTmp.clear(); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4335 | mReadOverflow = false; |
| 4336 | mIteratingHistory = true; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 4337 | return (mHistoryIterator = mHistory) != null; |
| 4338 | } |
| 4339 | |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4340 | @Override |
| 4341 | public boolean getNextOldHistoryLocked(HistoryItem out) { |
| 4342 | boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize(); |
| 4343 | if (!end) { |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 4344 | mHistoryReadTmp.readDelta(mHistoryBuffer); |
| 4345 | mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4346 | } |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 4347 | HistoryItem cur = mHistoryIterator; |
| 4348 | if (cur == null) { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4349 | if (!mReadOverflow && !end) { |
| 4350 | Slog.w(TAG, "Old history ends before new history!"); |
| 4351 | } |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 4352 | return false; |
| 4353 | } |
| 4354 | out.setTo(cur); |
| 4355 | mHistoryIterator = cur.next; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4356 | if (!mReadOverflow) { |
| 4357 | if (end) { |
| 4358 | Slog.w(TAG, "New history ends before old history!"); |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 4359 | } else if (!out.same(mHistoryReadTmp)) { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4360 | long now = getHistoryBaseTime() + SystemClock.elapsedRealtime(); |
| 4361 | PrintWriter pw = new PrintWriter(new LogWriter(android.util.Log.WARN, TAG)); |
| 4362 | pw.println("Histories differ!"); |
| 4363 | pw.println("Old history:"); |
| 4364 | (new HistoryPrinter()).printNextItem(pw, out, now); |
| 4365 | pw.println("New history:"); |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 4366 | (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, now); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4367 | } |
| 4368 | } |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 4369 | return true; |
| 4370 | } |
| 4371 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4372 | @Override |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4373 | public void finishIteratingOldHistoryLocked() { |
| 4374 | mIteratingHistory = false; |
| 4375 | mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize()); |
| 4376 | } |
| 4377 | |
| 4378 | @Override |
| 4379 | public boolean startIteratingHistoryLocked() { |
| 4380 | if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize() |
| 4381 | + " pos=" + mHistoryBuffer.dataPosition()); |
| 4382 | mHistoryBuffer.setDataPosition(0); |
| 4383 | mReadOverflow = false; |
| 4384 | mIteratingHistory = true; |
| 4385 | return mHistoryBuffer.dataSize() > 0; |
| 4386 | } |
| 4387 | |
| 4388 | @Override |
| 4389 | public boolean getNextHistoryLocked(HistoryItem out) { |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 4390 | final int pos = mHistoryBuffer.dataPosition(); |
| 4391 | if (pos == 0) { |
| 4392 | out.clear(); |
| 4393 | } |
| 4394 | boolean end = pos >= mHistoryBuffer.dataSize(); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4395 | if (end) { |
| 4396 | return false; |
| 4397 | } |
| 4398 | |
Dianne Hackborn | 1fadab5 | 2011-04-14 17:57:33 -0700 | [diff] [blame] | 4399 | out.readDelta(mHistoryBuffer); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 4400 | return true; |
| 4401 | } |
| 4402 | |
| 4403 | @Override |
| 4404 | public void finishIteratingHistoryLocked() { |
| 4405 | mIteratingHistory = false; |
| 4406 | mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize()); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 4407 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4408 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 4409 | @Override |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 4410 | public long getHistoryBaseTime() { |
| 4411 | return mHistoryBaseTime; |
| 4412 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4413 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 4414 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4415 | public int getStartCount() { |
| 4416 | return mStartCount; |
| 4417 | } |
| 4418 | |
| 4419 | public boolean isOnBattery() { |
| 4420 | return mOnBattery; |
| 4421 | } |
| 4422 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4423 | public boolean isScreenOn() { |
| 4424 | return mScreenOn; |
| 4425 | } |
| 4426 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4427 | void initTimes() { |
| 4428 | mBatteryRealtime = mTrackBatteryPastUptime = 0; |
| 4429 | mBatteryUptime = mTrackBatteryPastRealtime = 0; |
| 4430 | mUptimeStart = mTrackBatteryUptimeStart = SystemClock.uptimeMillis() * 1000; |
| 4431 | mRealtimeStart = mTrackBatteryRealtimeStart = SystemClock.elapsedRealtime() * 1000; |
| 4432 | mUnpluggedBatteryUptime = getBatteryUptimeLocked(mUptimeStart); |
| 4433 | mUnpluggedBatteryRealtime = getBatteryRealtimeLocked(mRealtimeStart); |
| 4434 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4435 | |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 4436 | void initDischarge() { |
| 4437 | mLowDischargeAmountSinceCharge = 0; |
| 4438 | mHighDischargeAmountSinceCharge = 0; |
| 4439 | mDischargeAmountScreenOn = 0; |
| 4440 | mDischargeAmountScreenOnSinceCharge = 0; |
| 4441 | mDischargeAmountScreenOff = 0; |
| 4442 | mDischargeAmountScreenOffSinceCharge = 0; |
| 4443 | } |
| 4444 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4445 | public void resetAllStatsLocked() { |
| 4446 | mStartCount = 0; |
| 4447 | initTimes(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4448 | mScreenOnTimer.reset(this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4449 | for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4450 | mScreenBrightnessTimer[i].reset(this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4451 | } |
| 4452 | mInputEventCounter.reset(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4453 | mPhoneOnTimer.reset(this, false); |
| 4454 | mAudioOnTimer.reset(this, false); |
| 4455 | mVideoOnTimer.reset(this, false); |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 4456 | for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4457 | mPhoneSignalStrengthsTimer[i].reset(this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4458 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4459 | mPhoneSignalScanningTimer.reset(this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4460 | for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4461 | mPhoneDataConnectionsTimer[i].reset(this, false); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4462 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4463 | mWifiOnTimer.reset(this, false); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 4464 | mGlobalWifiRunningTimer.reset(this, false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4465 | mBluetoothOnTimer.reset(this, false); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4466 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4467 | for (int i=0; i<mUidStats.size(); i++) { |
| 4468 | if (mUidStats.valueAt(i).reset()) { |
| 4469 | mUidStats.remove(mUidStats.keyAt(i)); |
| 4470 | i--; |
| 4471 | } |
| 4472 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4473 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4474 | if (mKernelWakelockStats.size() > 0) { |
| 4475 | for (SamplingTimer timer : mKernelWakelockStats.values()) { |
| 4476 | mUnpluggables.remove(timer); |
| 4477 | } |
| 4478 | mKernelWakelockStats.clear(); |
| 4479 | } |
| 4480 | |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 4481 | initDischarge(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4482 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4483 | clearHistoryLocked(); |
| 4484 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4485 | |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4486 | void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) { |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 4487 | if (oldScreenOn) { |
| 4488 | int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel; |
| 4489 | if (diff > 0) { |
| 4490 | mDischargeAmountScreenOn += diff; |
| 4491 | mDischargeAmountScreenOnSinceCharge += diff; |
| 4492 | } |
| 4493 | } else { |
| 4494 | int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel; |
| 4495 | if (diff > 0) { |
| 4496 | mDischargeAmountScreenOff += diff; |
| 4497 | mDischargeAmountScreenOffSinceCharge += diff; |
| 4498 | } |
| 4499 | } |
| 4500 | if (newScreenOn) { |
| 4501 | mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel; |
| 4502 | mDischargeScreenOffUnplugLevel = 0; |
| 4503 | } else { |
| 4504 | mDischargeScreenOnUnplugLevel = 0; |
| 4505 | mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel; |
| 4506 | } |
| 4507 | } |
| 4508 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4509 | void setOnBattery(boolean onBattery, int oldStatus, int level) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4510 | synchronized(this) { |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4511 | setOnBatteryLocked(onBattery, oldStatus, level); |
| 4512 | } |
| 4513 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4514 | |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4515 | void setOnBatteryLocked(boolean onBattery, int oldStatus, int level) { |
| 4516 | boolean doWrite = false; |
| 4517 | Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE); |
| 4518 | m.arg1 = onBattery ? 1 : 0; |
| 4519 | mHandler.sendMessage(m); |
| 4520 | mOnBattery = mOnBatteryInternal = onBattery; |
| 4521 | |
| 4522 | long uptime = SystemClock.uptimeMillis() * 1000; |
| 4523 | long mSecRealtime = SystemClock.elapsedRealtime(); |
| 4524 | long realtime = mSecRealtime * 1000; |
| 4525 | if (onBattery) { |
| 4526 | // We will reset our status if we are unplugging after the |
| 4527 | // battery was last full, or the level is at 100, or |
| 4528 | // we have gone through a significant charge (from a very low |
| 4529 | // level to a now very high level). |
| 4530 | if (oldStatus == BatteryManager.BATTERY_STATUS_FULL |
| 4531 | || level >= 90 |
| 4532 | || (mDischargeCurrentLevel < 20 && level >= 80)) { |
| 4533 | doWrite = true; |
| 4534 | resetAllStatsLocked(); |
| 4535 | mDischargeStartLevel = level; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4536 | } |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4537 | updateKernelWakelocksLocked(); |
| 4538 | mHistoryCur.batteryLevel = (byte)level; |
| 4539 | mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG; |
| 4540 | if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: " |
| 4541 | + Integer.toHexString(mHistoryCur.states)); |
| 4542 | addHistoryRecordLocked(mSecRealtime); |
| 4543 | mTrackBatteryUptimeStart = uptime; |
| 4544 | mTrackBatteryRealtimeStart = realtime; |
| 4545 | mUnpluggedBatteryUptime = getBatteryUptimeLocked(uptime); |
| 4546 | mUnpluggedBatteryRealtime = getBatteryRealtimeLocked(realtime); |
| 4547 | mDischargeCurrentLevel = mDischargeUnplugLevel = level; |
| 4548 | if (mScreenOn) { |
| 4549 | mDischargeScreenOnUnplugLevel = level; |
| 4550 | mDischargeScreenOffUnplugLevel = 0; |
| 4551 | } else { |
| 4552 | mDischargeScreenOnUnplugLevel = 0; |
| 4553 | mDischargeScreenOffUnplugLevel = level; |
| 4554 | } |
| 4555 | mDischargeAmountScreenOn = 0; |
| 4556 | mDischargeAmountScreenOff = 0; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 4557 | doUnplugLocked(realtime, mUnpluggedBatteryUptime, mUnpluggedBatteryRealtime); |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4558 | } else { |
| 4559 | updateKernelWakelocksLocked(); |
| 4560 | mHistoryCur.batteryLevel = (byte)level; |
| 4561 | mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG; |
| 4562 | if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: " |
| 4563 | + Integer.toHexString(mHistoryCur.states)); |
| 4564 | addHistoryRecordLocked(mSecRealtime); |
| 4565 | mTrackBatteryPastUptime += uptime - mTrackBatteryUptimeStart; |
| 4566 | mTrackBatteryPastRealtime += realtime - mTrackBatteryRealtimeStart; |
| 4567 | mDischargeCurrentLevel = level; |
| 4568 | if (level < mDischargeUnplugLevel) { |
| 4569 | mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1; |
| 4570 | mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level; |
| 4571 | } |
| 4572 | updateDischargeScreenLevelsLocked(mScreenOn, mScreenOn); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 4573 | doPlugLocked(realtime, getBatteryUptimeLocked(uptime), getBatteryRealtimeLocked(realtime)); |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4574 | } |
| 4575 | if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) { |
| 4576 | if (mFile != null) { |
| 4577 | writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4578 | } |
| 4579 | } |
| 4580 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4581 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4582 | // This should probably be exposed in the API, though it's not critical |
| 4583 | private static final int BATTERY_PLUGGED_NONE = 0; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4584 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4585 | public void setBatteryState(int status, int health, int plugType, int level, |
| 4586 | int temp, int volt) { |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4587 | synchronized(this) { |
| 4588 | boolean onBattery = plugType == BATTERY_PLUGGED_NONE; |
| 4589 | int oldStatus = mHistoryCur.batteryStatus; |
| 4590 | if (!mHaveBatteryLevel) { |
| 4591 | mHaveBatteryLevel = true; |
| 4592 | // We start out assuming that the device is plugged in (not |
| 4593 | // on battery). If our first report is now that we are indeed |
| 4594 | // plugged in, then twiddle our state to correctly reflect that |
| 4595 | // since we won't be going through the full setOnBattery(). |
| 4596 | if (onBattery == mOnBattery) { |
| 4597 | if (onBattery) { |
| 4598 | mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG; |
| 4599 | } else { |
| 4600 | mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG; |
| 4601 | } |
| 4602 | } |
| 4603 | oldStatus = status; |
| 4604 | } |
| 4605 | if (onBattery) { |
| 4606 | mDischargeCurrentLevel = level; |
| 4607 | mRecordingHistory = true; |
| 4608 | } |
| 4609 | if (onBattery != mOnBattery) { |
| 4610 | mHistoryCur.batteryLevel = (byte)level; |
| 4611 | mHistoryCur.batteryStatus = (byte)status; |
| 4612 | mHistoryCur.batteryHealth = (byte)health; |
| 4613 | mHistoryCur.batteryPlugType = (byte)plugType; |
| 4614 | mHistoryCur.batteryTemperature = (char)temp; |
| 4615 | mHistoryCur.batteryVoltage = (char)volt; |
| 4616 | setOnBatteryLocked(onBattery, oldStatus, level); |
| 4617 | } else { |
| 4618 | boolean changed = false; |
| 4619 | if (mHistoryCur.batteryLevel != level) { |
| 4620 | mHistoryCur.batteryLevel = (byte)level; |
| 4621 | changed = true; |
| 4622 | } |
| 4623 | if (mHistoryCur.batteryStatus != status) { |
| 4624 | mHistoryCur.batteryStatus = (byte)status; |
| 4625 | changed = true; |
| 4626 | } |
| 4627 | if (mHistoryCur.batteryHealth != health) { |
| 4628 | mHistoryCur.batteryHealth = (byte)health; |
| 4629 | changed = true; |
| 4630 | } |
| 4631 | if (mHistoryCur.batteryPlugType != plugType) { |
| 4632 | mHistoryCur.batteryPlugType = (byte)plugType; |
| 4633 | changed = true; |
| 4634 | } |
| 4635 | if (temp >= (mHistoryCur.batteryTemperature+10) |
| 4636 | || temp <= (mHistoryCur.batteryTemperature-10)) { |
| 4637 | mHistoryCur.batteryTemperature = (char)temp; |
| 4638 | changed = true; |
| 4639 | } |
| 4640 | if (volt > (mHistoryCur.batteryVoltage+20) |
| 4641 | || volt < (mHistoryCur.batteryVoltage-20)) { |
| 4642 | mHistoryCur.batteryVoltage = (char)volt; |
| 4643 | changed = true; |
| 4644 | } |
| 4645 | if (changed) { |
| 4646 | addHistoryRecordLocked(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4647 | } |
| 4648 | } |
Dianne Hackborn | 32de2f6 | 2011-03-09 14:03:35 -0800 | [diff] [blame] | 4649 | if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) { |
| 4650 | // We don't record history while we are plugged in and fully charged. |
| 4651 | // The next time we are unplugged, history will be cleared. |
| 4652 | mRecordingHistory = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4653 | } |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 4654 | } |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4655 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4656 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4657 | public void updateKernelWakelocksLocked() { |
| 4658 | Map<String, KernelWakelockStats> m = readKernelWakelockStats(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4659 | |
Marco Nelissen | d859331 | 2009-04-30 14:45:06 -0700 | [diff] [blame] | 4660 | if (m == null) { |
| 4661 | // Not crashing might make board bringup easier. |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 4662 | Slog.w(TAG, "Couldn't get kernel wake lock stats"); |
Marco Nelissen | d859331 | 2009-04-30 14:45:06 -0700 | [diff] [blame] | 4663 | return; |
| 4664 | } |
| 4665 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4666 | for (Map.Entry<String, KernelWakelockStats> ent : m.entrySet()) { |
| 4667 | String name = ent.getKey(); |
| 4668 | KernelWakelockStats kws = ent.getValue(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4669 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4670 | SamplingTimer kwlt = mKernelWakelockStats.get(name); |
| 4671 | if (kwlt == null) { |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4672 | kwlt = new SamplingTimer(mUnpluggables, mOnBatteryInternal, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4673 | true /* track reported values */); |
| 4674 | mKernelWakelockStats.put(name, kwlt); |
| 4675 | } |
| 4676 | kwlt.updateCurrentReportedCount(kws.mCount); |
| 4677 | kwlt.updateCurrentReportedTotalTime(kws.mTotalTime); |
| 4678 | kwlt.setUpdateVersion(sKernelWakelockUpdateVersion); |
| 4679 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4680 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 4681 | if (m.size() != mKernelWakelockStats.size()) { |
| 4682 | // Set timers to stale if they didn't appear in /proc/wakelocks this time. |
| 4683 | for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) { |
| 4684 | SamplingTimer st = ent.getValue(); |
| 4685 | if (st.getUpdateVersion() != sKernelWakelockUpdateVersion) { |
| 4686 | st.setStale(); |
| 4687 | } |
| 4688 | } |
| 4689 | } |
| 4690 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4691 | |
| 4692 | public long getAwakeTimeBattery() { |
| 4693 | return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT); |
| 4694 | } |
| 4695 | |
| 4696 | public long getAwakeTimePlugged() { |
| 4697 | return (SystemClock.uptimeMillis() * 1000) - getAwakeTimeBattery(); |
| 4698 | } |
| 4699 | |
| 4700 | @Override |
| 4701 | public long computeUptime(long curTime, int which) { |
| 4702 | switch (which) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4703 | case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4704 | case STATS_LAST: return mLastUptime; |
| 4705 | case STATS_CURRENT: return (curTime-mUptimeStart); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4706 | case STATS_SINCE_UNPLUGGED: return (curTime-mTrackBatteryUptimeStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4707 | } |
| 4708 | return 0; |
| 4709 | } |
| 4710 | |
| 4711 | @Override |
| 4712 | public long computeRealtime(long curTime, int which) { |
| 4713 | switch (which) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4714 | case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4715 | case STATS_LAST: return mLastRealtime; |
| 4716 | case STATS_CURRENT: return (curTime-mRealtimeStart); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4717 | case STATS_SINCE_UNPLUGGED: return (curTime-mTrackBatteryRealtimeStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4718 | } |
| 4719 | return 0; |
| 4720 | } |
| 4721 | |
| 4722 | @Override |
| 4723 | public long computeBatteryUptime(long curTime, int which) { |
| 4724 | switch (which) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4725 | case STATS_SINCE_CHARGED: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4726 | return mBatteryUptime + getBatteryUptime(curTime); |
| 4727 | case STATS_LAST: |
| 4728 | return mBatteryLastUptime; |
| 4729 | case STATS_CURRENT: |
| 4730 | return getBatteryUptime(curTime); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4731 | case STATS_SINCE_UNPLUGGED: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4732 | return getBatteryUptimeLocked(curTime) - mUnpluggedBatteryUptime; |
| 4733 | } |
| 4734 | return 0; |
| 4735 | } |
| 4736 | |
| 4737 | @Override |
| 4738 | public long computeBatteryRealtime(long curTime, int which) { |
| 4739 | switch (which) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4740 | case STATS_SINCE_CHARGED: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4741 | return mBatteryRealtime + getBatteryRealtimeLocked(curTime); |
| 4742 | case STATS_LAST: |
| 4743 | return mBatteryLastRealtime; |
| 4744 | case STATS_CURRENT: |
| 4745 | return getBatteryRealtimeLocked(curTime); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4746 | case STATS_SINCE_UNPLUGGED: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4747 | return getBatteryRealtimeLocked(curTime) - mUnpluggedBatteryRealtime; |
| 4748 | } |
| 4749 | return 0; |
| 4750 | } |
| 4751 | |
| 4752 | long getBatteryUptimeLocked(long curTime) { |
| 4753 | long time = mTrackBatteryPastUptime; |
| 4754 | if (mOnBatteryInternal) { |
| 4755 | time += curTime - mTrackBatteryUptimeStart; |
| 4756 | } |
| 4757 | return time; |
| 4758 | } |
| 4759 | |
| 4760 | long getBatteryUptimeLocked() { |
| 4761 | return getBatteryUptime(SystemClock.uptimeMillis() * 1000); |
| 4762 | } |
| 4763 | |
| 4764 | @Override |
| 4765 | public long getBatteryUptime(long curTime) { |
| 4766 | return getBatteryUptimeLocked(curTime); |
| 4767 | } |
| 4768 | |
| 4769 | long getBatteryRealtimeLocked(long curTime) { |
| 4770 | long time = mTrackBatteryPastRealtime; |
| 4771 | if (mOnBatteryInternal) { |
| 4772 | time += curTime - mTrackBatteryRealtimeStart; |
| 4773 | } |
| 4774 | return time; |
| 4775 | } |
| 4776 | |
| 4777 | @Override |
| 4778 | public long getBatteryRealtime(long curTime) { |
| 4779 | return getBatteryRealtimeLocked(curTime); |
| 4780 | } |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4781 | |
| 4782 | private long getTcpBytes(long current, long[] dataBytes, int which) { |
| 4783 | if (which == STATS_LAST) { |
| 4784 | return dataBytes[STATS_LAST]; |
| 4785 | } else { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4786 | if (which == STATS_SINCE_UNPLUGGED) { |
| 4787 | if (dataBytes[STATS_SINCE_UNPLUGGED] < 0) { |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4788 | return dataBytes[STATS_LAST]; |
| 4789 | } else { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4790 | return current - dataBytes[STATS_SINCE_UNPLUGGED]; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4791 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4792 | } else if (which == STATS_SINCE_CHARGED) { |
| 4793 | return (current - dataBytes[STATS_CURRENT]) + dataBytes[STATS_SINCE_CHARGED]; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4794 | } |
| 4795 | return current - dataBytes[STATS_CURRENT]; |
| 4796 | } |
| 4797 | } |
| 4798 | |
| 4799 | /** Only STATS_UNPLUGGED works properly */ |
| 4800 | public long getMobileTcpBytesSent(int which) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 4801 | final long mobileTxBytes = getNetworkStatsSummary().getTotal(null, mMobileIfaces).txBytes; |
| 4802 | return getTcpBytes(mobileTxBytes, mMobileDataTx, which); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | /** Only STATS_UNPLUGGED works properly */ |
| 4806 | public long getMobileTcpBytesReceived(int which) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 4807 | final long mobileRxBytes = getNetworkStatsSummary().getTotal(null, mMobileIfaces).rxBytes; |
| 4808 | return getTcpBytes(mobileRxBytes, mMobileDataRx, which); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /** Only STATS_UNPLUGGED works properly */ |
| 4812 | public long getTotalTcpBytesSent(int which) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 4813 | final long totalTxBytes = getNetworkStatsSummary().getTotal(null).txBytes; |
| 4814 | return getTcpBytes(totalTxBytes, mTotalDataTx, which); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | /** Only STATS_UNPLUGGED works properly */ |
| 4818 | public long getTotalTcpBytesReceived(int which) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 4819 | final long totalRxBytes = getNetworkStatsSummary().getTotal(null).rxBytes; |
| 4820 | return getTcpBytes(totalRxBytes, mTotalDataRx, which); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 4821 | } |
| 4822 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4823 | @Override |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4824 | public int getDischargeStartLevel() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4825 | synchronized(this) { |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4826 | return getDischargeStartLevelLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4827 | } |
| 4828 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4829 | |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4830 | public int getDischargeStartLevelLocked() { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 4831 | return mDischargeUnplugLevel; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4832 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4833 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4834 | @Override |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4835 | public int getDischargeCurrentLevel() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4836 | synchronized(this) { |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4837 | return getDischargeCurrentLevelLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4838 | } |
| 4839 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 4840 | |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 4841 | public int getDischargeCurrentLevelLocked() { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 4842 | return mDischargeCurrentLevel; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4843 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4844 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 4845 | @Override |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 4846 | public int getLowDischargeAmountSinceCharge() { |
| 4847 | synchronized(this) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 4848 | int val = mLowDischargeAmountSinceCharge; |
| 4849 | if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) { |
| 4850 | val += mDischargeUnplugLevel-mDischargeCurrentLevel-1; |
| 4851 | } |
| 4852 | return val; |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 4853 | } |
| 4854 | } |
| 4855 | |
| 4856 | @Override |
| 4857 | public int getHighDischargeAmountSinceCharge() { |
| 4858 | synchronized(this) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 4859 | int val = mHighDischargeAmountSinceCharge; |
| 4860 | if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) { |
| 4861 | val += mDischargeUnplugLevel-mDischargeCurrentLevel; |
| 4862 | } |
| 4863 | return val; |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 4864 | } |
| 4865 | } |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 4866 | |
| 4867 | public int getDischargeAmountScreenOn() { |
| 4868 | synchronized(this) { |
| 4869 | int val = mDischargeAmountScreenOn; |
| 4870 | if (mOnBattery && mScreenOn |
| 4871 | && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) { |
| 4872 | val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel; |
| 4873 | } |
| 4874 | return val; |
| 4875 | } |
| 4876 | } |
| 4877 | |
| 4878 | public int getDischargeAmountScreenOnSinceCharge() { |
| 4879 | synchronized(this) { |
| 4880 | int val = mDischargeAmountScreenOnSinceCharge; |
| 4881 | if (mOnBattery && mScreenOn |
| 4882 | && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) { |
| 4883 | val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel; |
| 4884 | } |
| 4885 | return val; |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | public int getDischargeAmountScreenOff() { |
| 4890 | synchronized(this) { |
| 4891 | int val = mDischargeAmountScreenOff; |
| 4892 | if (mOnBattery && !mScreenOn |
| 4893 | && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) { |
| 4894 | val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel; |
| 4895 | } |
| 4896 | return val; |
| 4897 | } |
| 4898 | } |
| 4899 | |
| 4900 | public int getDischargeAmountScreenOffSinceCharge() { |
| 4901 | synchronized(this) { |
| 4902 | int val = mDischargeAmountScreenOffSinceCharge; |
| 4903 | if (mOnBattery && !mScreenOn |
| 4904 | && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) { |
| 4905 | val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel; |
| 4906 | } |
| 4907 | return val; |
| 4908 | } |
| 4909 | } |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 4910 | |
| 4911 | @Override |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 4912 | public int getCpuSpeedSteps() { |
| 4913 | return sNumSpeedSteps; |
| 4914 | } |
| 4915 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4916 | /** |
| 4917 | * Retrieve the statistics object for a particular uid, creating if needed. |
| 4918 | */ |
| 4919 | public Uid getUidStatsLocked(int uid) { |
| 4920 | Uid u = mUidStats.get(uid); |
| 4921 | if (u == null) { |
| 4922 | u = new Uid(uid); |
| 4923 | mUidStats.put(uid, u); |
| 4924 | } |
| 4925 | return u; |
| 4926 | } |
| 4927 | |
| 4928 | /** |
| 4929 | * Remove the statistics object for a particular uid. |
| 4930 | */ |
| 4931 | public void removeUidStatsLocked(int uid) { |
| 4932 | mUidStats.remove(uid); |
| 4933 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 4934 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4935 | /** |
| 4936 | * Retrieve the statistics object for a particular process, creating |
| 4937 | * if needed. |
| 4938 | */ |
| 4939 | public Uid.Proc getProcessStatsLocked(int uid, String name) { |
| 4940 | Uid u = getUidStatsLocked(uid); |
| 4941 | return u.getProcessStatsLocked(name); |
| 4942 | } |
| 4943 | |
| 4944 | /** |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 4945 | * Retrieve the statistics object for a particular process, given |
| 4946 | * the name of the process. |
| 4947 | * @param name process name |
| 4948 | * @return the statistics object for the process |
| 4949 | */ |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 4950 | public Uid.Proc getProcessStatsLocked(String name, int pid) { |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 4951 | int uid; |
| 4952 | if (mUidCache.containsKey(name)) { |
| 4953 | uid = mUidCache.get(name); |
| 4954 | } else { |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 4955 | uid = Process.getUidForPid(pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 4956 | mUidCache.put(name, uid); |
| 4957 | } |
| 4958 | Uid u = getUidStatsLocked(uid); |
| 4959 | return u.getProcessStatsLocked(name); |
| 4960 | } |
| 4961 | |
| 4962 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4963 | * Retrieve the statistics object for a particular process, creating |
| 4964 | * if needed. |
| 4965 | */ |
| 4966 | public Uid.Pkg getPackageStatsLocked(int uid, String pkg) { |
| 4967 | Uid u = getUidStatsLocked(uid); |
| 4968 | return u.getPackageStatsLocked(pkg); |
| 4969 | } |
| 4970 | |
| 4971 | /** |
| 4972 | * Retrieve the statistics object for a particular service, creating |
| 4973 | * if needed. |
| 4974 | */ |
| 4975 | public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) { |
| 4976 | Uid u = getUidStatsLocked(uid); |
| 4977 | return u.getServiceStatsLocked(pkg, name); |
| 4978 | } |
| 4979 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 4980 | /** |
| 4981 | * Massage data to distribute any reasonable work down to more specific |
| 4982 | * owners. Must only be called on a dead BatteryStats object! |
| 4983 | */ |
| 4984 | public void distributeWorkLocked(int which) { |
| 4985 | // Aggregate all CPU time associated with WIFI. |
| 4986 | Uid wifiUid = mUidStats.get(Process.WIFI_UID); |
| 4987 | if (wifiUid != null) { |
| 4988 | long uSecTime = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which); |
| 4989 | for (Uid.Proc proc : wifiUid.mProcessStats.values()) { |
| 4990 | long totalRunningTime = getGlobalWifiRunningTime(uSecTime, which); |
| 4991 | for (int i=0; i<mUidStats.size(); i++) { |
| 4992 | Uid uid = mUidStats.valueAt(i); |
| 4993 | if (uid.mUid != Process.WIFI_UID) { |
| 4994 | long uidRunningTime = uid.getWifiRunningTime(uSecTime, which); |
| 4995 | if (uidRunningTime > 0) { |
| 4996 | Uid.Proc uidProc = uid.getProcessStatsLocked("*wifi*"); |
| 4997 | long time = proc.getUserTime(which); |
| 4998 | time = (time*uidRunningTime)/totalRunningTime; |
| 4999 | uidProc.mUserTime += time; |
| 5000 | proc.mUserTime -= time; |
| 5001 | time = proc.getSystemTime(which); |
| 5002 | time = (time*uidRunningTime)/totalRunningTime; |
| 5003 | uidProc.mSystemTime += time; |
| 5004 | proc.mSystemTime -= time; |
| 5005 | time = proc.getForegroundTime(which); |
| 5006 | time = (time*uidRunningTime)/totalRunningTime; |
| 5007 | uidProc.mForegroundTime += time; |
| 5008 | proc.mForegroundTime -= time; |
| 5009 | for (int sb=0; sb<proc.mSpeedBins.length; sb++) { |
| 5010 | SamplingCounter sc = proc.mSpeedBins[sb]; |
| 5011 | if (sc != null) { |
| 5012 | time = sc.getCountLocked(which); |
| 5013 | time = (time*uidRunningTime)/totalRunningTime; |
| 5014 | SamplingCounter uidSc = uidProc.mSpeedBins[sb]; |
| 5015 | if (uidSc == null) { |
| 5016 | uidSc = new SamplingCounter(mUnpluggables); |
| 5017 | uidProc.mSpeedBins[sb] = uidSc; |
| 5018 | } |
| 5019 | uidSc.mCount.addAndGet((int)time); |
| 5020 | sc.mCount.addAndGet((int)-time); |
| 5021 | } |
| 5022 | } |
| 5023 | totalRunningTime -= uidRunningTime; |
| 5024 | } |
| 5025 | } |
| 5026 | } |
| 5027 | } |
| 5028 | } |
| 5029 | } |
| 5030 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5031 | public void shutdownLocked() { |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 5032 | writeSyncLocked(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5033 | mShuttingDown = true; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5034 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5035 | |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 5036 | Parcel mPendingWrite = null; |
| 5037 | final ReentrantLock mWriteLock = new ReentrantLock(); |
| 5038 | |
| 5039 | public void writeAsyncLocked() { |
| 5040 | writeLocked(false); |
| 5041 | } |
| 5042 | |
| 5043 | public void writeSyncLocked() { |
| 5044 | writeLocked(true); |
| 5045 | } |
| 5046 | |
| 5047 | void writeLocked(boolean sync) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5048 | if (mFile == null) { |
| 5049 | Slog.w("BatteryStats", "writeLocked: no file associated with this instance"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5050 | return; |
| 5051 | } |
| 5052 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5053 | if (mShuttingDown) { |
| 5054 | return; |
| 5055 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5056 | |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 5057 | Parcel out = Parcel.obtain(); |
| 5058 | writeSummaryToParcel(out); |
| 5059 | mLastWriteTime = SystemClock.elapsedRealtime(); |
| 5060 | |
| 5061 | if (mPendingWrite != null) { |
| 5062 | mPendingWrite.recycle(); |
| 5063 | } |
| 5064 | mPendingWrite = out; |
| 5065 | |
| 5066 | if (sync) { |
| 5067 | commitPendingDataToDisk(); |
| 5068 | } else { |
| 5069 | Thread thr = new Thread("BatteryStats-Write") { |
| 5070 | @Override |
| 5071 | public void run() { |
| 5072 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 5073 | commitPendingDataToDisk(); |
| 5074 | } |
| 5075 | }; |
| 5076 | thr.start(); |
| 5077 | } |
| 5078 | } |
| 5079 | |
| 5080 | public void commitPendingDataToDisk() { |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 5081 | final Parcel next; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 5082 | synchronized (this) { |
| 5083 | next = mPendingWrite; |
| 5084 | mPendingWrite = null; |
Dianne Hackborn | f47d8f2 | 2010-10-08 10:46:55 -0700 | [diff] [blame] | 5085 | if (next == null) { |
| 5086 | return; |
| 5087 | } |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 5088 | |
| 5089 | mWriteLock.lock(); |
| 5090 | } |
| 5091 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5092 | try { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5093 | FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite()); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 5094 | stream.write(next.marshall()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5095 | stream.flush(); |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 5096 | FileUtils.sync(stream); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5097 | stream.close(); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5098 | mFile.commit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5099 | } catch (IOException e) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5100 | Slog.w("BatteryStats", "Error writing battery statistics", e); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 5101 | mFile.rollback(); |
| 5102 | } finally { |
| 5103 | next.recycle(); |
| 5104 | mWriteLock.unlock(); |
Suchi Amalapurapu | 8550f25 | 2009-09-29 15:20:32 -0700 | [diff] [blame] | 5105 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | static byte[] readFully(FileInputStream stream) throws java.io.IOException { |
| 5109 | int pos = 0; |
| 5110 | int avail = stream.available(); |
| 5111 | byte[] data = new byte[avail]; |
| 5112 | while (true) { |
| 5113 | int amt = stream.read(data, pos, data.length-pos); |
| 5114 | //Log.i("foo", "Read " + amt + " bytes at " + pos |
| 5115 | // + " of avail " + data.length); |
| 5116 | if (amt <= 0) { |
| 5117 | //Log.i("foo", "**** FINISHED READING: pos=" + pos |
| 5118 | // + " len=" + data.length); |
| 5119 | return data; |
| 5120 | } |
| 5121 | pos += amt; |
| 5122 | avail = stream.available(); |
| 5123 | if (avail > data.length-pos) { |
| 5124 | byte[] newData = new byte[pos+avail]; |
| 5125 | System.arraycopy(data, 0, newData, 0, pos); |
| 5126 | data = newData; |
| 5127 | } |
| 5128 | } |
| 5129 | } |
| 5130 | |
| 5131 | public void readLocked() { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5132 | if (mFile == null) { |
| 5133 | Slog.w("BatteryStats", "readLocked: no file associated with this instance"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5134 | return; |
| 5135 | } |
| 5136 | |
| 5137 | mUidStats.clear(); |
| 5138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5139 | try { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5140 | File file = mFile.chooseForRead(); |
| 5141 | if (!file.exists()) { |
| 5142 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5143 | } |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5144 | FileInputStream stream = new FileInputStream(file); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5145 | |
| 5146 | byte[] raw = readFully(stream); |
| 5147 | Parcel in = Parcel.obtain(); |
| 5148 | in.unmarshall(raw, 0, raw.length); |
| 5149 | in.setDataPosition(0); |
| 5150 | stream.close(); |
| 5151 | |
| 5152 | readSummaryFromParcel(in); |
| 5153 | } catch(java.io.IOException e) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5154 | Slog.e("BatteryStats", "Error reading battery statistics", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5155 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5156 | |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5157 | long now = SystemClock.elapsedRealtime(); |
Dianne Hackborn | e8c88e6 | 2011-08-17 19:09:09 -0700 | [diff] [blame] | 5158 | if (USE_OLD_HISTORY) { |
| 5159 | addHistoryRecordLocked(now, HistoryItem.CMD_START); |
| 5160 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5161 | addHistoryBufferLocked(now, HistoryItem.CMD_START); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | public int describeContents() { |
| 5165 | return 0; |
| 5166 | } |
| 5167 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5168 | void readHistory(Parcel in, boolean andOldHistory) { |
| 5169 | final long historyBaseTime = in.readLong(); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5170 | |
| 5171 | mHistoryBuffer.setDataSize(0); |
| 5172 | mHistoryBuffer.setDataPosition(0); |
| 5173 | |
| 5174 | int bufSize = in.readInt(); |
| 5175 | int curPos = in.dataPosition(); |
| 5176 | if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) { |
| 5177 | Slog.w(TAG, "File corrupt: history data buffer too large " + bufSize); |
| 5178 | } else if ((bufSize&~3) != bufSize) { |
| 5179 | Slog.w(TAG, "File corrupt: history data buffer not aligned " + bufSize); |
| 5180 | } else { |
| 5181 | if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize |
| 5182 | + " bytes at " + curPos); |
| 5183 | mHistoryBuffer.appendFrom(in, curPos, bufSize); |
| 5184 | in.setDataPosition(curPos + bufSize); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5185 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5186 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5187 | if (andOldHistory) { |
| 5188 | readOldHistory(in); |
| 5189 | } |
| 5190 | |
| 5191 | if (DEBUG_HISTORY) { |
| 5192 | StringBuilder sb = new StringBuilder(128); |
| 5193 | sb.append("****************** OLD mHistoryBaseTime: "); |
| 5194 | TimeUtils.formatDuration(mHistoryBaseTime, sb); |
| 5195 | Slog.i(TAG, sb.toString()); |
| 5196 | } |
| 5197 | mHistoryBaseTime = historyBaseTime; |
| 5198 | if (DEBUG_HISTORY) { |
| 5199 | StringBuilder sb = new StringBuilder(128); |
| 5200 | sb.append("****************** NEW mHistoryBaseTime: "); |
| 5201 | TimeUtils.formatDuration(mHistoryBaseTime, sb); |
| 5202 | Slog.i(TAG, sb.toString()); |
| 5203 | } |
| 5204 | |
| 5205 | // We are just arbitrarily going to insert 1 minute from the sample of |
| 5206 | // the last run until samples in this run. |
| 5207 | if (mHistoryBaseTime > 0) { |
| 5208 | long oldnow = SystemClock.elapsedRealtime(); |
| 5209 | mHistoryBaseTime = (mHistoryBaseTime - oldnow) + 60*1000; |
| 5210 | if (DEBUG_HISTORY) { |
| 5211 | StringBuilder sb = new StringBuilder(128); |
| 5212 | sb.append("****************** ADJUSTED mHistoryBaseTime: "); |
| 5213 | TimeUtils.formatDuration(mHistoryBaseTime, sb); |
| 5214 | Slog.i(TAG, sb.toString()); |
| 5215 | } |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 5216 | } |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5217 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5218 | |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5219 | void readOldHistory(Parcel in) { |
Dianne Hackborn | e8c88e6 | 2011-08-17 19:09:09 -0700 | [diff] [blame] | 5220 | if (!USE_OLD_HISTORY) { |
| 5221 | return; |
| 5222 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5223 | mHistory = mHistoryEnd = mHistoryCache = null; |
| 5224 | long time; |
Conley Owens | 5e3357f | 2011-05-02 09:59:30 -0700 | [diff] [blame] | 5225 | while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5226 | HistoryItem rec = new HistoryItem(time, in); |
| 5227 | addHistoryRecordLocked(rec); |
| 5228 | } |
| 5229 | } |
| 5230 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5231 | void writeHistory(Parcel out, boolean andOldHistory) { |
| 5232 | if (DEBUG_HISTORY) { |
| 5233 | StringBuilder sb = new StringBuilder(128); |
| 5234 | sb.append("****************** WRITING mHistoryBaseTime: "); |
| 5235 | TimeUtils.formatDuration(mHistoryBaseTime, sb); |
| 5236 | sb.append(" mLastHistoryTime: "); |
| 5237 | TimeUtils.formatDuration(mLastHistoryTime, sb); |
| 5238 | Slog.i(TAG, sb.toString()); |
| 5239 | } |
| 5240 | out.writeLong(mHistoryBaseTime + mLastHistoryTime); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5241 | out.writeInt(mHistoryBuffer.dataSize()); |
| 5242 | if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: " |
| 5243 | + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition()); |
| 5244 | out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize()); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5245 | |
| 5246 | if (andOldHistory) { |
| 5247 | writeOldHistory(out); |
| 5248 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | void writeOldHistory(Parcel out) { |
Dianne Hackborn | e8c88e6 | 2011-08-17 19:09:09 -0700 | [diff] [blame] | 5252 | if (!USE_OLD_HISTORY) { |
| 5253 | return; |
| 5254 | } |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5255 | HistoryItem rec = mHistory; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5256 | while (rec != null) { |
| 5257 | if (rec.time >= 0) rec.writeToParcel(out, 0); |
| 5258 | rec = rec.next; |
| 5259 | } |
| 5260 | out.writeLong(-1); |
| 5261 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5263 | private void readSummaryFromParcel(Parcel in) { |
| 5264 | final int version = in.readInt(); |
| 5265 | if (version != VERSION) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5266 | Slog.w("BatteryStats", "readFromParcel: version got " + version |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5267 | + ", expected " + VERSION + "; erasing old stats"); |
| 5268 | return; |
| 5269 | } |
| 5270 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5271 | readHistory(in, true); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5273 | mStartCount = in.readInt(); |
| 5274 | mBatteryUptime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5275 | mBatteryRealtime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5276 | mUptime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5277 | mRealtime = in.readLong(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5278 | mDischargeUnplugLevel = in.readInt(); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 5279 | mDischargeCurrentLevel = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5280 | mLowDischargeAmountSinceCharge = in.readInt(); |
| 5281 | mHighDischargeAmountSinceCharge = in.readInt(); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 5282 | mDischargeAmountScreenOnSinceCharge = in.readInt(); |
| 5283 | mDischargeAmountScreenOffSinceCharge = in.readInt(); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5285 | mStartCount++; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5287 | mScreenOn = false; |
| 5288 | mScreenOnTimer.readSummaryFromParcelLocked(in); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5289 | for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
| 5290 | mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in); |
| 5291 | } |
| 5292 | mInputEventCounter.readSummaryFromParcelLocked(in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5293 | mPhoneOn = false; |
| 5294 | mPhoneOnTimer.readSummaryFromParcelLocked(in); |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 5295 | for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5296 | mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in); |
| 5297 | } |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 5298 | mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5299 | for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { |
| 5300 | mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in); |
| 5301 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5302 | mWifiOn = false; |
| 5303 | mWifiOnTimer.readSummaryFromParcelLocked(in); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5304 | mGlobalWifiRunning = false; |
| 5305 | mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5306 | mBluetoothOn = false; |
| 5307 | mBluetoothOnTimer.readSummaryFromParcelLocked(in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5308 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 5309 | int NKW = in.readInt(); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5310 | if (NKW > 10000) { |
| 5311 | Slog.w(TAG, "File corrupt: too many kernel wake locks " + NKW); |
| 5312 | return; |
| 5313 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 5314 | for (int ikw = 0; ikw < NKW; ikw++) { |
| 5315 | if (in.readInt() != 0) { |
| 5316 | String kwltName = in.readString(); |
| 5317 | getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in); |
| 5318 | } |
| 5319 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 5320 | |
| 5321 | sNumSpeedSteps = in.readInt(); |
| 5322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5323 | final int NU = in.readInt(); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5324 | if (NU > 10000) { |
| 5325 | Slog.w(TAG, "File corrupt: too many uids " + NU); |
| 5326 | return; |
| 5327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5328 | for (int iu = 0; iu < NU; iu++) { |
| 5329 | int uid = in.readInt(); |
| 5330 | Uid u = new Uid(uid); |
| 5331 | mUidStats.put(uid, u); |
| 5332 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5333 | u.mWifiRunning = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5334 | if (in.readInt() != 0) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5335 | u.mWifiRunningTimer.readSummaryFromParcelLocked(in); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5336 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5337 | u.mFullWifiLockOut = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5338 | if (in.readInt() != 0) { |
| 5339 | u.mFullWifiLockTimer.readSummaryFromParcelLocked(in); |
| 5340 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 5341 | u.mWifiScanStarted = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5342 | if (in.readInt() != 0) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 5343 | u.mWifiScanTimer.readSummaryFromParcelLocked(in); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5344 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 5345 | u.mWifiMulticastEnabled = false; |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5346 | if (in.readInt() != 0) { |
| 5347 | u.mWifiMulticastTimer.readSummaryFromParcelLocked(in); |
| 5348 | } |
| 5349 | u.mAudioTurnedOn = false; |
| 5350 | if (in.readInt() != 0) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5351 | u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5352 | } |
| 5353 | u.mVideoTurnedOn = false; |
| 5354 | if (in.readInt() != 0) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5355 | u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in); |
| 5356 | } |
| 5357 | if (in.readInt() != 0) { |
| 5358 | u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5359 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 5360 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5361 | if (in.readInt() != 0) { |
| 5362 | if (u.mUserActivityCounters == null) { |
| 5363 | u.initUserActivityLocked(); |
| 5364 | } |
| 5365 | for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) { |
| 5366 | u.mUserActivityCounters[i].readSummaryFromParcelLocked(in); |
| 5367 | } |
| 5368 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5370 | int NW = in.readInt(); |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 5371 | if (NW > 100) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5372 | Slog.w(TAG, "File corrupt: too many wake locks " + NW); |
| 5373 | return; |
| 5374 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5375 | for (int iw = 0; iw < NW; iw++) { |
| 5376 | String wlName = in.readString(); |
| 5377 | if (in.readInt() != 0) { |
| 5378 | u.getWakeTimerLocked(wlName, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in); |
| 5379 | } |
| 5380 | if (in.readInt() != 0) { |
| 5381 | u.getWakeTimerLocked(wlName, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in); |
| 5382 | } |
| 5383 | if (in.readInt() != 0) { |
| 5384 | u.getWakeTimerLocked(wlName, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in); |
| 5385 | } |
| 5386 | } |
| 5387 | |
| 5388 | int NP = in.readInt(); |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 5389 | if (NP > 1000) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5390 | Slog.w(TAG, "File corrupt: too many sensors " + NP); |
| 5391 | return; |
| 5392 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5393 | for (int is = 0; is < NP; is++) { |
| 5394 | int seNumber = in.readInt(); |
| 5395 | if (in.readInt() != 0) { |
| 5396 | u.getSensorTimerLocked(seNumber, true) |
| 5397 | .readSummaryFromParcelLocked(in); |
| 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | NP = in.readInt(); |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 5402 | if (NP > 1000) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5403 | Slog.w(TAG, "File corrupt: too many processes " + NP); |
| 5404 | return; |
| 5405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5406 | for (int ip = 0; ip < NP; ip++) { |
| 5407 | String procName = in.readString(); |
| 5408 | Uid.Proc p = u.getProcessStatsLocked(procName); |
| 5409 | p.mUserTime = p.mLoadedUserTime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5410 | p.mSystemTime = p.mLoadedSystemTime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5411 | p.mStarts = p.mLoadedStarts = in.readInt(); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5412 | int NSB = in.readInt(); |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 5413 | if (NSB > 100) { |
| 5414 | Slog.w(TAG, "File corrupt: too many speed bins " + NSB); |
| 5415 | return; |
| 5416 | } |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5417 | p.mSpeedBins = new SamplingCounter[NSB]; |
| 5418 | for (int i=0; i<NSB; i++) { |
| 5419 | if (in.readInt() != 0) { |
| 5420 | p.mSpeedBins[i] = new SamplingCounter(mUnpluggables); |
| 5421 | p.mSpeedBins[i].readSummaryFromParcelLocked(in); |
| 5422 | } |
| 5423 | } |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 5424 | if (!p.readExcessivePowerFromParcelLocked(in)) { |
| 5425 | return; |
| 5426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | NP = in.readInt(); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 5430 | if (NP > 10000) { |
| 5431 | Slog.w(TAG, "File corrupt: too many packages " + NP); |
| 5432 | return; |
| 5433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5434 | for (int ip = 0; ip < NP; ip++) { |
| 5435 | String pkgName = in.readString(); |
| 5436 | Uid.Pkg p = u.getPackageStatsLocked(pkgName); |
| 5437 | p.mWakeups = p.mLoadedWakeups = in.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5438 | final int NS = in.readInt(); |
Dianne Hackborn | 7b9c56f | 2010-12-07 11:14:53 -0800 | [diff] [blame] | 5439 | if (NS > 1000) { |
| 5440 | Slog.w(TAG, "File corrupt: too many services " + NS); |
| 5441 | return; |
| 5442 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5443 | for (int is = 0; is < NS; is++) { |
| 5444 | String servName = in.readString(); |
| 5445 | Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName); |
| 5446 | s.mStartTime = s.mLoadedStartTime = in.readLong(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5447 | s.mStarts = s.mLoadedStarts = in.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5448 | s.mLaunches = s.mLoadedLaunches = in.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5449 | } |
| 5450 | } |
| 5451 | |
| 5452 | u.mLoadedTcpBytesReceived = in.readLong(); |
| 5453 | u.mLoadedTcpBytesSent = in.readLong(); |
| 5454 | } |
| 5455 | } |
| 5456 | |
| 5457 | /** |
| 5458 | * Writes a summary of the statistics to a Parcel, in a format suitable to be written to |
| 5459 | * disk. This format does not allow a lossless round-trip. |
| 5460 | * |
| 5461 | * @param out the Parcel to be written to. |
| 5462 | */ |
| 5463 | public void writeSummaryToParcel(Parcel out) { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5464 | // Need to update with current kernel wake lock counts. |
| 5465 | updateKernelWakelocksLocked(); |
| 5466 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5467 | final long NOW_SYS = SystemClock.uptimeMillis() * 1000; |
| 5468 | final long NOWREAL_SYS = SystemClock.elapsedRealtime() * 1000; |
| 5469 | final long NOW = getBatteryUptimeLocked(NOW_SYS); |
| 5470 | final long NOWREAL = getBatteryRealtimeLocked(NOWREAL_SYS); |
| 5471 | |
| 5472 | out.writeInt(VERSION); |
| 5473 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5474 | writeHistory(out, true); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5476 | out.writeInt(mStartCount); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5477 | out.writeLong(computeBatteryUptime(NOW_SYS, STATS_SINCE_CHARGED)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5478 | out.writeLong(computeBatteryRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5479 | out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5480 | out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED)); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5481 | out.writeInt(mDischargeUnplugLevel); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 5482 | out.writeInt(mDischargeCurrentLevel); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 5483 | out.writeInt(getLowDischargeAmountSinceCharge()); |
| 5484 | out.writeInt(getHighDischargeAmountSinceCharge()); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 5485 | out.writeInt(getDischargeAmountScreenOnSinceCharge()); |
| 5486 | out.writeInt(getDischargeAmountScreenOffSinceCharge()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5487 | |
| 5488 | mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5489 | for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
| 5490 | mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL); |
| 5491 | } |
| 5492 | mInputEventCounter.writeSummaryFromParcelLocked(out); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5493 | mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 5494 | for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5495 | mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL); |
| 5496 | } |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 5497 | mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5498 | for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { |
| 5499 | mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL); |
| 5500 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5501 | mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5502 | mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5503 | mBluetoothOnTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5504 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 5505 | out.writeInt(mKernelWakelockStats.size()); |
| 5506 | for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) { |
| 5507 | Timer kwlt = ent.getValue(); |
| 5508 | if (kwlt != null) { |
| 5509 | out.writeInt(1); |
| 5510 | out.writeString(ent.getKey()); |
| 5511 | ent.getValue().writeSummaryFromParcelLocked(out, NOWREAL); |
| 5512 | } else { |
| 5513 | out.writeInt(0); |
| 5514 | } |
| 5515 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5516 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 5517 | out.writeInt(sNumSpeedSteps); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5518 | final int NU = mUidStats.size(); |
| 5519 | out.writeInt(NU); |
| 5520 | for (int iu = 0; iu < NU; iu++) { |
| 5521 | out.writeInt(mUidStats.keyAt(iu)); |
| 5522 | Uid u = mUidStats.valueAt(iu); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5523 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5524 | if (u.mWifiRunningTimer != null) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5525 | out.writeInt(1); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5526 | u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5527 | } else { |
| 5528 | out.writeInt(0); |
| 5529 | } |
| 5530 | if (u.mFullWifiLockTimer != null) { |
| 5531 | out.writeInt(1); |
| 5532 | u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5533 | } else { |
| 5534 | out.writeInt(0); |
| 5535 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 5536 | if (u.mWifiScanTimer != null) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5537 | out.writeInt(1); |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 5538 | u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5539 | } else { |
| 5540 | out.writeInt(0); |
| 5541 | } |
| 5542 | if (u.mWifiMulticastTimer != null) { |
| 5543 | out.writeInt(1); |
| 5544 | u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5545 | } else { |
| 5546 | out.writeInt(0); |
| 5547 | } |
| 5548 | if (u.mAudioTurnedOnTimer != null) { |
| 5549 | out.writeInt(1); |
| 5550 | u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5551 | } else { |
| 5552 | out.writeInt(0); |
| 5553 | } |
| 5554 | if (u.mVideoTurnedOnTimer != null) { |
| 5555 | out.writeInt(1); |
| 5556 | u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5557 | } else { |
| 5558 | out.writeInt(0); |
| 5559 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5560 | if (u.mVibratorOnTimer != null) { |
| 5561 | out.writeInt(1); |
| 5562 | u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5563 | } else { |
| 5564 | out.writeInt(0); |
| 5565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5566 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5567 | if (u.mUserActivityCounters == null) { |
| 5568 | out.writeInt(0); |
| 5569 | } else { |
| 5570 | out.writeInt(1); |
| 5571 | for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) { |
| 5572 | u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out); |
| 5573 | } |
| 5574 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5576 | int NW = u.mWakelockStats.size(); |
| 5577 | out.writeInt(NW); |
| 5578 | if (NW > 0) { |
| 5579 | for (Map.Entry<String, BatteryStatsImpl.Uid.Wakelock> ent |
| 5580 | : u.mWakelockStats.entrySet()) { |
| 5581 | out.writeString(ent.getKey()); |
| 5582 | Uid.Wakelock wl = ent.getValue(); |
| 5583 | if (wl.mTimerFull != null) { |
| 5584 | out.writeInt(1); |
| 5585 | wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5586 | } else { |
| 5587 | out.writeInt(0); |
| 5588 | } |
| 5589 | if (wl.mTimerPartial != null) { |
| 5590 | out.writeInt(1); |
| 5591 | wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5592 | } else { |
| 5593 | out.writeInt(0); |
| 5594 | } |
| 5595 | if (wl.mTimerWindow != null) { |
| 5596 | out.writeInt(1); |
| 5597 | wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5598 | } else { |
| 5599 | out.writeInt(0); |
| 5600 | } |
| 5601 | } |
| 5602 | } |
| 5603 | |
| 5604 | int NSE = u.mSensorStats.size(); |
| 5605 | out.writeInt(NSE); |
| 5606 | if (NSE > 0) { |
| 5607 | for (Map.Entry<Integer, BatteryStatsImpl.Uid.Sensor> ent |
| 5608 | : u.mSensorStats.entrySet()) { |
| 5609 | out.writeInt(ent.getKey()); |
| 5610 | Uid.Sensor se = ent.getValue(); |
| 5611 | if (se.mTimer != null) { |
| 5612 | out.writeInt(1); |
| 5613 | se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL); |
| 5614 | } else { |
| 5615 | out.writeInt(0); |
| 5616 | } |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | int NP = u.mProcessStats.size(); |
| 5621 | out.writeInt(NP); |
| 5622 | if (NP > 0) { |
| 5623 | for (Map.Entry<String, BatteryStatsImpl.Uid.Proc> ent |
| 5624 | : u.mProcessStats.entrySet()) { |
| 5625 | out.writeString(ent.getKey()); |
| 5626 | Uid.Proc ps = ent.getValue(); |
| 5627 | out.writeLong(ps.mUserTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5628 | out.writeLong(ps.mSystemTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5629 | out.writeInt(ps.mStarts); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5630 | final int N = ps.mSpeedBins.length; |
| 5631 | out.writeInt(N); |
| 5632 | for (int i=0; i<N; i++) { |
| 5633 | if (ps.mSpeedBins[i] != null) { |
| 5634 | out.writeInt(1); |
| 5635 | ps.mSpeedBins[i].writeSummaryFromParcelLocked(out); |
| 5636 | } else { |
| 5637 | out.writeInt(0); |
| 5638 | } |
| 5639 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 5640 | ps.writeExcessivePowerToParcelLocked(out); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | NP = u.mPackageStats.size(); |
| 5645 | out.writeInt(NP); |
| 5646 | if (NP > 0) { |
| 5647 | for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent |
| 5648 | : u.mPackageStats.entrySet()) { |
| 5649 | out.writeString(ent.getKey()); |
| 5650 | Uid.Pkg ps = ent.getValue(); |
| 5651 | out.writeInt(ps.mWakeups); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5652 | final int NS = ps.mServiceStats.size(); |
| 5653 | out.writeInt(NS); |
| 5654 | if (NS > 0) { |
| 5655 | for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg.Serv> sent |
| 5656 | : ps.mServiceStats.entrySet()) { |
| 5657 | out.writeString(sent.getKey()); |
| 5658 | BatteryStatsImpl.Uid.Pkg.Serv ss = sent.getValue(); |
| 5659 | long time = ss.getStartTimeToNowLocked(NOW); |
| 5660 | out.writeLong(time); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5661 | out.writeInt(ss.mStarts); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5662 | out.writeInt(ss.mLaunches); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5663 | } |
| 5664 | } |
| 5665 | } |
| 5666 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5667 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5668 | out.writeLong(u.getTcpBytesReceived(STATS_SINCE_CHARGED)); |
| 5669 | out.writeLong(u.getTcpBytesSent(STATS_SINCE_CHARGED)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5670 | } |
| 5671 | } |
| 5672 | |
| 5673 | public void readFromParcel(Parcel in) { |
| 5674 | readFromParcelLocked(in); |
| 5675 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5677 | void readFromParcelLocked(Parcel in) { |
| 5678 | int magic = in.readInt(); |
| 5679 | if (magic != MAGIC) { |
| 5680 | throw new ParcelFormatException("Bad magic number"); |
| 5681 | } |
| 5682 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5683 | readHistory(in, false); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5684 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5685 | mStartCount = in.readInt(); |
| 5686 | mBatteryUptime = in.readLong(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5687 | mBatteryLastUptime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5688 | mBatteryRealtime = in.readLong(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5689 | mBatteryLastRealtime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5690 | mScreenOn = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5691 | mScreenOnTimer = new StopwatchTimer(null, -1, null, mUnpluggables, in); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5692 | for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5693 | mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, |
| 5694 | null, mUnpluggables, in); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5695 | } |
| 5696 | mInputEventCounter = new Counter(mUnpluggables, in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5697 | mPhoneOn = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5698 | mPhoneOnTimer = new StopwatchTimer(null, -2, null, mUnpluggables, in); |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 5699 | for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5700 | mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i, |
| 5701 | null, mUnpluggables, in); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5702 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5703 | mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mUnpluggables, in); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5704 | for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5705 | mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i, |
| 5706 | null, mUnpluggables, in); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5707 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5708 | mWifiOn = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5709 | mWifiOnTimer = new StopwatchTimer(null, -2, null, mUnpluggables, in); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5710 | mGlobalWifiRunning = false; |
| 5711 | mGlobalWifiRunningTimer = new StopwatchTimer(null, -2, null, mUnpluggables, in); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5712 | mBluetoothOn = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 5713 | mBluetoothOnTimer = new StopwatchTimer(null, -2, null, mUnpluggables, in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5714 | mUptime = in.readLong(); |
| 5715 | mUptimeStart = in.readLong(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5716 | mLastUptime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5717 | mRealtime = in.readLong(); |
| 5718 | mRealtimeStart = in.readLong(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5719 | mLastRealtime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5720 | mOnBattery = in.readInt() != 0; |
| 5721 | mOnBatteryInternal = false; // we are no longer really running. |
| 5722 | mTrackBatteryPastUptime = in.readLong(); |
| 5723 | mTrackBatteryUptimeStart = in.readLong(); |
| 5724 | mTrackBatteryPastRealtime = in.readLong(); |
| 5725 | mTrackBatteryRealtimeStart = in.readLong(); |
| 5726 | mUnpluggedBatteryUptime = in.readLong(); |
| 5727 | mUnpluggedBatteryRealtime = in.readLong(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5728 | mDischargeUnplugLevel = in.readInt(); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 5729 | mDischargeCurrentLevel = in.readInt(); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5730 | mLowDischargeAmountSinceCharge = in.readInt(); |
| 5731 | mHighDischargeAmountSinceCharge = in.readInt(); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 5732 | mDischargeAmountScreenOn = in.readInt(); |
| 5733 | mDischargeAmountScreenOnSinceCharge = in.readInt(); |
| 5734 | mDischargeAmountScreenOff = in.readInt(); |
| 5735 | mDischargeAmountScreenOffSinceCharge = in.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5736 | mLastWriteTime = in.readLong(); |
| 5737 | |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 5738 | mMobileDataRx[STATS_LAST] = in.readLong(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5739 | mMobileDataRx[STATS_SINCE_UNPLUGGED] = -1; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 5740 | mMobileDataTx[STATS_LAST] = in.readLong(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5741 | mMobileDataTx[STATS_SINCE_UNPLUGGED] = -1; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 5742 | mTotalDataRx[STATS_LAST] = in.readLong(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5743 | mTotalDataRx[STATS_SINCE_UNPLUGGED] = -1; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 5744 | mTotalDataTx[STATS_LAST] = in.readLong(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5745 | mTotalDataTx[STATS_SINCE_UNPLUGGED] = -1; |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 5746 | |
| 5747 | mRadioDataUptime = in.readLong(); |
| 5748 | mRadioDataStart = -1; |
| 5749 | |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 5750 | mBluetoothPingCount = in.readInt(); |
| 5751 | mBluetoothPingStart = -1; |
| 5752 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 5753 | mKernelWakelockStats.clear(); |
| 5754 | int NKW = in.readInt(); |
| 5755 | for (int ikw = 0; ikw < NKW; ikw++) { |
| 5756 | if (in.readInt() != 0) { |
| 5757 | String wakelockName = in.readString(); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 5758 | in.readInt(); // Extra 0/1 written by Timer.writeTimerToParcel |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 5759 | SamplingTimer kwlt = new SamplingTimer(mUnpluggables, mOnBattery, in); |
| 5760 | mKernelWakelockStats.put(wakelockName, kwlt); |
| 5761 | } |
| 5762 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5763 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5764 | mPartialTimers.clear(); |
| 5765 | mFullTimers.clear(); |
| 5766 | mWindowTimers.clear(); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5767 | mWifiRunningTimers.clear(); |
| 5768 | mFullWifiLockTimers.clear(); |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 5769 | mWifiScanTimers.clear(); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5770 | mWifiMulticastTimers.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5771 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 5772 | sNumSpeedSteps = in.readInt(); |
| 5773 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5774 | int numUids = in.readInt(); |
| 5775 | mUidStats.clear(); |
| 5776 | for (int i = 0; i < numUids; i++) { |
| 5777 | int uid = in.readInt(); |
| 5778 | Uid u = new Uid(uid); |
| 5779 | u.readFromParcelLocked(mUnpluggables, in); |
| 5780 | mUidStats.append(uid, u); |
| 5781 | } |
| 5782 | } |
| 5783 | |
| 5784 | public void writeToParcel(Parcel out, int flags) { |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5785 | writeToParcelLocked(out, true, flags); |
| 5786 | } |
| 5787 | |
| 5788 | public void writeToParcelWithoutUids(Parcel out, int flags) { |
| 5789 | writeToParcelLocked(out, false, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5790 | } |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5791 | |
| 5792 | @SuppressWarnings("unused") |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5793 | void writeToParcelLocked(Parcel out, boolean inclUids, int flags) { |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5794 | // Need to update with current kernel wake lock counts. |
| 5795 | updateKernelWakelocksLocked(); |
| 5796 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5797 | final long uSecUptime = SystemClock.uptimeMillis() * 1000; |
| 5798 | final long uSecRealtime = SystemClock.elapsedRealtime() * 1000; |
| 5799 | final long batteryUptime = getBatteryUptimeLocked(uSecUptime); |
| 5800 | final long batteryRealtime = getBatteryRealtimeLocked(uSecRealtime); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5801 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5802 | out.writeInt(MAGIC); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5803 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 5804 | writeHistory(out, false); |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5805 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5806 | out.writeInt(mStartCount); |
| 5807 | out.writeLong(mBatteryUptime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5808 | out.writeLong(mBatteryRealtime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5809 | mScreenOnTimer.writeToParcel(out, batteryRealtime); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5810 | for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
| 5811 | mScreenBrightnessTimer[i].writeToParcel(out, batteryRealtime); |
| 5812 | } |
| 5813 | mInputEventCounter.writeToParcel(out); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5814 | mPhoneOnTimer.writeToParcel(out, batteryRealtime); |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 5815 | for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5816 | mPhoneSignalStrengthsTimer[i].writeToParcel(out, batteryRealtime); |
| 5817 | } |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 5818 | mPhoneSignalScanningTimer.writeToParcel(out, batteryRealtime); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5819 | for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { |
| 5820 | mPhoneDataConnectionsTimer[i].writeToParcel(out, batteryRealtime); |
| 5821 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5822 | mWifiOnTimer.writeToParcel(out, batteryRealtime); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5823 | mGlobalWifiRunningTimer.writeToParcel(out, batteryRealtime); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5824 | mBluetoothOnTimer.writeToParcel(out, batteryRealtime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5825 | out.writeLong(mUptime); |
| 5826 | out.writeLong(mUptimeStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5827 | out.writeLong(mRealtime); |
| 5828 | out.writeLong(mRealtimeStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5829 | out.writeInt(mOnBattery ? 1 : 0); |
| 5830 | out.writeLong(batteryUptime); |
| 5831 | out.writeLong(mTrackBatteryUptimeStart); |
| 5832 | out.writeLong(batteryRealtime); |
| 5833 | out.writeLong(mTrackBatteryRealtimeStart); |
| 5834 | out.writeLong(mUnpluggedBatteryUptime); |
| 5835 | out.writeLong(mUnpluggedBatteryRealtime); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5836 | out.writeInt(mDischargeUnplugLevel); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 5837 | out.writeInt(mDischargeCurrentLevel); |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5838 | out.writeInt(mLowDischargeAmountSinceCharge); |
| 5839 | out.writeInt(mHighDischargeAmountSinceCharge); |
Dianne Hackborn | c1b40e3 | 2011-01-05 18:27:40 -0800 | [diff] [blame] | 5840 | out.writeInt(mDischargeAmountScreenOn); |
| 5841 | out.writeInt(mDischargeAmountScreenOnSinceCharge); |
| 5842 | out.writeInt(mDischargeAmountScreenOff); |
| 5843 | out.writeInt(mDischargeAmountScreenOffSinceCharge); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5844 | out.writeLong(mLastWriteTime); |
| 5845 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 5846 | out.writeLong(getMobileTcpBytesReceived(STATS_SINCE_UNPLUGGED)); |
| 5847 | out.writeLong(getMobileTcpBytesSent(STATS_SINCE_UNPLUGGED)); |
| 5848 | out.writeLong(getTotalTcpBytesReceived(STATS_SINCE_UNPLUGGED)); |
| 5849 | out.writeLong(getTotalTcpBytesSent(STATS_SINCE_UNPLUGGED)); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 5850 | |
| 5851 | // Write radio uptime for data |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 5852 | out.writeLong(getRadioDataUptime()); |
| 5853 | |
| 5854 | out.writeInt(getBluetoothPingCount()); |
Amith Yamasani | 3718aaa | 2009-06-09 06:32:35 -0700 | [diff] [blame] | 5855 | |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5856 | if (inclUids) { |
| 5857 | out.writeInt(mKernelWakelockStats.size()); |
| 5858 | for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) { |
| 5859 | SamplingTimer kwlt = ent.getValue(); |
| 5860 | if (kwlt != null) { |
| 5861 | out.writeInt(1); |
| 5862 | out.writeString(ent.getKey()); |
| 5863 | Timer.writeTimerToParcel(out, kwlt, batteryRealtime); |
| 5864 | } else { |
| 5865 | out.writeInt(0); |
| 5866 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 5867 | } |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5868 | } else { |
| 5869 | out.writeInt(0); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 5870 | } |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 5871 | |
| 5872 | out.writeInt(sNumSpeedSteps); |
| 5873 | |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5874 | if (inclUids) { |
| 5875 | int size = mUidStats.size(); |
| 5876 | out.writeInt(size); |
| 5877 | for (int i = 0; i < size; i++) { |
| 5878 | out.writeInt(mUidStats.keyAt(i)); |
| 5879 | Uid uid = mUidStats.valueAt(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5880 | |
Dianne Hackborn | 3bee5af8 | 2010-07-23 00:22:04 -0700 | [diff] [blame] | 5881 | uid.writeToParcelLocked(out, batteryRealtime); |
| 5882 | } |
| 5883 | } else { |
| 5884 | out.writeInt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5885 | } |
| 5886 | } |
| 5887 | |
| 5888 | public static final Parcelable.Creator<BatteryStatsImpl> CREATOR = |
| 5889 | new Parcelable.Creator<BatteryStatsImpl>() { |
| 5890 | public BatteryStatsImpl createFromParcel(Parcel in) { |
| 5891 | return new BatteryStatsImpl(in); |
| 5892 | } |
| 5893 | |
| 5894 | public BatteryStatsImpl[] newArray(int size) { |
| 5895 | return new BatteryStatsImpl[size]; |
| 5896 | } |
| 5897 | }; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 5898 | |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 5899 | public void prepareForDumpLocked() { |
| 5900 | // Need to retrieve current kernel wake lock stats before printing. |
| 5901 | updateKernelWakelocksLocked(); |
| 5902 | } |
| 5903 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5904 | public void dumpLocked(PrintWriter pw) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5905 | if (DEBUG) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5906 | Printer pr = new PrintWriterPrinter(pw); |
| 5907 | pr.println("*** Screen timer:"); |
| 5908 | mScreenOnTimer.logState(pr, " "); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5909 | for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5910 | pr.println("*** Screen brightness #" + i + ":"); |
| 5911 | mScreenBrightnessTimer[i].logState(pr, " "); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 5912 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5913 | pr.println("*** Input event counter:"); |
| 5914 | mInputEventCounter.logState(pr, " "); |
| 5915 | pr.println("*** Phone timer:"); |
| 5916 | mPhoneOnTimer.logState(pr, " "); |
Wink Saville | 5284090 | 2011-02-18 12:40:47 -0800 | [diff] [blame] | 5917 | for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5918 | pr.println("*** Signal strength #" + i + ":"); |
| 5919 | mPhoneSignalStrengthsTimer[i].logState(pr, " "); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5920 | } |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 5921 | pr.println("*** Signal scanning :"); |
| 5922 | mPhoneSignalScanningTimer.logState(pr, " "); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5923 | for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5924 | pr.println("*** Data connection type #" + i + ":"); |
| 5925 | mPhoneDataConnectionsTimer[i].logState(pr, " "); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 5926 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5927 | pr.println("*** Wifi timer:"); |
| 5928 | mWifiOnTimer.logState(pr, " "); |
| 5929 | pr.println("*** WifiRunning timer:"); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 5930 | mGlobalWifiRunningTimer.logState(pr, " "); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5931 | pr.println("*** Bluetooth timer:"); |
| 5932 | mBluetoothOnTimer.logState(pr, " "); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 5933 | pr.println("*** Mobile ifaces:"); |
| 5934 | pr.println(mMobileIfaces.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5935 | } |
| 5936 | super.dumpLocked(pw); |
| 5937 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 5938 | |
| 5939 | private NetworkStats mNetworkSummaryCache; |
| 5940 | private NetworkStats mNetworkDetailCache; |
| 5941 | |
| 5942 | private NetworkStats getNetworkStatsSummary() { |
| 5943 | // NOTE: calls from BatteryStatsService already hold this lock |
| 5944 | synchronized (this) { |
| 5945 | if (mNetworkSummaryCache == null |
| 5946 | || mNetworkSummaryCache.getElapsedRealtimeAge() > SECOND_IN_MILLIS) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 5947 | mNetworkSummaryCache = null; |
| 5948 | |
| 5949 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 5950 | try { |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 5951 | mNetworkSummaryCache = mNetworkStatsFactory.readNetworkStatsSummaryDev(); |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 5952 | } catch (IOException e) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 5953 | Log.wtf(TAG, "problem reading network stats", e); |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | if (mNetworkSummaryCache == null) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 5958 | mNetworkSummaryCache = new NetworkStats(SystemClock.elapsedRealtime(), 0); |
| 5959 | } |
| 5960 | } |
| 5961 | return mNetworkSummaryCache; |
| 5962 | } |
| 5963 | } |
| 5964 | |
| 5965 | private NetworkStats getNetworkStatsDetailGroupedByUid() { |
| 5966 | // NOTE: calls from BatteryStatsService already hold this lock |
| 5967 | synchronized (this) { |
| 5968 | if (mNetworkDetailCache == null |
| 5969 | || mNetworkDetailCache.getElapsedRealtimeAge() > SECOND_IN_MILLIS) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 5970 | mNetworkDetailCache = null; |
| 5971 | |
| 5972 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 5973 | try { |
| 5974 | mNetworkDetailCache = mNetworkStatsFactory |
| 5975 | .readNetworkStatsDetail().groupedByUid(); |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 5976 | } catch (IOException e) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 5977 | Log.wtf(TAG, "problem reading network stats", e); |
| 5978 | } |
| 5979 | } |
| 5980 | |
| 5981 | if (mNetworkDetailCache == null) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 5982 | mNetworkDetailCache = new NetworkStats(SystemClock.elapsedRealtime(), 0); |
| 5983 | } |
| 5984 | } |
| 5985 | return mNetworkDetailCache; |
| 5986 | } |
| 5987 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5988 | } |