• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.annotation.NonNull;
19 import android.os.BatteryConsumer;
20 import android.os.BatteryStats;
21 import android.os.BatteryUsageStats;
22 import android.os.BatteryUsageStatsQuery;
23 import android.os.UidBatteryConsumer;
24 import android.util.SparseArray;
25 
26 import java.io.PrintWriter;
27 
28 /**
29  * Calculates power use of a device subsystem for an app.
30  */
31 public abstract class PowerCalculator {
32     protected static final boolean DEBUG = false;
33 
34     protected static final double MILLIAMPHOUR_PER_MICROCOULOMB = 1.0 / 1000.0 / 60.0 / 60.0;
35 
36     /**
37      * Returns true if this power calculator computes power/duration for the specified
38      * power component.
39      */
isPowerComponentSupported( @atteryConsumer.PowerComponent int powerComponent)40     public abstract boolean isPowerComponentSupported(
41             @BatteryConsumer.PowerComponent int powerComponent);
42 
43     /**
44      * Attributes the total amount of power used by this subsystem to various consumers such
45      * as apps.
46      *
47      * @param builder       {@link BatteryUsageStats.Builder that contains a list of
48      *                      per-UID battery consumer builders for attribution data.
49      *                      The calculator may modify the builder and its constituent parts.
50      * @param batteryStats  The recorded battery stats.
51      * @param rawRealtimeUs The raw system realtime in microseconds.
52      * @param rawUptimeUs   The raw system uptime in microseconds.
53      * @param query         The query parameters for the calculator.
54      */
calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)55     public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats,
56             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
57         final SparseArray<UidBatteryConsumer.Builder> uidBatteryConsumerBuilders =
58                 builder.getUidBatteryConsumerBuilders();
59         for (int i = uidBatteryConsumerBuilders.size() - 1; i >= 0; i--) {
60             final UidBatteryConsumer.Builder app = uidBatteryConsumerBuilders.valueAt(i);
61             calculateApp(app, app.getBatteryStatsUid(), rawRealtimeUs, rawUptimeUs, query);
62         }
63     }
64 
65     /**
66      * Calculate the amount of power an app used for this subsystem.
67      * @param app The UidBatteryConsumer.Builder that represents the power use of an app.
68      * @param u The recorded stats for the app.
69      * @param rawRealtimeUs The raw system realtime in microseconds.
70      * @param rawUptimeUs The raw system uptime in microseconds.
71      * @param query Power calculation parameters.
72      */
calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)73     protected void calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u,
74             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
75     }
76 
77     /**
78      * Reset any state maintained in this calculator.
79      */
reset()80     public void reset() {
81     }
82 
getPowerModel( long measuredEnergyUC, @NonNull BatteryUsageStatsQuery query)83     protected static @BatteryConsumer.PowerModel int getPowerModel(
84             long measuredEnergyUC, @NonNull BatteryUsageStatsQuery query) {
85         if (measuredEnergyUC != BatteryStats.POWER_DATA_UNAVAILABLE
86                 && !query.shouldForceUsePowerProfileModel()) {
87             return BatteryConsumer.POWER_MODEL_MEASURED_ENERGY;
88         }
89         return BatteryConsumer.POWER_MODEL_POWER_PROFILE;
90     }
91 
getPowerModel(long measuredEnergyUC)92     protected static @BatteryConsumer.PowerModel int getPowerModel(long measuredEnergyUC) {
93         return measuredEnergyUC != BatteryStats.POWER_DATA_UNAVAILABLE
94                 ? BatteryConsumer.POWER_MODEL_MEASURED_ENERGY
95                 : BatteryConsumer.POWER_MODEL_POWER_PROFILE;
96     }
97 
98     /**
99      * Prints formatted amount of power in milli-amp-hours.
100      */
printPowerMah(PrintWriter pw, double powerMah)101     public static void printPowerMah(PrintWriter pw, double powerMah) {
102         pw.print(BatteryStats.formatCharge(powerMah));
103     }
104 
uCtoMah(long chargeUC)105     static double uCtoMah(long chargeUC) {
106         return chargeUC * MILLIAMPHOUR_PER_MICROCOULOMB;
107     }
108 }
109