Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 29b15648 authored by Adam Bookatz's avatar Adam Bookatz
Browse files

CustomMeasuredPowerCalculator

Adds a BatterySipper.customMeasuredPowerMah field to account
for custom energy buckets (i.e. EnergyConsumers of type OTHER)
and adds it to the total accumulated battery drain for each uid.

Bug: 179107328
Bug: 174818228
Test: Manual
Change-Id: Iab9c7ff5f410cd7c4be1ce78dfb2c39d4f783418
parent da600e0c
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -5294,6 +5294,12 @@ public abstract class BatteryStats implements Parcelable {
                        pw.print(" flash=");
                        printmAh(pw, bs.flashlightPowerMah);
                    }
                    if (bs.customMeasuredPowerMah != null) {
                        for (int idx = 0; idx < bs.customMeasuredPowerMah.length; idx++) {
                            pw.print(" custom[" + idx + "]=");
                            printmAh(pw, bs.customMeasuredPowerMah[idx]);
                        }
                    }
                    pw.print(" )");
                }

+17 −0
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ public class BatterySipper implements Comparable<BatterySipper> {
    public double wakeLockPowerMah;
    public double wifiPowerMah;
    public double systemServiceCpuPowerMah;
    public double[] customMeasuredPowerMah;

    // Do not include this sipper in results because it is included
    // in an aggregate sipper.
@@ -251,6 +252,17 @@ public class BatterySipper implements Comparable<BatterySipper> {
        proportionalSmearMah += other.proportionalSmearMah;
        totalSmearedPowerMah += other.totalSmearedPowerMah;
        systemServiceCpuPowerMah += other.systemServiceCpuPowerMah;
        if (other.customMeasuredPowerMah != null) {
            if (customMeasuredPowerMah == null) {
                customMeasuredPowerMah = new double[other.customMeasuredPowerMah.length];
            }
            if (customMeasuredPowerMah.length == other.customMeasuredPowerMah.length) {
                // This should always be true.
                for (int idx = 0; idx < other.customMeasuredPowerMah.length; idx++) {
                    customMeasuredPowerMah[idx] += other.customMeasuredPowerMah[idx];
                }
            }
        }
    }

    /**
@@ -264,6 +276,11 @@ public class BatterySipper implements Comparable<BatterySipper> {
                sensorPowerMah + mobileRadioPowerMah + wakeLockPowerMah + cameraPowerMah +
                flashlightPowerMah + bluetoothPowerMah + audioPowerMah + videoPowerMah
                + systemServiceCpuPowerMah;
        if (customMeasuredPowerMah != null) {
            for (int idx = 0; idx < customMeasuredPowerMah.length; idx++) {
                totalPowerMah += customMeasuredPowerMah[idx];
            }
        }
        totalSmearedPowerMah = totalPowerMah + screenPowerMah + proportionalSmearMah;

        return totalPowerMah;
+1 −0
Original line number Diff line number Diff line
@@ -347,6 +347,7 @@ public class BatteryStatsHelper {
            mPowerCalculators.add(new AmbientDisplayPowerCalculator(mPowerProfile));
            mPowerCalculators.add(new SystemServicePowerCalculator(mPowerProfile));
            mPowerCalculators.add(new IdlePowerCalculator(mPowerProfile));
            mPowerCalculators.add(new CustomMeasuredPowerCalculator(mPowerProfile));

            mPowerCalculators.add(new UserPowerCalculator());
        }
+1 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ public class BatteryUsageStatsProvider {
                mPowerCalculators.add(new AmbientDisplayPowerCalculator(mPowerProfile));
                mPowerCalculators.add(new SystemServicePowerCalculator(mPowerProfile));
                mPowerCalculators.add(new IdlePowerCalculator(mPowerProfile));
                mPowerCalculators.add(new CustomMeasuredPowerCalculator(mPowerProfile));

                mPowerCalculators.add(new UserPowerCalculator());
            }
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.internal.os;

import android.os.BatteryStats;

/**
 * Calculates the amount of power consumed by custom energy consumers (i.e. consumers of type
 * {@link android.hardware.power.stats.EnergyConsumerType#OTHER}).
 */
public class CustomMeasuredPowerCalculator extends PowerCalculator {
    public CustomMeasuredPowerCalculator(PowerProfile powerProfile) {
    }

    @Override
    protected void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
            long rawUptimeUs, int statsType) {
        updateCustomMeasuredPowerMah(app, u.getCustomMeasuredEnergiesMicroJoules());
    }

    private void updateCustomMeasuredPowerMah(BatterySipper sipper, long[] measuredEnergiesUJ) {
        sipper.customMeasuredPowerMah = calculateMeasuredEnergiesMah(measuredEnergiesUJ);
    }

    private double[] calculateMeasuredEnergiesMah(long[] measuredEnergiesUJ) {
        if (measuredEnergiesUJ == null) {
            return null;
        }
        final double[] measuredEnergiesMah = new double[measuredEnergiesUJ.length];
        for (int i = 0; i < measuredEnergiesUJ.length; i++) {
            measuredEnergiesMah[i] = uJtoMah(measuredEnergiesUJ[i]);
        }
        return measuredEnergiesMah;
    }
}