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

Commit 241b4d0d authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Add Screen PowerStatsProcessor

Bug: 333941740
Test: atest PowerStatsTestsRavenwood; atest PowerStatsTests
Flag: com.android.server.power.optimization.streamlined_misc_battery_stats
Change-Id: I49a78d3beb73e1b1b0380834bcd9ddb318186c6b
parent f55c8df1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ public abstract class BatteryConsumer {
            POWER_COMPONENT_WAKELOCK,
            POWER_COMPONENT_MEMORY,
            POWER_COMPONENT_PHONE,
            POWER_COMPONENT_AMBIENT_DISPLAY,
            POWER_COMPONENT_IDLE,
            POWER_COMPONENT_REATTRIBUTED_TO_OTHER_CONSUMERS,
    })
+26 −0
Original line number Diff line number Diff line
@@ -124,6 +124,7 @@ import com.android.server.net.BaseNetworkObserver;
import com.android.server.pm.UserManagerInternal;
import com.android.server.power.optimization.Flags;
import com.android.server.power.stats.AggregatedPowerStatsConfig;
import com.android.server.power.stats.AmbientDisplayPowerStatsProcessor;
import com.android.server.power.stats.AudioPowerStatsProcessor;
import com.android.server.power.stats.BatteryExternalStatsWorker;
import com.android.server.power.stats.BatteryStatsDumpHelperImpl;
@@ -142,6 +143,7 @@ import com.android.server.power.stats.PowerStatsExporter;
import com.android.server.power.stats.PowerStatsScheduler;
import com.android.server.power.stats.PowerStatsStore;
import com.android.server.power.stats.PowerStatsUidResolver;
import com.android.server.power.stats.ScreenPowerStatsProcessor;
import com.android.server.power.stats.SystemServerCpuThreadReader.SystemServiceCpuThreadTimes;
import com.android.server.power.stats.VideoPowerStatsProcessor;
import com.android.server.power.stats.WifiPowerStatsProcessor;
@@ -488,6 +490,20 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                .setProcessor(
                        new CpuPowerStatsProcessor(mPowerProfile, mCpuScalingPolicies));

        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_SCREEN)
                .trackDeviceStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_SCREEN)
                .trackUidStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_SCREEN)
                .setProcessor(
                        new ScreenPowerStatsProcessor(mPowerProfile));

        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY,
                        BatteryConsumer.POWER_COMPONENT_SCREEN)
                .setProcessor(new AmbientDisplayPowerStatsProcessor());

        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO)
                .trackDeviceStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
@@ -638,6 +654,16 @@ public final class BatteryStatsService extends IBatteryStats.Stub

        mStats.setPowerStatsCollectorEnabled(BatteryConsumer.POWER_COMPONENT_SCREEN,
                Flags.streamlinedMiscBatteryStats());
        mBatteryUsageStatsProvider.setPowerStatsExporterEnabled(
                BatteryConsumer.POWER_COMPONENT_SCREEN,
                Flags.streamlinedMiscBatteryStats());

        mStats.setPowerStatsCollectorEnabled(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY,
                Flags.streamlinedMiscBatteryStats());
        mBatteryUsageStatsProvider.setPowerStatsExporterEnabled(
                BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY,
                Flags.streamlinedMiscBatteryStats());

        mStats.setPowerStatsCollectorEnabled(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO,
                Flags.streamlinedConnectivityBatteryStats());
        mBatteryUsageStatsProvider.setPowerStatsExporterEnabled(
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.server.power.stats;

import android.os.BatteryConsumer;
import android.os.PersistableBundle;

import com.android.internal.os.PowerStats;

public class AmbientDisplayPowerStatsProcessor extends PowerStatsProcessor {
    private final PowerStatsLayout mStatsLayout;
    private final PowerStats.Descriptor mDescriptor;
    private final long[] mTmpDeviceStats;
    private PowerStats.Descriptor mScreenPowerStatsDescriptor;
    private ScreenPowerStatsLayout mScreenPowerStatsLayout;
    private long[] mTmpScreenStats;

    public AmbientDisplayPowerStatsProcessor() {
        mStatsLayout = new PowerStatsLayout();
        mStatsLayout.addDeviceSectionPowerEstimate();
        PersistableBundle extras = new PersistableBundle();
        mStatsLayout.toExtras(extras);
        mDescriptor = new PowerStats.Descriptor(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY,
                mStatsLayout.getDeviceStatsArrayLength(), null, 0, 0, extras);
        mTmpDeviceStats = new long[mDescriptor.statsArrayLength];
    }

    @Override
    void finish(PowerComponentAggregatedPowerStats stats, long timestampMs) {
        stats.setPowerStatsDescriptor(mDescriptor);

        PowerComponentAggregatedPowerStats screenStats =
                stats.getAggregatedPowerStats().getPowerComponentStats(
                        BatteryConsumer.POWER_COMPONENT_SCREEN);
        if (screenStats == null) {
            return;
        }

        if (mScreenPowerStatsDescriptor == null) {
            mScreenPowerStatsDescriptor = screenStats.getPowerStatsDescriptor();
            if (mScreenPowerStatsDescriptor == null) {
                return;
            }

            mScreenPowerStatsLayout = new ScreenPowerStatsLayout(mScreenPowerStatsDescriptor);
            mTmpScreenStats = new long[mScreenPowerStatsDescriptor.statsArrayLength];
        }

        MultiStateStats.States[] deviceStateConfig = screenStats.getConfig().getDeviceStateConfig();

        // Ambient display power estimates have already been calculated by the screen power stats
        // processor. All that remains to be done is copy the estimates over.
        MultiStateStats.States.forEachTrackedStateCombination(deviceStateConfig,
                states -> {
                    screenStats.getDeviceStats(mTmpScreenStats, states);
                    double power =
                            mScreenPowerStatsLayout.getScreenDozePowerEstimate(mTmpScreenStats);
                    mStatsLayout.setDevicePowerEstimate(mTmpDeviceStats, power);
                    stats.setDeviceStats(states, mTmpDeviceStats);
                });
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -110,8 +110,13 @@ public class BatteryUsageStatsProvider {
                if (!mPowerStatsExporterEnabled.get(BatteryConsumer.POWER_COMPONENT_VIDEO)) {
                    mPowerCalculators.add(new VideoPowerCalculator(mPowerProfile));
                }
                if (!mPowerStatsExporterEnabled.get(BatteryConsumer.POWER_COMPONENT_SCREEN)) {
                    mPowerCalculators.add(new ScreenPowerCalculator(mPowerProfile));
                }
                if (!mPowerStatsExporterEnabled.get(
                        BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY)) {
                    mPowerCalculators.add(new AmbientDisplayPowerCalculator(mPowerProfile));
                }
                mPowerCalculators.add(new IdlePowerCalculator(mPowerProfile));
                if (!mPowerStatsExporterEnabled.get(BatteryConsumer.POWER_COMPONENT_ANY)) {
                    mPowerCalculators.add(new CustomEnergyConsumerPowerCalculator(mPowerProfile));
+2 −3
Original line number Diff line number Diff line
@@ -163,15 +163,14 @@ class PowerComponentAggregatedPowerStats {
        }
    }

    void setDeviceStats(@AggregatedPowerStatsConfig.TrackedState int[] states, long[] values) {
    void setDeviceStats(int[] states, long[] values) {
        if (mDeviceStats == null) {
            createDeviceStats(0);
        }
        mDeviceStats.setStats(states, values);
    }

    void setUidStats(int uid, @AggregatedPowerStatsConfig.TrackedState int[] states,
            long[] values) {
    void setUidStats(int uid, int[] states, long[] values) {
        UidStats uidStats = getUidStats(uid);
        uidStats.stats.setStats(states, values);
    }
Loading