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

Commit 1a0b8001 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Automerger Merge Worker
Browse files

Merge "Do realistic data mocking in BluetoothPowerCalculatorTest" into sc-dev am: b5d43c6b

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14458439

Change-Id: Ie73b27216661c426404a1c784a81848bf0a9693f
parents 2aedff2c b5d43c6b
Loading
Loading
Loading
Loading
+19 −12
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ public class BluetoothPowerCalculator extends PowerCalculator {
    @Override
    public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats,
            long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
        if (!mHasBluetoothPowerController || !batteryStats.hasBluetoothActivityReporting()) {
        if (!batteryStats.hasBluetoothActivityReporting()) {
            return;
        }

@@ -69,8 +69,8 @@ public class BluetoothPowerCalculator extends PowerCalculator {
        final ControllerActivityCounter activityCounter =
                batteryStats.getBluetoothControllerActivity();
        final long systemDurationMs = calculateDuration(activityCounter);
        final double systemPowerMah =
                calculatePowerMah(powerModel, measuredChargeUC, activityCounter);
        final double systemPowerMah = calculatePowerMah(powerModel, measuredChargeUC,
                activityCounter, query.shouldForceUsePowerProfileModel());

        // Subtract what the apps used, but clamp to 0.
        final long systemComponentDurationMs = Math.max(0, systemDurationMs - total.durationMs);
@@ -100,7 +100,8 @@ public class BluetoothPowerCalculator extends PowerCalculator {
        final ControllerActivityCounter activityCounter =
                app.getBatteryStatsUid().getBluetoothControllerActivity();
        final long durationMs = calculateDuration(activityCounter);
        final double powerMah = calculatePowerMah(powerModel, measuredChargeUC, activityCounter);
        final double powerMah = calculatePowerMah(powerModel, measuredChargeUC, activityCounter,
                query.shouldForceUsePowerProfileModel());

        app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_BLUETOOTH, durationMs)
                .setConsumedPower(BatteryConsumer.POWER_COMPONENT_BLUETOOTH, powerMah, powerModel);
@@ -132,7 +133,7 @@ public class BluetoothPowerCalculator extends PowerCalculator {
                batteryStats.getBluetoothControllerActivity();
        final long systemDurationMs = calculateDuration(activityCounter);
        final double systemPowerMah =
                calculatePowerMah(powerModel, measuredChargeUC, activityCounter);
                calculatePowerMah(powerModel, measuredChargeUC, activityCounter, false);

        // Subtract what the apps used, but clamp to 0.
        final double powerMah = Math.max(0, systemPowerMah - total.powerMah);
@@ -165,7 +166,8 @@ public class BluetoothPowerCalculator extends PowerCalculator {
        final int powerModel = getPowerModel(measuredChargeUC);
        final ControllerActivityCounter activityCounter = u.getBluetoothControllerActivity();
        final long durationMs = calculateDuration(activityCounter);
        final double powerMah = calculatePowerMah(powerModel, measuredChargeUC, activityCounter);
        final double powerMah = calculatePowerMah(powerModel, measuredChargeUC, activityCounter,
                false);

        app.bluetoothRunningTimeMs = durationMs;
        app.bluetoothPowerMah = powerMah;
@@ -188,7 +190,7 @@ public class BluetoothPowerCalculator extends PowerCalculator {

    /** Returns bluetooth power usage based on the best data available. */
    private double calculatePowerMah(@BatteryConsumer.PowerModel int powerModel,
            long measuredChargeUC, ControllerActivityCounter counter) {
            long measuredChargeUC, ControllerActivityCounter counter, boolean ignoreReportedPower) {
        if (powerModel == BatteryConsumer.POWER_MODEL_MEASURED_ENERGY) {
            return uCtoMah(measuredChargeUC);
        }
@@ -197,13 +199,18 @@ public class BluetoothPowerCalculator extends PowerCalculator {
            return 0;
        }

        if (!ignoreReportedPower) {
            final double powerMah =
                    counter.getPowerCounter().getCountLocked(BatteryStats.STATS_SINCE_CHARGED)
                            / (double) (1000 * 60 * 60);

            if (powerMah != 0) {
                return powerMah;
            }
        }

        if (!mHasBluetoothPowerController) {
            return 0;
        }

        final long idleTimeMs =
                counter.getIdleTimeCounter().getCountLocked(BatteryStats.STATS_SINCE_CHARGED);
+1 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ public class AmbientDisplayPowerCalculatorTest {

    @Test
    public void testMeasuredEnergyBasedModel() {
        mStatsRule.initMeasuredEnergyStatsLocked();
        BatteryStatsImpl stats = mStatsRule.getBatteryStats();

        stats.updateDisplayMeasuredEnergyStatsLocked(300_000_000, Display.STATE_ON, 0);
+3 −0
Original line number Diff line number Diff line
@@ -506,6 +506,7 @@ public class BatteryStatsNoteTest extends TestCase {
    public void testUpdateDisplayMeasuredEnergyStatsLocked() {
        final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
        final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
        bi.initMeasuredEnergyStats();

        clocks.realtime = 0;
        int screen = Display.STATE_OFF;
@@ -590,6 +591,7 @@ public class BatteryStatsNoteTest extends TestCase {
    public void testUpdateCustomMeasuredEnergyStatsLocked_neverCalled() {
        final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
        final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
        bi.initMeasuredEnergyStats();
        bi.setOnBatteryInternal(true);

        final int uid1 = 11500;
@@ -603,6 +605,7 @@ public class BatteryStatsNoteTest extends TestCase {
    public void testUpdateCustomMeasuredEnergyStatsLocked() {
        final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
        final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
        bi.initMeasuredEnergyStats();

        final int bucketA = 0; // Custom bucket 0
        final int bucketB = 1; // Custom bucket 1
+10 −7
Original line number Diff line number Diff line
@@ -51,12 +51,7 @@ public class BatteryUsageStatsRule implements TestRule {

    private final PowerProfile mPowerProfile;
    private final MockClocks mMockClocks = new MockClocks();
    private final MockBatteryStatsImpl mBatteryStats = new MockBatteryStatsImpl(mMockClocks) {
        @Override
        public boolean hasBluetoothActivityReporting() {
            return true;
        }
    };
    private final MockBatteryStatsImpl mBatteryStats;

    private BatteryUsageStats mBatteryUsageStats;
    private boolean mScreenOn;
@@ -64,6 +59,7 @@ public class BatteryUsageStatsRule implements TestRule {
    public BatteryUsageStatsRule() {
        Context context = InstrumentationRegistry.getContext();
        mPowerProfile = spy(new PowerProfile(context, true /* forTest */));
        mBatteryStats = new MockBatteryStatsImpl(mMockClocks);
        mBatteryStats.setPowerProfile(mPowerProfile);
    }

@@ -110,10 +106,17 @@ public class BatteryUsageStatsRule implements TestRule {

    /** Call only after setting the power profile information. */
    public BatteryUsageStatsRule initMeasuredEnergyStatsLocked() {
        return initMeasuredEnergyStatsLocked(new String[0]);
    }

    /** Call only after setting the power profile information. */
    public BatteryUsageStatsRule initMeasuredEnergyStatsLocked(
            String[] customPowerComponentNames) {
        final boolean[] supportedStandardBuckets =
                new boolean[MeasuredEnergyStats.NUMBER_STANDARD_POWER_BUCKETS];
        Arrays.fill(supportedStandardBuckets, true);
        mBatteryStats.initMeasuredEnergyStatsLocked(supportedStandardBuckets, new String[0]);
        mBatteryStats.initMeasuredEnergyStatsLocked(supportedStandardBuckets,
                customPowerComponentNames);
        mBatteryStats.informThatAllExternalStatsAreFlushed();
        return this;
    }
+47 −69
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.annotation.Nullable;
import android.bluetooth.BluetoothActivityEnergyInfo;
import android.bluetooth.UidTraffic;
import android.os.BatteryConsumer;
import android.os.BatteryStats;
import android.os.BatteryUsageStatsQuery;
import android.os.Process;

@@ -43,111 +44,89 @@ public class BluetoothPowerCalculatorTest {
            .setAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_IDLE, 10.0)
            .setAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_RX, 50.0)
            .setAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_TX, 100.0)
            .initMeasuredEnergyStatsLocked();
            .setAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE, 3700);

    @Test
    public void testTimerBasedModel() {
        setDurationsAndPower(mStatsRule.getUidStats(Process.BLUETOOTH_UID)
                        .getOrCreateBluetoothControllerActivityLocked(),
                1000, 2000, 3000, 0);
        setupBluetoothEnergyInfo(0, BatteryStats.POWER_DATA_UNAVAILABLE);

        setDurationsAndPower(mStatsRule.getUidStats(APP_UID)
                        .getOrCreateBluetoothControllerActivityLocked(),
                4000, 5000, 6000, 0);
        BluetoothPowerCalculator calculator =
                new BluetoothPowerCalculator(mStatsRule.getPowerProfile());

        mStatsRule.apply(BatteryUsageStatsRule.POWER_PROFILE_MODEL_ONLY, calculator);

        assertCalculatedPower(0.08216, 0.18169, 0.26388, 0.26386,
                BatteryConsumer.POWER_MODEL_POWER_PROFILE);
    }

        setDurationsAndPower((BatteryStatsImpl.ControllerActivityCounterImpl)
                        mStatsRule.getBatteryStats().getBluetoothControllerActivity(),
                6000, 8000, 10000, 0);
    @Test
    public void testReportedEnergyBasedModel() {
        setupBluetoothEnergyInfo(4000000, BatteryStats.POWER_DATA_UNAVAILABLE);

        BluetoothPowerCalculator calculator =
                new BluetoothPowerCalculator(mStatsRule.getPowerProfile());

        mStatsRule.apply(BatteryUsageStatsRule.POWER_PROFILE_MODEL_ONLY, calculator);
        mStatsRule.apply(new BatteryUsageStatsQuery.Builder().includePowerModels().build(),
                calculator);

        assertBluetoothPowerAndDuration(
                mStatsRule.getUidBatteryConsumer(Process.BLUETOOTH_UID),
                0.11388, 6000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertBluetoothPowerAndDuration(
                mStatsRule.getUidBatteryConsumer(APP_UID),
                0.24722, 15000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertBluetoothPowerAndDuration(
                mStatsRule.getDeviceBatteryConsumer(),
                0.40555, 24000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertBluetoothPowerAndDuration(
                mStatsRule.getAppsBatteryConsumer(),
                0.36111, 21000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertCalculatedPower(0.08216, 0.18169, 0.30030, 0.26386,
                BatteryConsumer.POWER_MODEL_POWER_PROFILE);
    }

    @Test
    public void testReportedPowerBasedModel() {
        setDurationsAndPower(mStatsRule.getUidStats(Process.BLUETOOTH_UID)
                        .getOrCreateBluetoothControllerActivityLocked(),
                1000, 2000, 3000, 360000);
    public void testMeasuredEnergyBasedModel() {
        mStatsRule.initMeasuredEnergyStatsLocked();
        setupBluetoothEnergyInfo(0, 1200000);

        setDurationsAndPower(mStatsRule.getUidStats(APP_UID)
                        .getOrCreateBluetoothControllerActivityLocked(),
                4000, 5000, 6000, 720000);
        final BluetoothPowerCalculator calculator =
                new BluetoothPowerCalculator(mStatsRule.getPowerProfile());

        setDurationsAndPower((BatteryStatsImpl.ControllerActivityCounterImpl)
                        mStatsRule.getBatteryStats().getBluetoothControllerActivity(),
                6000, 8000, 10000, 1260000);
        mStatsRule.apply(calculator);

        assertCalculatedPower(0.10378, 0.22950, 0.33333, 0.33329,
                BatteryConsumer.POWER_MODEL_MEASURED_ENERGY);
    }

    @Test
    public void testIgnoreMeasuredEnergyBasedModel() {
        mStatsRule.initMeasuredEnergyStatsLocked();
        setupBluetoothEnergyInfo(4000000, 1200000);

        BluetoothPowerCalculator calculator =
                new BluetoothPowerCalculator(mStatsRule.getPowerProfile());

        mStatsRule.apply(BatteryUsageStatsRule.POWER_PROFILE_MODEL_ONLY, calculator);

        assertBluetoothPowerAndDuration(
                mStatsRule.getUidBatteryConsumer(Process.BLUETOOTH_UID),
                0.1, 6000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertBluetoothPowerAndDuration(
                mStatsRule.getUidBatteryConsumer(APP_UID),
                0.2, 15000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertBluetoothPowerAndDuration(
                mStatsRule.getDeviceBatteryConsumer(),
                0.35, 24000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertBluetoothPowerAndDuration(
                mStatsRule.getAppsBatteryConsumer(),
                0.3, 21000, BatteryConsumer.POWER_MODEL_POWER_PROFILE);
        assertCalculatedPower(0.08216, 0.18169, 0.26388, 0.26386,
                BatteryConsumer.POWER_MODEL_POWER_PROFILE);
    }

    @Test
    public void testMeasuredEnergyBasedModel() {
    private void setupBluetoothEnergyInfo(long reportedEnergyUc, long consumedEnergyUc) {
        final BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo(1000,
                BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_ACTIVE, 7000, 5000, 0, 100000);
                BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_ACTIVE, 7000, 5000, 0,
                reportedEnergyUc);
        info.setUidTraffic(new UidTraffic[]{
                new UidTraffic(Process.BLUETOOTH_UID, 1000, 2000),
                new UidTraffic(APP_UID, 3000, 4000)
        });
        mStatsRule.getBatteryStats().updateBluetoothStateLocked(info, 1200000, 1000, 1000);

        final BluetoothPowerCalculator calculator =
                new BluetoothPowerCalculator(mStatsRule.getPowerProfile());

        mStatsRule.apply(new BatteryUsageStatsQuery.Builder().includePowerModels().build(),
                calculator);
        mStatsRule.getBatteryStats().updateBluetoothStateLocked(info,
                consumedEnergyUc, 1000, 1000);
    }

    private void assertCalculatedPower(double bluetoothUidPowerMah, double appPowerMah,
            double devicePowerMah, double allAppsPowerMah, int powerModelPowerProfile) {
        assertBluetoothPowerAndDuration(
                mStatsRule.getUidBatteryConsumer(Process.BLUETOOTH_UID),
                0.10378, 3583, BatteryConsumer.POWER_MODEL_MEASURED_ENERGY);
                bluetoothUidPowerMah, 3583, powerModelPowerProfile);
        assertBluetoothPowerAndDuration(
                mStatsRule.getUidBatteryConsumer(APP_UID),
                0.22950, 8416, BatteryConsumer.POWER_MODEL_MEASURED_ENERGY);
                appPowerMah, 8416, powerModelPowerProfile);
        assertBluetoothPowerAndDuration(
                mStatsRule.getDeviceBatteryConsumer(),
                0.33333, 12000, BatteryConsumer.POWER_MODEL_MEASURED_ENERGY);
                devicePowerMah, 12000, powerModelPowerProfile);
        assertBluetoothPowerAndDuration(
                mStatsRule.getAppsBatteryConsumer(),
                0.33329, 11999, BatteryConsumer.POWER_MODEL_MEASURED_ENERGY);
    }

    private void setDurationsAndPower(
            BatteryStatsImpl.ControllerActivityCounterImpl controllerActivity, int idleDurationMs,
            int rxDurationMs, int txDurationMs, long powerMaMs) {
        controllerActivity.getIdleTimeCounter().addCountLocked(idleDurationMs);
        controllerActivity.getRxTimeCounter().addCountLocked(rxDurationMs);
        controllerActivity.getTxTimeCounters()[0].addCountLocked(txDurationMs);
        controllerActivity.getPowerCounter().addCountLocked(powerMaMs);
                allAppsPowerMah, 11999, powerModelPowerProfile);
    }

    private void assertBluetoothPowerAndDuration(@Nullable BatteryConsumer batteryConsumer,
@@ -162,7 +141,6 @@ public class BluetoothPowerCalculatorTest {

        long usageDurationMillis = batteryConsumer.getUsageDurationMillis(
                BatteryConsumer.POWER_COMPONENT_BLUETOOTH);

        assertThat(usageDurationMillis).isEqualTo(durationMs);
    }
}
Loading