blob: 0baf73cc024aa21b375edd44cf009867704cd518 [file] [log] [blame]
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001/*
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 */
16package com.android.internal.os;
17
Mathew Inwoodc185f082018-08-20 14:28:54 +010018import android.annotation.UnsupportedAppUsage;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080019import android.os.BatteryStats.Uid;
20
jackqdyulei1424c1d2017-06-05 16:15:54 -070021import java.util.List;
22
Dianne Hackborna7c837f2014-01-15 16:20:44 -080023/**
24 * Contains power usage of an application, system service, or hardware type.
25 */
26public class BatterySipper implements Comparable<BatterySipper> {
Mathew Inwoodc185f082018-08-20 14:28:54 +010027 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028 public int userId;
Mathew Inwoodc185f082018-08-20 14:28:54 +010029 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -080030 public Uid uidObj;
Mathew Inwoodc185f082018-08-20 14:28:54 +010031 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -080032 public DrainType drainType;
Adam Lesinski33dac552015-03-09 15:24:48 -070033
Adam Lesinskie08af192015-03-25 16:42:59 -070034 /**
jackqdyulei1424c1d2017-06-05 16:15:54 -070035 * 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 Inwoodc185f082018-08-20 14:28:54 +010060 @UnsupportedAppUsage
jackqdyulei1424c1d2017-06-05 16:15:54 -070061 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 Lesinskie08af192015-03-25 16:42:59 -070071 * Generic usage time in milliseconds.
72 */
Mathew Inwoodc185f082018-08-20 14:28:54 +010073 @UnsupportedAppUsage
Adam Lesinskie08af192015-03-25 16:42:59 -070074 public long usageTimeMs;
75
76 /**
77 * Generic power usage in mAh.
78 */
79 public double usagePowerMah;
80
81 // Subsystem usage times.
Mike Ma07305c02018-03-02 16:57:31 -080082 public long audioTimeMs;
Adam Lesinski9f55cc72016-01-27 20:42:14 -080083 public long bluetoothRunningTimeMs;
Mike Ma07305c02018-03-02 16:57:31 -080084 public long cameraTimeMs;
Mathew Inwoodc185f082018-08-20 14:28:54 +010085 @UnsupportedAppUsage
Mike Ma07305c02018-03-02 16:57:31 -080086 public long cpuFgTimeMs;
Mathew Inwoodc185f082018-08-20 14:28:54 +010087 @UnsupportedAppUsage
Mike Ma07305c02018-03-02 16:57:31 -080088 public long cpuTimeMs;
89 public long flashlightTimeMs;
Mathew Inwoodc185f082018-08-20 14:28:54 +010090 @UnsupportedAppUsage
Mike Ma07305c02018-03-02 16:57:31 -080091 public long gpsTimeMs;
92 public long videoTimeMs;
Mathew Inwoodc185f082018-08-20 14:28:54 +010093 @UnsupportedAppUsage
Mike Ma07305c02018-03-02 16:57:31 -080094 public long wakeLockTimeMs;
Mathew Inwoodc185f082018-08-20 14:28:54 +010095 @UnsupportedAppUsage
Mike Ma07305c02018-03-02 16:57:31 -080096 public long wifiRunningTimeMs;
Adam Lesinski33dac552015-03-09 15:24:48 -070097
Dianne Hackborna7c837f2014-01-15 16:20:44 -080098 public long mobileRxPackets;
99 public long mobileTxPackets;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800100 public long mobileActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800101 public int mobileActiveCount;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800102 public double mobilemspp; // milliseconds per packet
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800103 public long wifiRxPackets;
104 public long wifiTxPackets;
105 public long mobileRxBytes;
106 public long mobileTxBytes;
107 public long wifiRxBytes;
108 public long wifiTxBytes;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800109 public long btRxBytes;
110 public long btTxBytes;
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800111 public double percent;
112 public double noCoveragePercent;
Mathew Inwoodc185f082018-08-20 14:28:54 +0100113 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800114 public String[] mPackages;
Mathew Inwoodc185f082018-08-20 14:28:54 +0100115 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800116 public String packageWithHighestDrain;
117
Adam Lesinski33dac552015-03-09 15:24:48 -0700118 // Measured in mAh (milli-ampere per hour).
Adam Lesinskie08af192015-03-25 16:42:59 -0700119 // These are included when summed.
Mike Ma07305c02018-03-02 16:57:31 -0800120 public double audioPowerMah;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800121 public double bluetoothPowerMah;
Mike Ma07305c02018-03-02 16:57:31 -0800122 public double cameraPowerMah;
Mathew Inwoodc185f082018-08-20 14:28:54 +0100123 @UnsupportedAppUsage
Mike Ma07305c02018-03-02 16:57:31 -0800124 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 Lesinski33dac552015-03-09 15:24:48 -0700132
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800133 public enum DrainType {
Mike Maeb0d8a72018-02-27 16:41:54 -0800134 AMBIENT_DISPLAY,
Mathew Inwoodc185f082018-08-20 14:28:54 +0100135 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800136 APP,
Mike Maeb0d8a72018-02-27 16:41:54 -0800137 BLUETOOTH,
James Carr2dd7e5e2016-07-20 18:48:39 -0700138 CAMERA,
Mike Maeb0d8a72018-02-27 16:41:54 -0800139 CELL,
140 FLASHLIGHT,
141 IDLE,
142 MEMORY,
143 OVERCOUNTED,
144 PHONE,
145 SCREEN,
146 UNACCOUNTED,
147 USER,
148 WIFI,
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800149 }
150
Mathew Inwoodc185f082018-08-20 14:28:54 +0100151 @UnsupportedAppUsage
Adam Lesinskie08af192015-03-25 16:42:59 -0700152 public BatterySipper(DrainType drainType, Uid uid, double value) {
153 this.totalPowerMah = value;
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800154 this.drainType = drainType;
155 uidObj = uid;
156 }
157
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800158 public void computeMobilemspp() {
jackqdyulei1424c1d2017-06-05 16:15:54 -0700159 long packets = mobileRxPackets + mobileTxPackets;
160 mobilemspp = packets > 0 ? (mobileActive / (double) packets) : 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800161 }
162
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800163 @Override
164 public int compareTo(BatterySipper other) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -0700165 // 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 Hackborna7c837f2014-01-15 16:20:44 -0800174 // Return the flipped value because we want the items in descending order
Adam Lesinskie08af192015-03-25 16:42:59 -0700175 return Double.compare(other.totalPowerMah, totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800176 }
177
178 /**
179 * Gets a list of packages associated with the current user
180 */
Mathew Inwoodc185f082018-08-20 14:28:54 +0100181 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800182 public String[] getPackages() {
183 return mPackages;
184 }
185
Mathew Inwoodc185f082018-08-20 14:28:54 +0100186 @UnsupportedAppUsage
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800187 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 Lesinski33dac552015-03-09 15:24:48 -0700194
195 /**
196 * Add stats from other to this BatterySipper.
197 */
Mathew Inwoodc185f082018-08-20 14:28:54 +0100198 @UnsupportedAppUsage
Adam Lesinski33dac552015-03-09 15:24:48 -0700199 public void add(BatterySipper other) {
Adam Lesinskie08af192015-03-25 16:42:59 -0700200 totalPowerMah += other.totalPowerMah;
201 usageTimeMs += other.usageTimeMs;
202 usagePowerMah += other.usagePowerMah;
Mike Ma07305c02018-03-02 16:57:31 -0800203 audioTimeMs += other.audioTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -0700204 cpuTimeMs += other.cpuTimeMs;
205 gpsTimeMs += other.gpsTimeMs;
206 wifiRunningTimeMs += other.wifiRunningTimeMs;
207 cpuFgTimeMs += other.cpuFgTimeMs;
Mike Ma07305c02018-03-02 16:57:31 -0800208 videoTimeMs += other.videoTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -0700209 wakeLockTimeMs += other.wakeLockTimeMs;
Ruben Brunk5b1308f2015-06-03 18:49:27 -0700210 cameraTimeMs += other.cameraTimeMs;
211 flashlightTimeMs += other.flashlightTimeMs;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800212 bluetoothRunningTimeMs += other.bluetoothRunningTimeMs;
Adam Lesinski33dac552015-03-09 15:24:48 -0700213 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 Lesinski9f55cc72016-01-27 20:42:14 -0800223 btRxBytes += other.btRxBytes;
224 btTxBytes += other.btTxBytes;
Mike Ma07305c02018-03-02 16:57:31 -0800225 audioPowerMah += other.audioPowerMah;
Adam Lesinskie08af192015-03-25 16:42:59 -0700226 wifiPowerMah += other.wifiPowerMah;
227 gpsPowerMah += other.gpsPowerMah;
228 cpuPowerMah += other.cpuPowerMah;
229 sensorPowerMah += other.sensorPowerMah;
230 mobileRadioPowerMah += other.mobileRadioPowerMah;
231 wakeLockPowerMah += other.wakeLockPowerMah;
Ruben Brunk5b1308f2015-06-03 18:49:27 -0700232 cameraPowerMah += other.cameraPowerMah;
233 flashlightPowerMah += other.flashlightPowerMah;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800234 bluetoothPowerMah += other.bluetoothPowerMah;
jackqdyulei1424c1d2017-06-05 16:15:54 -0700235 screenPowerMah += other.screenPowerMah;
Mike Ma07305c02018-03-02 16:57:31 -0800236 videoPowerMah += other.videoPowerMah;
jackqdyulei1424c1d2017-06-05 16:15:54 -0700237 proportionalSmearMah += other.proportionalSmearMah;
238 totalSmearedPowerMah += other.totalSmearedPowerMah;
Adam Lesinskie08af192015-03-25 16:42:59 -0700239 }
240
241 /**
242 * Sum all the powers and store the value into `value`.
jackqdyulei1424c1d2017-06-05 16:15:54 -0700243 * Also sum the {@code smearedTotalPowerMah} by adding smeared powerMah.
244 *
Adam Lesinskie08af192015-03-25 16:42:59 -0700245 * @return the sum of all the power in this BatterySipper.
246 */
247 public double sumPower() {
jackqdyulei1424c1d2017-06-05 16:15:54 -0700248 totalPowerMah = usagePowerMah + wifiPowerMah + gpsPowerMah + cpuPowerMah +
Ruben Brunk5b1308f2015-06-03 18:49:27 -0700249 sensorPowerMah + mobileRadioPowerMah + wakeLockPowerMah + cameraPowerMah +
Mike Ma07305c02018-03-02 16:57:31 -0800250 flashlightPowerMah + bluetoothPowerMah + audioPowerMah + videoPowerMah;
jackqdyulei1424c1d2017-06-05 16:15:54 -0700251 totalSmearedPowerMah = totalPowerMah + screenPowerMah + proportionalSmearMah;
252
253 return totalPowerMah;
Adam Lesinski33dac552015-03-09 15:24:48 -0700254 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800255}