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