• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.compat.annotation.UnsupportedAppUsage;
19 import android.os.BatteryStats.Uid;
20 import android.os.Build;
21 
22 import java.util.List;
23 
24 /**
25  * Contains power usage of an application, system service, or hardware type.
26  *
27  * @deprecated Please use BatteryStatsManager.getBatteryUsageStats instead.
28  */
29 @Deprecated
30 public class BatterySipper implements Comparable<BatterySipper> {
31     @UnsupportedAppUsage
32     public int userId;
33     @UnsupportedAppUsage
34     public Uid uidObj;
35     @UnsupportedAppUsage
36     public DrainType drainType;
37 
38     /**
39      * Smeared power from screen usage.
40      * We split the screen usage power and smear them among apps, based on activity time.
41      * The actual screen usage power may be measured or estimated, affecting the granularity and
42      * accuracy of the smearing, but the smearing algorithm is essentially the same.
43      */
44     public double screenPowerMah;
45 
46     /**
47      * Smeared power using proportional method.
48      *
49      * we smear power usage from hidden sippers to all apps proportionally.(except for screen usage)
50      *
51      * @see BatteryStatsHelper#shouldHideSipper(BatterySipper)
52      * @see BatteryStatsHelper#removeHiddenBatterySippers(List)
53      */
54     public double proportionalSmearMah;
55 
56     /**
57      * Total power that adding the smeared power.
58      *
59      * @see #sumPower()
60      */
61     public double totalSmearedPowerMah;
62 
63     /**
64      * Total power before smearing
65      */
66     @UnsupportedAppUsage
67     public double totalPowerMah;
68 
69     /**
70      * Whether we should hide this sipper
71      *
72      * @see BatteryStatsHelper#shouldHideSipper(BatterySipper)
73      */
74     public boolean shouldHide;
75 
76     /**
77      * Generic usage time in milliseconds.
78      */
79     @UnsupportedAppUsage
80     public long usageTimeMs;
81 
82     /**
83      * Generic power usage in mAh.
84      */
85     public double usagePowerMah;
86 
87     // Subsystem usage times.
88     public long audioTimeMs;
89     public long bluetoothRunningTimeMs;
90     public long cameraTimeMs;
91     @UnsupportedAppUsage
92     public long cpuFgTimeMs;
93     @UnsupportedAppUsage
94     public long cpuTimeMs;
95     public long flashlightTimeMs;
96     @UnsupportedAppUsage
97     public long gpsTimeMs;
98     public long videoTimeMs;
99     @UnsupportedAppUsage
100     public long wakeLockTimeMs;
101     @UnsupportedAppUsage
102     public long wifiRunningTimeMs;
103 
104     public long mobileRxPackets;
105     public long mobileTxPackets;
106     public long mobileActive;
107     public int mobileActiveCount;
108     public double mobilemspp;         // milliseconds per packet
109     public long wifiRxPackets;
110     public long wifiTxPackets;
111     public long mobileRxBytes;
112     public long mobileTxBytes;
113     public long wifiRxBytes;
114     public long wifiTxBytes;
115     public long btRxBytes;
116     public long btTxBytes;
117     public double percent;
118     public double noCoveragePercent;
119     @UnsupportedAppUsage
120     public String[] mPackages;
121     @UnsupportedAppUsage
122     public String packageWithHighestDrain;
123 
124     // Measured in mAh (milli-ampere per hour).
125     // These are included when summed.
126     public double audioPowerMah;
127     public double bluetoothPowerMah;
128     public double cameraPowerMah;
129     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
130     public double cpuPowerMah;
131     public double flashlightPowerMah;
132     public double gpsPowerMah;
133     public double mobileRadioPowerMah;
134     public double sensorPowerMah;
135     public double videoPowerMah;
136     public double wakeLockPowerMah;
137     public double wifiPowerMah;
138     public double systemServiceCpuPowerMah;
139     public double[] customMeasuredPowerMah;
140 
141     // Power that is re-attributed to other sippers. For example, for System Server
142     // this represents the power attributed to apps requesting system services.
143     // The value should be negative or zero.
144     public double powerReattributedToOtherSippersMah;
145 
146     // Do not include this sipper in results because it is included
147     // in an aggregate sipper.
148     public boolean isAggregated;
149 
150     //                           ****************
151     // This list must be kept current with atoms.proto (frameworks/base/cmds/statsd/src/atoms.proto)
152     // so the ordinal values (and therefore the order) must never change.
153     //                           ****************
154     @UnsupportedAppUsage(implicitMember =
155             "values()[Lcom/android/internal/os/BatterySipper$DrainType;")
156     public enum DrainType {
157         AMBIENT_DISPLAY,
158         @UnsupportedAppUsage
159         APP,
160         BLUETOOTH,
161         CAMERA,
162         CELL,
163         FLASHLIGHT,
164         IDLE,
165         MEMORY,
166         OVERCOUNTED,
167         PHONE,
168         SCREEN,
169         UNACCOUNTED,
170         USER,
171         WIFI,
172     }
173 
174     @UnsupportedAppUsage
BatterySipper(DrainType drainType, Uid uid, double value)175     public BatterySipper(DrainType drainType, Uid uid, double value) {
176         this.totalPowerMah = value;
177         this.drainType = drainType;
178         uidObj = uid;
179     }
180 
computeMobilemspp()181     public void computeMobilemspp() {
182         long packets = mobileRxPackets + mobileTxPackets;
183         mobilemspp = packets > 0 ? (mobileActive / (double) packets) : 0;
184     }
185 
186     @Override
compareTo(BatterySipper other)187     public int compareTo(BatterySipper other) {
188         // Over-counted always goes to the bottom.
189         if (drainType != other.drainType) {
190             if (drainType == DrainType.OVERCOUNTED) {
191                 // This is "larger"
192                 return 1;
193             } else if (other.drainType == DrainType.OVERCOUNTED) {
194                 return -1;
195             }
196         }
197         // Return the flipped value because we want the items in descending order
198         return Double.compare(other.totalPowerMah, totalPowerMah);
199     }
200 
201     /**
202      * Gets a list of packages associated with the current user
203      */
204     @UnsupportedAppUsage
getPackages()205     public String[] getPackages() {
206         return mPackages;
207     }
208 
209     @UnsupportedAppUsage
getUid()210     public int getUid() {
211         // Bail out if the current sipper is not an App sipper.
212         if (uidObj == null) {
213             return 0;
214         }
215         return uidObj.getUid();
216     }
217 
218     /**
219      * Add stats from other to this BatterySipper.
220      */
221     @UnsupportedAppUsage
add(BatterySipper other)222     public void add(BatterySipper other) {
223         totalPowerMah += other.totalPowerMah;
224         usageTimeMs += other.usageTimeMs;
225         usagePowerMah += other.usagePowerMah;
226         audioTimeMs += other.audioTimeMs;
227         cpuTimeMs += other.cpuTimeMs;
228         gpsTimeMs += other.gpsTimeMs;
229         wifiRunningTimeMs += other.wifiRunningTimeMs;
230         cpuFgTimeMs += other.cpuFgTimeMs;
231         videoTimeMs += other.videoTimeMs;
232         wakeLockTimeMs += other.wakeLockTimeMs;
233         cameraTimeMs += other.cameraTimeMs;
234         flashlightTimeMs += other.flashlightTimeMs;
235         bluetoothRunningTimeMs += other.bluetoothRunningTimeMs;
236         mobileRxPackets += other.mobileRxPackets;
237         mobileTxPackets += other.mobileTxPackets;
238         mobileActive += other.mobileActive;
239         mobileActiveCount += other.mobileActiveCount;
240         wifiRxPackets += other.wifiRxPackets;
241         wifiTxPackets += other.wifiTxPackets;
242         mobileRxBytes += other.mobileRxBytes;
243         mobileTxBytes += other.mobileTxBytes;
244         wifiRxBytes += other.wifiRxBytes;
245         wifiTxBytes += other.wifiTxBytes;
246         btRxBytes += other.btRxBytes;
247         btTxBytes += other.btTxBytes;
248         audioPowerMah += other.audioPowerMah;
249         wifiPowerMah += other.wifiPowerMah;
250         gpsPowerMah += other.gpsPowerMah;
251         cpuPowerMah += other.cpuPowerMah;
252         sensorPowerMah += other.sensorPowerMah;
253         mobileRadioPowerMah += other.mobileRadioPowerMah;
254         wakeLockPowerMah += other.wakeLockPowerMah;
255         cameraPowerMah += other.cameraPowerMah;
256         flashlightPowerMah += other.flashlightPowerMah;
257         bluetoothPowerMah += other.bluetoothPowerMah;
258         screenPowerMah += other.screenPowerMah;
259         videoPowerMah += other.videoPowerMah;
260         proportionalSmearMah += other.proportionalSmearMah;
261         totalSmearedPowerMah += other.totalSmearedPowerMah;
262         systemServiceCpuPowerMah += other.systemServiceCpuPowerMah;
263         if (other.customMeasuredPowerMah != null) {
264             if (customMeasuredPowerMah == null) {
265                 customMeasuredPowerMah = new double[other.customMeasuredPowerMah.length];
266             }
267             if (customMeasuredPowerMah.length == other.customMeasuredPowerMah.length) {
268                 // This should always be true.
269                 for (int idx = 0; idx < other.customMeasuredPowerMah.length; idx++) {
270                     customMeasuredPowerMah[idx] += other.customMeasuredPowerMah[idx];
271                 }
272             }
273         }
274         powerReattributedToOtherSippersMah += other.powerReattributedToOtherSippersMah;
275     }
276 
277     /**
278      * Sum all the powers and store the value into `value`.
279      * Also sum the {@code smearedTotalPowerMah} by adding smeared powerMah.
280      *
281      * @return the sum of all the power in this BatterySipper.
282      */
sumPower()283     public double sumPower() {
284         totalPowerMah = usagePowerMah + wifiPowerMah + gpsPowerMah + cpuPowerMah +
285                 sensorPowerMah + mobileRadioPowerMah + wakeLockPowerMah + cameraPowerMah +
286                 flashlightPowerMah + bluetoothPowerMah + audioPowerMah + videoPowerMah
287                 + systemServiceCpuPowerMah;
288         if (customMeasuredPowerMah != null) {
289             for (int idx = 0; idx < customMeasuredPowerMah.length; idx++) {
290                 totalPowerMah += customMeasuredPowerMah[idx];
291             }
292         }
293 
294         // powerAttributedToOtherSippersMah is negative or zero
295         totalPowerMah = totalPowerMah + powerReattributedToOtherSippersMah;
296 
297         totalSmearedPowerMah = totalPowerMah + screenPowerMah + proportionalSmearMah;
298 
299         return totalPowerMah;
300     }
301 }
302