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

Commit c8e9407d authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge changes I49a78d3b,Ibaf53bb4 into main

* changes:
  Add Screen PowerStatsProcessor
  Add Screen PowerStatsCollector
parents d4a8bd48 241b4d0d
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,
    })
+1 −1
Original line number Diff line number Diff line
@@ -2491,7 +2491,7 @@ public abstract class BatteryStats {
    public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
    public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;

    static final String[] SCREEN_BRIGHTNESS_NAMES = {
    public static final String[] SCREEN_BRIGHTNESS_NAMES = {
        "dark", "dim", "medium", "light", "bright"
    };

+28 −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,
@@ -636,6 +652,18 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                BatteryConsumer.POWER_COMPONENT_CPU,
                Flags.streamlinedBatteryStats());

        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);
                });
    }
}
+83 −8
Original line number Diff line number Diff line
@@ -296,6 +296,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
    private int[] mCpuPowerBracketMap;
    private final CpuPowerStatsCollector mCpuPowerStatsCollector;
    private final ScreenPowerStatsCollector mScreenPowerStatsCollector;
    private final MobileRadioPowerStatsCollector mMobileRadioPowerStatsCollector;
    private final WifiPowerStatsCollector mWifiPowerStatsCollector;
    private final BluetoothPowerStatsCollector mBluetoothPowerStatsCollector;
@@ -303,6 +304,54 @@ public class BatteryStatsImpl extends BatteryStats {
    private final GnssPowerStatsCollector mGnssPowerStatsCollector;
    private final CustomEnergyConsumerPowerStatsCollector mCustomEnergyConsumerPowerStatsCollector;
    private final SparseBooleanArray mPowerStatsCollectorEnabled = new SparseBooleanArray();
    private ScreenPowerStatsCollector.ScreenUsageTimeRetriever mScreenUsageTimeRetriever =
            new ScreenPowerStatsCollector.ScreenUsageTimeRetriever() {
                @Override
                public long getScreenOnTimeMs(int display) {
                    synchronized (BatteryStatsImpl.this) {
                        return getDisplayScreenOnTime(display,
                                mClock.elapsedRealtime() * 1000) / 1000;
                    }
                }
                @Override
                public long getBrightnessLevelTimeMs(int display, int brightnessLevel) {
                    synchronized (BatteryStatsImpl.this) {
                        return getDisplayScreenBrightnessTime(display, brightnessLevel,
                                mClock.elapsedRealtime() * 1000) / 1000;
                    }
                }
                @Override
                public long getScreenDozeTimeMs(int display) {
                    synchronized (BatteryStatsImpl.this) {
                        return getDisplayScreenDozeTime(display,
                                mClock.elapsedRealtime() * 1000) / 1000;
                    }
                }
                @Override
                public void retrieveTopActivityTimes(Callback callback) {
                    synchronized (BatteryStatsImpl.this) {
                        long elapsedTimeUs = mClock.elapsedRealtime() * 1000;
                        for (int i = mUidStats.size() - 1; i >= 0; i--) {
                            Uid uid = mUidStats.valueAt(i);
                            long topStateTime = uid.getProcessStateTime(Uid.PROCESS_STATE_TOP,
                                    elapsedTimeUs, STATS_SINCE_CHARGED) / 1000;
                            Timer timer = uid.getForegroundActivityTimer();
                            if (timer == null) {
                                callback.onUidTopActivityTime(uid.mUid, topStateTime);
                            } else {
                                long topActivityTime = timer.getTotalTimeLocked(elapsedTimeUs,
                                        STATS_SINCE_CHARGED) / 1000;
                                callback.onUidTopActivityTime(uid.mUid, Math.min(topStateTime,
                                        topActivityTime));
                            }
                        }
                    }
                }
            };
    private final WifiPowerStatsCollector.WifiStatsRetriever mWifiStatsRetriever =
            new WifiPowerStatsCollector.WifiStatsRetriever() {
                @Override
@@ -1966,8 +2015,9 @@ public class BatteryStatsImpl extends BatteryStats {
    }
    private class PowerStatsCollectorInjector implements CpuPowerStatsCollector.Injector,
            MobileRadioPowerStatsCollector.Injector, WifiPowerStatsCollector.Injector,
            BluetoothPowerStatsCollector.Injector, EnergyConsumerPowerStatsCollector.Injector {
            ScreenPowerStatsCollector.Injector, MobileRadioPowerStatsCollector.Injector,
            WifiPowerStatsCollector.Injector, BluetoothPowerStatsCollector.Injector,
            EnergyConsumerPowerStatsCollector.Injector {
        private PackageManager mPackageManager;
        private PowerStatsCollector.ConsumedEnergyRetriever mConsumedEnergyRetriever;
        private NetworkStatsManager mNetworkStatsManager;
@@ -2038,6 +2088,16 @@ public class BatteryStatsImpl extends BatteryStats {
            return () -> mBatteryVoltageMv;
        }
        @Override
        public ScreenPowerStatsCollector.ScreenUsageTimeRetriever getScreenUsageTimeRetriever() {
            return mScreenUsageTimeRetriever;
        }
        @Override
        public int getDisplayCount() {
            return BatteryStatsImpl.this.getDisplayCount();
        }
        @Override
        public Supplier<NetworkStats> getMobileNetworkStatsSupplier() {
            return () -> readMobileNetworkStatsLocked(mNetworkStatsManager);
@@ -5736,6 +5796,9 @@ public class BatteryStatsImpl extends BatteryStats {
        maybeUpdateOverallScreenBrightness(overallBin, elapsedRealtimeMs, uptimeMs);
        if (shouldScheduleSync) {
            if (mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_SCREEN)) {
                mScreenPowerStatsCollector.schedule();
            } else {
                final int numDisplays = mPerDisplayBatteryStats.length;
                final int[] displayStates = new int[numDisplays];
                for (int i = 0; i < numDisplays; i++) {
@@ -5745,6 +5808,7 @@ public class BatteryStatsImpl extends BatteryStats {
                        batteryRunning, batteryScreenOffRunning, state, displayStates);
            }
        }
    }
    /**
     * Note screen brightness change for a display.
@@ -11290,6 +11354,9 @@ public class BatteryStatsImpl extends BatteryStats {
        mCpuPowerStatsCollector = new CpuPowerStatsCollector(mPowerStatsCollectorInjector);
        mCpuPowerStatsCollector.addConsumer(this::recordPowerStats);
        mScreenPowerStatsCollector = new ScreenPowerStatsCollector(mPowerStatsCollectorInjector);
        mScreenPowerStatsCollector.addConsumer(this::recordPowerStats);
        mMobileRadioPowerStatsCollector = new MobileRadioPowerStatsCollector(
                mPowerStatsCollectorInjector, this::onMobileRadioPowerStatsRetrieved);
        mMobileRadioPowerStatsCollector.addConsumer(this::recordPowerStats);
@@ -14750,6 +14817,10 @@ public class BatteryStatsImpl extends BatteryStats {
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_CPU));
        mCpuPowerStatsCollector.schedule();
        mScreenPowerStatsCollector.setEnabled(
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_SCREEN));
        mScreenPowerStatsCollector.schedule();
        mMobileRadioPowerStatsCollector.setEnabled(
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO));
        mMobileRadioPowerStatsCollector.schedule();
@@ -14786,6 +14857,8 @@ public class BatteryStatsImpl extends BatteryStats {
        switch (powerComponent) {
            case BatteryConsumer.POWER_COMPONENT_CPU:
                return mCpuPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_SCREEN:
                return mScreenPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO:
                return mMobileRadioPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_WIFI:
@@ -16329,6 +16402,7 @@ public class BatteryStatsImpl extends BatteryStats {
     */
    public void schedulePowerStatsSampleCollection() {
        mCpuPowerStatsCollector.forceSchedule();
        mScreenPowerStatsCollector.forceSchedule();
        mMobileRadioPowerStatsCollector.forceSchedule();
        mWifiPowerStatsCollector.forceSchedule();
        mBluetoothPowerStatsCollector.forceSchedule();
@@ -16351,6 +16425,7 @@ public class BatteryStatsImpl extends BatteryStats {
     */
    public void dumpStatsSample(PrintWriter pw) {
        mCpuPowerStatsCollector.collectAndDump(pw);
        mScreenPowerStatsCollector.collectAndDump(pw);
        mMobileRadioPowerStatsCollector.collectAndDump(pw);
        mWifiPowerStatsCollector.collectAndDump(pw);
        mBluetoothPowerStatsCollector.collectAndDump(pw);
Loading