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

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

Merge "Make PowerStatsCollector config more flexible, with a universal config setting" into main

parents 976da2d6 3335bc8d
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -27,16 +27,18 @@
    <!-- Whether to reset Battery Stats on unplug if the battery was significantly charged -->
    <bool name="config_batteryStatsResetOnUnplugAfterSignificantCharge">true</bool>

    <!-- CPU power stats collection throttle period in milliseconds.  Since power stats collection
    is a relatively expensive operation, this throttle period may need to be adjusted for low-power
    devices-->
    <integer name="config_defaultPowerStatsThrottlePeriodCpu">60000</integer>

    <!-- Mobile Radio power stats collection throttle period in milliseconds. -->
    <integer name="config_defaultPowerStatsThrottlePeriodMobileRadio">3600000</integer>

    <!-- Mobile Radio power stats collection throttle period in milliseconds. -->
    <integer name="config_defaultPowerStatsThrottlePeriodWifi">3600000</integer>
    <!-- Power stats collection throttle periods in milliseconds.  Since power stats collection
    is a relatively expensive operation, these throttle period may need to be adjusted for low-power
    devices.

    The syntax of this config string is as follows:
    <pre>
       power-component-name1:throttle-period-millis1 power-component-name2:throttle-period-ms2 ...
    </pre>
    Use "*" for the power-component-name to represent the default for all power components
    not mentioned by name.
    -->
    <string name="config_powerStatsThrottlePeriods">cpu:60000 *:300000</string>

    <!-- PowerStats aggregation period in milliseconds. This is the interval at which the power
    stats aggregation procedure is performed and the results stored in PowerStatsStore. -->
+1 −3
Original line number Diff line number Diff line
@@ -5243,9 +5243,7 @@

  <java-symbol type="bool" name="config_batteryStatsResetOnUnplugHighBatteryLevel" />
  <java-symbol type="bool" name="config_batteryStatsResetOnUnplugAfterSignificantCharge" />
  <java-symbol type="integer" name="config_defaultPowerStatsThrottlePeriodCpu" />
  <java-symbol type="integer" name="config_defaultPowerStatsThrottlePeriodMobileRadio" />
  <java-symbol type="integer" name="config_defaultPowerStatsThrottlePeriodWifi" />
  <java-symbol type="string" name="config_powerStatsThrottlePeriods" />
  <java-symbol type="integer" name="config_powerStatsAggregationPeriod" />
  <java-symbol type="integer" name="config_aggregatedPowerStatsSpanDuration" />

+28 −18
Original line number Diff line number Diff line
@@ -159,6 +159,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * All information we are collecting about things that can happen that impact
@@ -409,26 +411,14 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                com.android.internal.R.bool.config_batteryStatsResetOnUnplugHighBatteryLevel);
        final boolean resetOnUnplugAfterSignificantCharge = context.getResources().getBoolean(
                com.android.internal.R.bool.config_batteryStatsResetOnUnplugAfterSignificantCharge);
        final long powerStatsThrottlePeriodCpu = context.getResources().getInteger(
                com.android.internal.R.integer.config_defaultPowerStatsThrottlePeriodCpu);
        final long powerStatsThrottlePeriodMobileRadio = context.getResources().getInteger(
                com.android.internal.R.integer.config_defaultPowerStatsThrottlePeriodMobileRadio);
        final long powerStatsThrottlePeriodWifi = context.getResources().getInteger(
                com.android.internal.R.integer.config_defaultPowerStatsThrottlePeriodWifi);
        mBatteryStatsConfig =
        BatteryStatsImpl.BatteryStatsConfig.Builder batteryStatsConfigBuilder =
                new BatteryStatsImpl.BatteryStatsConfig.Builder()
                        .setResetOnUnplugHighBatteryLevel(resetOnUnplugHighBatteryLevel)
                        .setResetOnUnplugAfterSignificantCharge(resetOnUnplugAfterSignificantCharge)
                        .setPowerStatsThrottlePeriodMillis(
                                BatteryConsumer.POWER_COMPONENT_CPU,
                                powerStatsThrottlePeriodCpu)
                        .setPowerStatsThrottlePeriodMillis(
                                BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO,
                                powerStatsThrottlePeriodMobileRadio)
                        .setPowerStatsThrottlePeriodMillis(
                                BatteryConsumer.POWER_COMPONENT_WIFI,
                                powerStatsThrottlePeriodWifi)
                        .build();
                        .setResetOnUnplugAfterSignificantCharge(
                                resetOnUnplugAfterSignificantCharge);
        setPowerStatsThrottlePeriods(batteryStatsConfigBuilder, context.getResources().getString(
                com.android.internal.R.string.config_powerStatsThrottlePeriods));
        mBatteryStatsConfig = batteryStatsConfigBuilder.build();
        mPowerStatsUidResolver = new PowerStatsUidResolver();
        mStats = new BatteryStatsImpl(mBatteryStatsConfig, Clock.SYSTEM_CLOCK, mMonotonicClock,
                systemDir, mHandler, this, this, mUserManagerUserInfoProvider, mPowerProfile,
@@ -515,6 +505,26 @@ public final class BatteryStatsService extends IBatteryStats.Stub
        return config;
    }

    private void setPowerStatsThrottlePeriods(BatteryStatsImpl.BatteryStatsConfig.Builder builder,
            String configString) {
        Matcher matcher = Pattern.compile("([^:]+):(\\d+)\\s*").matcher(configString);
        while (matcher.find()) {
            String powerComponentName = matcher.group(1);
            long throttlePeriod;
            try {
                throttlePeriod = Long.parseLong(matcher.group(2));
            } catch (NumberFormatException nfe) {
                throw new IllegalArgumentException(
                        "Invalid config_powerStatsThrottlePeriods format: " + configString);
            }
            if (powerComponentName.equals("*")) {
                builder.setDefaultPowerStatsThrottlePeriodMillis(throttlePeriod);
            } else {
                builder.setPowerStatsThrottlePeriodMillis(powerComponentName, throttlePeriod);
            }
        }
    }

    /**
     * Creates an instance of BatteryStatsService and restores data from stored state.
     */
+49 −29
Original line number Diff line number Diff line
@@ -463,11 +463,17 @@ public class BatteryStatsImpl extends BatteryStats {
    public static class BatteryStatsConfig {
        static final int RESET_ON_UNPLUG_HIGH_BATTERY_LEVEL_FLAG = 1 << 0;
        static final int RESET_ON_UNPLUG_AFTER_SIGNIFICANT_CHARGE_FLAG = 1 << 1;
        static final long DEFAULT_POWER_STATS_COLLECTION_THROTTLE_PERIOD =
                TimeUnit.HOURS.toMillis(1);
        private final int mFlags;
        private SparseLongArray mPowerStatsThrottlePeriods;
        private final Long mDefaultPowerStatsThrottlePeriod;
        private final Map<String, Long> mPowerStatsThrottlePeriods;
        @VisibleForTesting
        public BatteryStatsConfig() {
            mFlags = 0;
            mDefaultPowerStatsThrottlePeriod = 0L;
            mPowerStatsThrottlePeriods = Map.of();
        }
        private BatteryStatsConfig(Builder builder) {
            int flags = 0;
@@ -478,6 +484,7 @@ public class BatteryStatsImpl extends BatteryStats {
                flags |= RESET_ON_UNPLUG_AFTER_SIGNIFICANT_CHARGE_FLAG;
            }
            mFlags = flags;
            mDefaultPowerStatsThrottlePeriod = builder.mDefaultPowerStatsThrottlePeriod;
            mPowerStatsThrottlePeriods = builder.mPowerStatsThrottlePeriods;
        }
@@ -485,7 +492,7 @@ public class BatteryStatsImpl extends BatteryStats {
         * Returns whether a BatteryStats reset should occur on unplug when the battery level is
         * high.
         */
        boolean shouldResetOnUnplugHighBatteryLevel() {
        public boolean shouldResetOnUnplugHighBatteryLevel() {
            return (mFlags & RESET_ON_UNPLUG_HIGH_BATTERY_LEVEL_FLAG)
                    == RESET_ON_UNPLUG_HIGH_BATTERY_LEVEL_FLAG;
        }
@@ -494,14 +501,18 @@ public class BatteryStatsImpl extends BatteryStats {
         * Returns whether a BatteryStats reset should occur on unplug if the battery charge a
         * significant amount since it has been plugged in.
         */
        boolean shouldResetOnUnplugAfterSignificantCharge() {
        public boolean shouldResetOnUnplugAfterSignificantCharge() {
            return (mFlags & RESET_ON_UNPLUG_AFTER_SIGNIFICANT_CHARGE_FLAG)
                    == RESET_ON_UNPLUG_AFTER_SIGNIFICANT_CHARGE_FLAG;
        }
        long getPowerStatsThrottlePeriod(@BatteryConsumer.PowerComponent int powerComponent) {
            return mPowerStatsThrottlePeriods.get(powerComponent,
                    DEFAULT_POWER_STATS_COLLECTION_THROTTLE_PERIOD);
        /**
         * Returns  the minimum amount of time (in millis) to wait between passes
         * of power stats collection for the specified power component.
         */
        public long getPowerStatsThrottlePeriod(String powerComponentName) {
            return mPowerStatsThrottlePeriods.getOrDefault(powerComponentName,
                    mDefaultPowerStatsThrottlePeriod);
        }
        /**
@@ -510,18 +521,19 @@ public class BatteryStatsImpl extends BatteryStats {
        public static class Builder {
            private boolean mResetOnUnplugHighBatteryLevel;
            private boolean mResetOnUnplugAfterSignificantCharge;
            private SparseLongArray mPowerStatsThrottlePeriods;
            public static final long DEFAULT_POWER_STATS_THROTTLE_PERIOD =
                    TimeUnit.HOURS.toMillis(1);
            public static final long DEFAULT_POWER_STATS_THROTTLE_PERIOD_CPU =
                    TimeUnit.MINUTES.toMillis(1);
            private long mDefaultPowerStatsThrottlePeriod = DEFAULT_POWER_STATS_THROTTLE_PERIOD;
            private final Map<String, Long> mPowerStatsThrottlePeriods = new HashMap<>();
            public Builder() {
                mResetOnUnplugHighBatteryLevel = true;
                mResetOnUnplugAfterSignificantCharge = true;
                mPowerStatsThrottlePeriods = new SparseLongArray();
                setPowerStatsThrottlePeriodMillis(BatteryConsumer.POWER_COMPONENT_CPU,
                        TimeUnit.MINUTES.toMillis(1));
                setPowerStatsThrottlePeriodMillis(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO,
                        TimeUnit.HOURS.toMillis(1));
                setPowerStatsThrottlePeriodMillis(BatteryConsumer.POWER_COMPONENT_WIFI,
                        TimeUnit.HOURS.toMillis(1));
                setPowerStatsThrottlePeriodMillis(BatteryConsumer.powerComponentIdToString(
                                BatteryConsumer.POWER_COMPONENT_CPU),
                        DEFAULT_POWER_STATS_THROTTLE_PERIOD_CPU);
            }
            /**
@@ -553,9 +565,18 @@ public class BatteryStatsImpl extends BatteryStats {
             * Sets the minimum amount of time (in millis) to wait between passes
             * of power stats collection for the specified power component.
             */
            public Builder setPowerStatsThrottlePeriodMillis(
                    @BatteryConsumer.PowerComponent int powerComponent, long periodMs) {
                mPowerStatsThrottlePeriods.put(powerComponent, periodMs);
            public Builder setPowerStatsThrottlePeriodMillis(String powerComponentName,
                    long periodMs) {
                mPowerStatsThrottlePeriods.put(powerComponentName, periodMs);
                return this;
            }
            /**
             * Sets the minimum amount of time (in millis) to wait between passes
             * of power stats collection for any components not configured explicitly.
             */
            public Builder setDefaultPowerStatsThrottlePeriodMillis(long periodMs) {
                mDefaultPowerStatsThrottlePeriod = periodMs;
                return this;
            }
        }
@@ -1586,8 +1607,7 @@ public class BatteryStatsImpl extends BatteryStats {
    protected final Constants mConstants;
    @VisibleForTesting
    @GuardedBy("this")
    protected BatteryStatsConfig mBatteryStatsConfig;
    protected final BatteryStatsConfig mBatteryStatsConfig;
    @GuardedBy("this")
    private AlarmManager mAlarmManager = null;
@@ -1932,6 +1952,11 @@ public class BatteryStatsImpl extends BatteryStats {
            return mClock;
        }
        @Override
        public long getPowerStatsCollectionThrottlePeriod(String powerComponentName) {
            return mBatteryStatsConfig.getPowerStatsThrottlePeriod(powerComponentName);
        }
        @Override
        public PowerStatsUidResolver getUidResolver() {
            return mPowerStatsUidResolver;
@@ -11167,19 +11192,14 @@ public class BatteryStatsImpl extends BatteryStats {
                mConstants.MAX_HISTORY_FILES, mConstants.MAX_HISTORY_BUFFER, mStepDetailsCalculator,
                mClock, mMonotonicClock, traceDelegate, eventLogger);
        mCpuPowerStatsCollector = new CpuPowerStatsCollector(mPowerStatsCollectorInjector,
                mBatteryStatsConfig.getPowerStatsThrottlePeriod(
                        BatteryConsumer.POWER_COMPONENT_CPU));
        mCpuPowerStatsCollector = new CpuPowerStatsCollector(mPowerStatsCollectorInjector);
        mCpuPowerStatsCollector.addConsumer(this::recordPowerStats);
        mMobileRadioPowerStatsCollector = new MobileRadioPowerStatsCollector(
                mPowerStatsCollectorInjector, mBatteryStatsConfig.getPowerStatsThrottlePeriod(
                BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO));
                mPowerStatsCollectorInjector);
        mMobileRadioPowerStatsCollector.addConsumer(this::recordPowerStats);
        mWifiPowerStatsCollector = new WifiPowerStatsCollector(
                mPowerStatsCollectorInjector, mBatteryStatsConfig.getPowerStatsThrottlePeriod(
                BatteryConsumer.POWER_COMPONENT_WIFI));
        mWifiPowerStatsCollector = new WifiPowerStatsCollector(mPowerStatsCollectorInjector);
        mWifiPowerStatsCollector.addConsumer(this::recordPowerStats);
        mStartCount++;
+6 −3
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ public class CpuPowerStatsCollector extends PowerStatsCollector {
        KernelCpuStatsReader getKernelCpuStatsReader();
        ConsumedEnergyRetriever getConsumedEnergyRetriever();
        IntSupplier getVoltageSupplier();
        long getPowerStatsCollectionThrottlePeriod(String powerComponentName);

        default int getDefaultCpuPowerBrackets() {
            return DEFAULT_CPU_POWER_BRACKETS;
@@ -94,9 +95,11 @@ public class CpuPowerStatsCollector extends PowerStatsCollector {
    private int mLastVoltageMv;
    private long[] mLastConsumedEnergyUws;

    public CpuPowerStatsCollector(Injector injector, long throttlePeriodMs) {
        super(injector.getHandler(), throttlePeriodMs, injector.getUidResolver(),
                injector.getClock());
    CpuPowerStatsCollector(Injector injector) {
        super(injector.getHandler(), injector.getPowerStatsCollectionThrottlePeriod(
                        BatteryConsumer.powerComponentIdToString(
                                BatteryConsumer.POWER_COMPONENT_CPU)),
                injector.getUidResolver(), injector.getClock());
        mInjector = injector;
    }

Loading