Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | package com.android.internal.os; |
| 17 | |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 18 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 19 | import android.os.BatteryStats.Uid; |
| 20 | |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 21 | import java.util.List; |
| 22 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Contains power usage of an application, system service, or hardware type. |
| 25 | */ |
| 26 | public class BatterySipper implements Comparable<BatterySipper> { |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 27 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 28 | public int userId; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 29 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 30 | public Uid uidObj; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 31 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 32 | public DrainType drainType; |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 33 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 34 | /** |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 35 | * Smeared power from screen usage. |
| 36 | * We split the screen usage power and smear them among apps, based on activity time. |
| 37 | */ |
| 38 | public double screenPowerMah; |
| 39 | |
| 40 | /** |
| 41 | * Smeared power using proportional method. |
| 42 | * |
| 43 | * we smear power usage from hidden sippers to all apps proportionally.(except for screen usage) |
| 44 | * |
| 45 | * @see BatteryStatsHelper#shouldHideSipper(BatterySipper) |
| 46 | * @see BatteryStatsHelper#removeHiddenBatterySippers(List) |
| 47 | */ |
| 48 | public double proportionalSmearMah; |
| 49 | |
| 50 | /** |
| 51 | * Total power that adding the smeared power. |
| 52 | * |
| 53 | * @see #sumPower() |
| 54 | */ |
| 55 | public double totalSmearedPowerMah; |
| 56 | |
| 57 | /** |
| 58 | * Total power before smearing |
| 59 | */ |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 60 | @UnsupportedAppUsage |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 61 | public double totalPowerMah; |
| 62 | |
| 63 | /** |
| 64 | * Whether we should hide this sipper |
| 65 | * |
| 66 | * @see BatteryStatsHelper#shouldHideSipper(BatterySipper) |
| 67 | */ |
| 68 | public boolean shouldHide; |
| 69 | |
| 70 | /** |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 71 | * Generic usage time in milliseconds. |
| 72 | */ |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 73 | @UnsupportedAppUsage |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 74 | public long usageTimeMs; |
| 75 | |
| 76 | /** |
| 77 | * Generic power usage in mAh. |
| 78 | */ |
| 79 | public double usagePowerMah; |
| 80 | |
| 81 | // Subsystem usage times. |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 82 | public long audioTimeMs; |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 83 | public long bluetoothRunningTimeMs; |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 84 | public long cameraTimeMs; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 85 | @UnsupportedAppUsage |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 86 | public long cpuFgTimeMs; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 87 | @UnsupportedAppUsage |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 88 | public long cpuTimeMs; |
| 89 | public long flashlightTimeMs; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 90 | @UnsupportedAppUsage |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 91 | public long gpsTimeMs; |
| 92 | public long videoTimeMs; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 93 | @UnsupportedAppUsage |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 94 | public long wakeLockTimeMs; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 95 | @UnsupportedAppUsage |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 96 | public long wifiRunningTimeMs; |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 97 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 98 | public long mobileRxPackets; |
| 99 | public long mobileTxPackets; |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 100 | public long mobileActive; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 101 | public int mobileActiveCount; |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 102 | public double mobilemspp; // milliseconds per packet |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 103 | public long wifiRxPackets; |
| 104 | public long wifiTxPackets; |
| 105 | public long mobileRxBytes; |
| 106 | public long mobileTxBytes; |
| 107 | public long wifiRxBytes; |
| 108 | public long wifiTxBytes; |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 109 | public long btRxBytes; |
| 110 | public long btTxBytes; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 111 | public double percent; |
| 112 | public double noCoveragePercent; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 113 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 114 | public String[] mPackages; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 115 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 116 | public String packageWithHighestDrain; |
| 117 | |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 118 | // Measured in mAh (milli-ampere per hour). |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 119 | // These are included when summed. |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 120 | public double audioPowerMah; |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 121 | public double bluetoothPowerMah; |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 122 | public double cameraPowerMah; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 123 | @UnsupportedAppUsage |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 124 | public double cpuPowerMah; |
| 125 | public double flashlightPowerMah; |
| 126 | public double gpsPowerMah; |
| 127 | public double mobileRadioPowerMah; |
| 128 | public double sensorPowerMah; |
| 129 | public double videoPowerMah; |
| 130 | public double wakeLockPowerMah; |
| 131 | public double wifiPowerMah; |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 132 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 133 | public enum DrainType { |
Mike Ma | eb0d8a7 | 2018-02-27 16:41:54 -0800 | [diff] [blame] | 134 | AMBIENT_DISPLAY, |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 135 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 136 | APP, |
Mike Ma | eb0d8a7 | 2018-02-27 16:41:54 -0800 | [diff] [blame] | 137 | BLUETOOTH, |
James Carr | 2dd7e5e | 2016-07-20 18:48:39 -0700 | [diff] [blame] | 138 | CAMERA, |
Mike Ma | eb0d8a7 | 2018-02-27 16:41:54 -0800 | [diff] [blame] | 139 | CELL, |
| 140 | FLASHLIGHT, |
| 141 | IDLE, |
| 142 | MEMORY, |
| 143 | OVERCOUNTED, |
| 144 | PHONE, |
| 145 | SCREEN, |
| 146 | UNACCOUNTED, |
| 147 | USER, |
| 148 | WIFI, |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 151 | @UnsupportedAppUsage |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 152 | public BatterySipper(DrainType drainType, Uid uid, double value) { |
| 153 | this.totalPowerMah = value; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 154 | this.drainType = drainType; |
| 155 | uidObj = uid; |
| 156 | } |
| 157 | |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 158 | public void computeMobilemspp() { |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 159 | long packets = mobileRxPackets + mobileTxPackets; |
| 160 | mobilemspp = packets > 0 ? (mobileActive / (double) packets) : 0; |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 163 | @Override |
| 164 | public int compareTo(BatterySipper other) { |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 165 | // Over-counted always goes to the bottom. |
| 166 | if (drainType != other.drainType) { |
| 167 | if (drainType == DrainType.OVERCOUNTED) { |
| 168 | // This is "larger" |
| 169 | return 1; |
| 170 | } else if (other.drainType == DrainType.OVERCOUNTED) { |
| 171 | return -1; |
| 172 | } |
| 173 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 174 | // Return the flipped value because we want the items in descending order |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 175 | return Double.compare(other.totalPowerMah, totalPowerMah); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets a list of packages associated with the current user |
| 180 | */ |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 181 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 182 | public String[] getPackages() { |
| 183 | return mPackages; |
| 184 | } |
| 185 | |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 186 | @UnsupportedAppUsage |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 187 | public int getUid() { |
| 188 | // Bail out if the current sipper is not an App sipper. |
| 189 | if (uidObj == null) { |
| 190 | return 0; |
| 191 | } |
| 192 | return uidObj.getUid(); |
| 193 | } |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Add stats from other to this BatterySipper. |
| 197 | */ |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 198 | @UnsupportedAppUsage |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 199 | public void add(BatterySipper other) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 200 | totalPowerMah += other.totalPowerMah; |
| 201 | usageTimeMs += other.usageTimeMs; |
| 202 | usagePowerMah += other.usagePowerMah; |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 203 | audioTimeMs += other.audioTimeMs; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 204 | cpuTimeMs += other.cpuTimeMs; |
| 205 | gpsTimeMs += other.gpsTimeMs; |
| 206 | wifiRunningTimeMs += other.wifiRunningTimeMs; |
| 207 | cpuFgTimeMs += other.cpuFgTimeMs; |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 208 | videoTimeMs += other.videoTimeMs; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 209 | wakeLockTimeMs += other.wakeLockTimeMs; |
Ruben Brunk | 5b1308f | 2015-06-03 18:49:27 -0700 | [diff] [blame] | 210 | cameraTimeMs += other.cameraTimeMs; |
| 211 | flashlightTimeMs += other.flashlightTimeMs; |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 212 | bluetoothRunningTimeMs += other.bluetoothRunningTimeMs; |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 213 | mobileRxPackets += other.mobileRxPackets; |
| 214 | mobileTxPackets += other.mobileTxPackets; |
| 215 | mobileActive += other.mobileActive; |
| 216 | mobileActiveCount += other.mobileActiveCount; |
| 217 | wifiRxPackets += other.wifiRxPackets; |
| 218 | wifiTxPackets += other.wifiTxPackets; |
| 219 | mobileRxBytes += other.mobileRxBytes; |
| 220 | mobileTxBytes += other.mobileTxBytes; |
| 221 | wifiRxBytes += other.wifiRxBytes; |
| 222 | wifiTxBytes += other.wifiTxBytes; |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 223 | btRxBytes += other.btRxBytes; |
| 224 | btTxBytes += other.btTxBytes; |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 225 | audioPowerMah += other.audioPowerMah; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 226 | wifiPowerMah += other.wifiPowerMah; |
| 227 | gpsPowerMah += other.gpsPowerMah; |
| 228 | cpuPowerMah += other.cpuPowerMah; |
| 229 | sensorPowerMah += other.sensorPowerMah; |
| 230 | mobileRadioPowerMah += other.mobileRadioPowerMah; |
| 231 | wakeLockPowerMah += other.wakeLockPowerMah; |
Ruben Brunk | 5b1308f | 2015-06-03 18:49:27 -0700 | [diff] [blame] | 232 | cameraPowerMah += other.cameraPowerMah; |
| 233 | flashlightPowerMah += other.flashlightPowerMah; |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 234 | bluetoothPowerMah += other.bluetoothPowerMah; |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 235 | screenPowerMah += other.screenPowerMah; |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 236 | videoPowerMah += other.videoPowerMah; |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 237 | proportionalSmearMah += other.proportionalSmearMah; |
| 238 | totalSmearedPowerMah += other.totalSmearedPowerMah; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sum all the powers and store the value into `value`. |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 243 | * Also sum the {@code smearedTotalPowerMah} by adding smeared powerMah. |
| 244 | * |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 245 | * @return the sum of all the power in this BatterySipper. |
| 246 | */ |
| 247 | public double sumPower() { |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 248 | totalPowerMah = usagePowerMah + wifiPowerMah + gpsPowerMah + cpuPowerMah + |
Ruben Brunk | 5b1308f | 2015-06-03 18:49:27 -0700 | [diff] [blame] | 249 | sensorPowerMah + mobileRadioPowerMah + wakeLockPowerMah + cameraPowerMah + |
Mike Ma | 07305c0 | 2018-03-02 16:57:31 -0800 | [diff] [blame] | 250 | flashlightPowerMah + bluetoothPowerMah + audioPowerMah + videoPowerMah; |
jackqdyulei | 1424c1d | 2017-06-05 16:15:54 -0700 | [diff] [blame] | 251 | totalSmearedPowerMah = totalPowerMah + screenPowerMah + proportionalSmearMah; |
| 252 | |
| 253 | return totalPowerMah; |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 254 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 255 | } |