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

Commit 99aacb29 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Simplify naming of power stats processors

Bug: 323970018
Test: atest --host PowerStatsTestsRavenwood
Test: atest PowerStatsTests

Change-Id: I458cf7e3dd57965b0e46d8508561cbc63c2fb857
parent 9952c7ad
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -124,9 +124,9 @@ import com.android.server.power.stats.BatteryExternalStatsWorker;
import com.android.server.power.stats.BatteryStatsDumpHelperImpl;
import com.android.server.power.stats.BatteryStatsImpl;
import com.android.server.power.stats.BatteryUsageStatsProvider;
import com.android.server.power.stats.CpuAggregatedPowerStatsProcessor;
import com.android.server.power.stats.MobileRadioAggregatedPowerStatsProcessor;
import com.android.server.power.stats.PhoneCallAggregatedPowerStatsProcessor;
import com.android.server.power.stats.CpuPowerStatsProcessor;
import com.android.server.power.stats.MobileRadioPowerStatsProcessor;
import com.android.server.power.stats.PhoneCallPowerStatsProcessor;
import com.android.server.power.stats.PowerStatsAggregator;
import com.android.server.power.stats.PowerStatsExporter;
import com.android.server.power.stats.PowerStatsScheduler;
@@ -479,7 +479,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                .setProcessor(
                        new CpuAggregatedPowerStatsProcessor(mPowerProfile, mCpuScalingPolicies));
                        new CpuPowerStatsProcessor(mPowerProfile, mCpuScalingPolicies));
        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO)
                .trackDeviceStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
@@ -489,10 +489,10 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                .setProcessor(
                        new MobileRadioAggregatedPowerStatsProcessor(mPowerProfile));
                        new MobileRadioPowerStatsProcessor(mPowerProfile));
        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_PHONE,
                        BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO)
                .setProcessor(new PhoneCallAggregatedPowerStatsProcessor());
                .setProcessor(new PhoneCallPowerStatsProcessor());
        return config;
    }

+5 −5
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ public class AggregatedPowerStatsConfig {
        private final int mPowerComponentId;
        private @TrackedState int[] mTrackedDeviceStates;
        private @TrackedState int[] mTrackedUidStates;
        private AggregatedPowerStatsProcessor mProcessor = NO_OP_PROCESSOR;
        private PowerStatsProcessor mProcessor = NO_OP_PROCESSOR;

        PowerComponent(int powerComponentId) {
            this.mPowerComponentId = powerComponentId;
@@ -108,7 +108,7 @@ public class AggregatedPowerStatsConfig {
         * before giving the aggregates stats to consumers. The processor can complete the
         * aggregation process, for example by computing estimated power usage.
         */
        public PowerComponent setProcessor(@NonNull AggregatedPowerStatsProcessor processor) {
        public PowerComponent setProcessor(@NonNull PowerStatsProcessor processor) {
            mProcessor = processor;
            return this;
        }
@@ -143,7 +143,7 @@ public class AggregatedPowerStatsConfig {
        }

        @NonNull
        public AggregatedPowerStatsProcessor getProcessor() {
        public PowerStatsProcessor getProcessor() {
            return mProcessor;
        }

@@ -206,8 +206,8 @@ public class AggregatedPowerStatsConfig {
        return mPowerComponents;
    }

    private static final AggregatedPowerStatsProcessor NO_OP_PROCESSOR =
            new AggregatedPowerStatsProcessor() {
    private static final PowerStatsProcessor NO_OP_PROCESSOR =
            new PowerStatsProcessor() {
                @Override
                void finish(PowerComponentAggregatedPowerStats stats) {
                }
+2 −161
Original line number Diff line number Diff line
@@ -90,171 +90,12 @@ public class CpuPowerStatsCollector extends PowerStatsCollector {
    private PowerStats.Descriptor mPowerStatsDescriptor;
    // Reusable instance
    private PowerStats mCpuPowerStats;
    private CpuStatsArrayLayout mLayout;
    private CpuPowerStatsLayout mLayout;
    private long mLastUpdateTimestampNanos;
    private long mLastUpdateUptimeMillis;
    private int mLastVoltageMv;
    private long[] mLastConsumedEnergyUws;

    /**
     * Captures the positions and lengths of sections of the stats array, such as time-in-state,
     * power usage estimates etc.
     */
    public static class CpuStatsArrayLayout extends StatsArrayLayout {
        private static final String EXTRA_DEVICE_TIME_BY_SCALING_STEP_POSITION = "dt";
        private static final String EXTRA_DEVICE_TIME_BY_SCALING_STEP_COUNT = "dtc";
        private static final String EXTRA_DEVICE_TIME_BY_CLUSTER_POSITION = "dc";
        private static final String EXTRA_DEVICE_TIME_BY_CLUSTER_COUNT = "dcc";
        private static final String EXTRA_UID_BRACKETS_POSITION = "ub";
        private static final String EXTRA_UID_STATS_SCALING_STEP_TO_POWER_BRACKET = "us";

        private int mDeviceCpuTimeByScalingStepPosition;
        private int mDeviceCpuTimeByScalingStepCount;
        private int mDeviceCpuTimeByClusterPosition;
        private int mDeviceCpuTimeByClusterCount;

        private int mUidPowerBracketsPosition;
        private int mUidPowerBracketCount;

        private int[] mScalingStepToPowerBracketMap;

        /**
         * Declare that the stats array has a section capturing CPU time per scaling step
         */
        public void addDeviceSectionCpuTimeByScalingStep(int scalingStepCount) {
            mDeviceCpuTimeByScalingStepPosition = addDeviceSection(scalingStepCount);
            mDeviceCpuTimeByScalingStepCount = scalingStepCount;
        }

        public int getCpuScalingStepCount() {
            return mDeviceCpuTimeByScalingStepCount;
        }

        /**
         * Saves the time duration in the <code>stats</code> element
         * corresponding to the CPU scaling <code>state</code>.
         */
        public void setTimeByScalingStep(long[] stats, int step, long value) {
            stats[mDeviceCpuTimeByScalingStepPosition + step] = value;
        }

        /**
         * Extracts the time duration from the <code>stats</code> element
         * corresponding to the CPU scaling <code>step</code>.
         */
        public long getTimeByScalingStep(long[] stats, int step) {
            return stats[mDeviceCpuTimeByScalingStepPosition + step];
        }

        /**
         * Declare that the stats array has a section capturing CPU time in each cluster
         */
        public void addDeviceSectionCpuTimeByCluster(int clusterCount) {
            mDeviceCpuTimeByClusterPosition = addDeviceSection(clusterCount);
            mDeviceCpuTimeByClusterCount = clusterCount;
        }

        public int getCpuClusterCount() {
            return mDeviceCpuTimeByClusterCount;
        }

        /**
         * Saves the time duration in the <code>stats</code> element
         * corresponding to the CPU <code>cluster</code>.
         */
        public void setTimeByCluster(long[] stats, int cluster, long value) {
            stats[mDeviceCpuTimeByClusterPosition + cluster] = value;
        }

        /**
         * Extracts the time duration from the <code>stats</code> element
         * corresponding to the CPU <code>cluster</code>.
         */
        public long getTimeByCluster(long[] stats, int cluster) {
            return stats[mDeviceCpuTimeByClusterPosition + cluster];
        }

        /**
         * Declare that the UID stats array has a section capturing CPU time per power bracket.
         */
        public void addUidSectionCpuTimeByPowerBracket(int[] scalingStepToPowerBracketMap) {
            mScalingStepToPowerBracketMap = scalingStepToPowerBracketMap;
            updatePowerBracketCount();
            mUidPowerBracketsPosition = addUidSection(mUidPowerBracketCount);
        }

        private void updatePowerBracketCount() {
            mUidPowerBracketCount = 1;
            for (int bracket : mScalingStepToPowerBracketMap) {
                if (bracket >= mUidPowerBracketCount) {
                    mUidPowerBracketCount = bracket + 1;
                }
            }
        }

        public int[] getScalingStepToPowerBracketMap() {
            return mScalingStepToPowerBracketMap;
        }

        public int getCpuPowerBracketCount() {
            return mUidPowerBracketCount;
        }

        /**
         * Saves time in <code>bracket</code> in the corresponding section of <code>stats</code>.
         */
        public void setUidTimeByPowerBracket(long[] stats, int bracket, long value) {
            stats[mUidPowerBracketsPosition + bracket] = value;
        }

        /**
         * Extracts the time in <code>bracket</code> from a UID stats array.
         */
        public long getUidTimeByPowerBracket(long[] stats, int bracket) {
            return stats[mUidPowerBracketsPosition + bracket];
        }

        /**
         * Copies the elements of the stats array layout into <code>extras</code>
         */
        public void toExtras(PersistableBundle extras) {
            super.toExtras(extras);
            extras.putInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_POSITION,
                    mDeviceCpuTimeByScalingStepPosition);
            extras.putInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_COUNT,
                    mDeviceCpuTimeByScalingStepCount);
            extras.putInt(EXTRA_DEVICE_TIME_BY_CLUSTER_POSITION,
                    mDeviceCpuTimeByClusterPosition);
            extras.putInt(EXTRA_DEVICE_TIME_BY_CLUSTER_COUNT,
                    mDeviceCpuTimeByClusterCount);
            extras.putInt(EXTRA_UID_BRACKETS_POSITION, mUidPowerBracketsPosition);
            putIntArray(extras, EXTRA_UID_STATS_SCALING_STEP_TO_POWER_BRACKET,
                    mScalingStepToPowerBracketMap);
        }

        /**
         * Retrieves elements of the stats array layout from <code>extras</code>
         */
        public void fromExtras(PersistableBundle extras) {
            super.fromExtras(extras);
            mDeviceCpuTimeByScalingStepPosition =
                    extras.getInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_POSITION);
            mDeviceCpuTimeByScalingStepCount =
                    extras.getInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_COUNT);
            mDeviceCpuTimeByClusterPosition =
                    extras.getInt(EXTRA_DEVICE_TIME_BY_CLUSTER_POSITION);
            mDeviceCpuTimeByClusterCount =
                    extras.getInt(EXTRA_DEVICE_TIME_BY_CLUSTER_COUNT);
            mUidPowerBracketsPosition = extras.getInt(EXTRA_UID_BRACKETS_POSITION);
            mScalingStepToPowerBracketMap =
                    getIntArray(extras, EXTRA_UID_STATS_SCALING_STEP_TO_POWER_BRACKET);
            if (mScalingStepToPowerBracketMap == null) {
                mScalingStepToPowerBracketMap = new int[mDeviceCpuTimeByScalingStepCount];
            }
            updatePowerBracketCount();
        }
    }

    public CpuPowerStatsCollector(Injector injector, long throttlePeriodMs) {
        super(injector.getHandler(), throttlePeriodMs, injector.getClock());
        mInjector = injector;
@@ -290,7 +131,7 @@ public class CpuPowerStatsCollector extends PowerStatsCollector {
        mTempCpuTimeByScalingStep = new long[cpuScalingStepCount];
        int[] scalingStepToPowerBracketMap = initPowerBrackets();

        mLayout = new CpuStatsArrayLayout();
        mLayout = new CpuPowerStatsLayout();
        mLayout.addDeviceSectionCpuTimeByScalingStep(cpuScalingStepCount);
        mLayout.addDeviceSectionCpuTimeByCluster(mCpuScalingPolicies.getPolicies().length);
        mLayout.addDeviceSectionUsageDuration();
+178 −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.PersistableBundle;

/**
 * Captures the positions and lengths of sections of the stats array, such as time-in-state,
 * power usage estimates etc.
 */
public class CpuPowerStatsLayout extends PowerStatsLayout {
    private static final String EXTRA_DEVICE_TIME_BY_SCALING_STEP_POSITION = "dt";
    private static final String EXTRA_DEVICE_TIME_BY_SCALING_STEP_COUNT = "dtc";
    private static final String EXTRA_DEVICE_TIME_BY_CLUSTER_POSITION = "dc";
    private static final String EXTRA_DEVICE_TIME_BY_CLUSTER_COUNT = "dcc";
    private static final String EXTRA_UID_BRACKETS_POSITION = "ub";
    private static final String EXTRA_UID_STATS_SCALING_STEP_TO_POWER_BRACKET = "us";

    private int mDeviceCpuTimeByScalingStepPosition;
    private int mDeviceCpuTimeByScalingStepCount;
    private int mDeviceCpuTimeByClusterPosition;
    private int mDeviceCpuTimeByClusterCount;

    private int mUidPowerBracketsPosition;
    private int mUidPowerBracketCount;

    private int[] mScalingStepToPowerBracketMap;

    /**
     * Declare that the stats array has a section capturing CPU time per scaling step
     */
    public void addDeviceSectionCpuTimeByScalingStep(int scalingStepCount) {
        mDeviceCpuTimeByScalingStepPosition = addDeviceSection(scalingStepCount);
        mDeviceCpuTimeByScalingStepCount = scalingStepCount;
    }

    public int getCpuScalingStepCount() {
        return mDeviceCpuTimeByScalingStepCount;
    }

    /**
     * Saves the time duration in the <code>stats</code> element
     * corresponding to the CPU scaling <code>state</code>.
     */
    public void setTimeByScalingStep(long[] stats, int step, long value) {
        stats[mDeviceCpuTimeByScalingStepPosition + step] = value;
    }

    /**
     * Extracts the time duration from the <code>stats</code> element
     * corresponding to the CPU scaling <code>step</code>.
     */
    public long getTimeByScalingStep(long[] stats, int step) {
        return stats[mDeviceCpuTimeByScalingStepPosition + step];
    }

    /**
     * Declare that the stats array has a section capturing CPU time in each cluster
     */
    public void addDeviceSectionCpuTimeByCluster(int clusterCount) {
        mDeviceCpuTimeByClusterPosition = addDeviceSection(clusterCount);
        mDeviceCpuTimeByClusterCount = clusterCount;
    }

    public int getCpuClusterCount() {
        return mDeviceCpuTimeByClusterCount;
    }

    /**
     * Saves the time duration in the <code>stats</code> element
     * corresponding to the CPU <code>cluster</code>.
     */
    public void setTimeByCluster(long[] stats, int cluster, long value) {
        stats[mDeviceCpuTimeByClusterPosition + cluster] = value;
    }

    /**
     * Extracts the time duration from the <code>stats</code> element
     * corresponding to the CPU <code>cluster</code>.
     */
    public long getTimeByCluster(long[] stats, int cluster) {
        return stats[mDeviceCpuTimeByClusterPosition + cluster];
    }

    /**
     * Declare that the UID stats array has a section capturing CPU time per power bracket.
     */
    public void addUidSectionCpuTimeByPowerBracket(int[] scalingStepToPowerBracketMap) {
        mScalingStepToPowerBracketMap = scalingStepToPowerBracketMap;
        updatePowerBracketCount();
        mUidPowerBracketsPosition = addUidSection(mUidPowerBracketCount);
    }

    private void updatePowerBracketCount() {
        mUidPowerBracketCount = 1;
        for (int bracket : mScalingStepToPowerBracketMap) {
            if (bracket >= mUidPowerBracketCount) {
                mUidPowerBracketCount = bracket + 1;
            }
        }
    }

    public int[] getScalingStepToPowerBracketMap() {
        return mScalingStepToPowerBracketMap;
    }

    public int getCpuPowerBracketCount() {
        return mUidPowerBracketCount;
    }

    /**
     * Saves time in <code>bracket</code> in the corresponding section of <code>stats</code>.
     */
    public void setUidTimeByPowerBracket(long[] stats, int bracket, long value) {
        stats[mUidPowerBracketsPosition + bracket] = value;
    }

    /**
     * Extracts the time in <code>bracket</code> from a UID stats array.
     */
    public long getUidTimeByPowerBracket(long[] stats, int bracket) {
        return stats[mUidPowerBracketsPosition + bracket];
    }

    /**
     * Copies the elements of the stats array layout into <code>extras</code>
     */
    public void toExtras(PersistableBundle extras) {
        super.toExtras(extras);
        extras.putInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_POSITION,
                mDeviceCpuTimeByScalingStepPosition);
        extras.putInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_COUNT,
                mDeviceCpuTimeByScalingStepCount);
        extras.putInt(EXTRA_DEVICE_TIME_BY_CLUSTER_POSITION,
                mDeviceCpuTimeByClusterPosition);
        extras.putInt(EXTRA_DEVICE_TIME_BY_CLUSTER_COUNT,
                mDeviceCpuTimeByClusterCount);
        extras.putInt(EXTRA_UID_BRACKETS_POSITION, mUidPowerBracketsPosition);
        putIntArray(extras, EXTRA_UID_STATS_SCALING_STEP_TO_POWER_BRACKET,
                mScalingStepToPowerBracketMap);
    }

    /**
     * Retrieves elements of the stats array layout from <code>extras</code>
     */
    public void fromExtras(PersistableBundle extras) {
        super.fromExtras(extras);
        mDeviceCpuTimeByScalingStepPosition =
                extras.getInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_POSITION);
        mDeviceCpuTimeByScalingStepCount =
                extras.getInt(EXTRA_DEVICE_TIME_BY_SCALING_STEP_COUNT);
        mDeviceCpuTimeByClusterPosition =
                extras.getInt(EXTRA_DEVICE_TIME_BY_CLUSTER_POSITION);
        mDeviceCpuTimeByClusterCount =
                extras.getInt(EXTRA_DEVICE_TIME_BY_CLUSTER_COUNT);
        mUidPowerBracketsPosition = extras.getInt(EXTRA_UID_BRACKETS_POSITION);
        mScalingStepToPowerBracketMap =
                getIntArray(extras, EXTRA_UID_STATS_SCALING_STEP_TO_POWER_BRACKET);
        if (mScalingStepToPowerBracketMap == null) {
            mScalingStepToPowerBracketMap = new int[mDeviceCpuTimeByScalingStepCount];
        }
        updatePowerBracketCount();
    }
}
+5 −6
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class CpuAggregatedPowerStatsProcessor extends AggregatedPowerStatsProcessor {
    private static final String TAG = "CpuAggregatedPowerStatsProcessor";
public class CpuPowerStatsProcessor extends PowerStatsProcessor {
    private static final String TAG = "CpuPowerStatsProcessor";

    private static final double HOUR_IN_MILLIS = TimeUnit.HOURS.toMillis(1);
    private static final int UNKNOWN = -1;
@@ -64,7 +64,7 @@ public class CpuAggregatedPowerStatsProcessor extends AggregatedPowerStatsProces
    private PowerStats.Descriptor mLastUsedDescriptor;
    // Cached results of parsing of current PowerStats.Descriptor. Only refreshed when
    // mLastUsedDescriptor changes
    private CpuPowerStatsCollector.CpuStatsArrayLayout mStatsLayout;
    private CpuPowerStatsLayout mStatsLayout;
    // Sequence of steps for power estimation and intermediate results.
    private PowerEstimationPlan mPlan;

@@ -73,8 +73,7 @@ public class CpuAggregatedPowerStatsProcessor extends AggregatedPowerStatsProces
    // Temp array for retrieval of UID power stats, to avoid repeated allocations
    private long[] mTmpUidStatsArray;

    public CpuAggregatedPowerStatsProcessor(PowerProfile powerProfile,
            CpuScalingPolicies scalingPolicies) {
    public CpuPowerStatsProcessor(PowerProfile powerProfile, CpuScalingPolicies scalingPolicies) {
        mCpuScalingPolicies = scalingPolicies;
        mCpuScalingStepCount = scalingPolicies.getScalingStepCount();
        mScalingStepToCluster = new int[mCpuScalingStepCount];
@@ -106,7 +105,7 @@ public class CpuAggregatedPowerStatsProcessor extends AggregatedPowerStatsProces
        }

        mLastUsedDescriptor = descriptor;
        mStatsLayout = new CpuPowerStatsCollector.CpuStatsArrayLayout();
        mStatsLayout = new CpuPowerStatsLayout();
        mStatsLayout.fromExtras(descriptor.extras);

        mTmpDeviceStatsArray = new long[descriptor.statsArrayLength];
Loading