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