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

Commit 98ecee97 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Re-enable Bluetooth stats reporting in dumpsys" into main

parents 33798902 b65fb214
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -11447,7 +11447,7 @@ public class BatteryStatsImpl extends BatteryStats {
        mWifiPowerStatsCollector.addConsumer(this::recordPowerStats);
        mBluetoothPowerStatsCollector = new BluetoothPowerStatsCollector(
                mPowerStatsCollectorInjector);
                mPowerStatsCollectorInjector, this::onBluetoothPowerStatsRetrieved);
        mBluetoothPowerStatsCollector.addConsumer(this::recordPowerStats);
        mCameraPowerStatsCollector = new CameraPowerStatsCollector(mPowerStatsCollectorInjector);
@@ -13417,6 +13417,13 @@ public class BatteryStatsImpl extends BatteryStats {
    private final BluetoothActivityInfoCache mLastBluetoothActivityInfo
            = new BluetoothActivityInfoCache();
    private void onBluetoothPowerStatsRetrieved(BluetoothActivityEnergyInfo info,
            long elapsedRealtimeMs, long uptimeMs) {
        // Do not populate consumed energy, because energy attribution is done by
        // BluetoothPowerStatsProcessor.
        updateBluetoothStateLocked(info, POWER_DATA_UNAVAILABLE, elapsedRealtimeMs, uptimeMs);
    }
    /**
     * Distribute Bluetooth energy info and network traffic to apps.
     *
@@ -13425,10 +13432,6 @@ public class BatteryStatsImpl extends BatteryStats {
    @GuardedBy("this")
    public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info,
            final long consumedChargeUC, long elapsedRealtimeMs, long uptimeMs) {
        if (mBluetoothPowerStatsCollector.isEnabled()) {
            return;
        }
        if (DEBUG_ENERGY) {
            Slog.d(TAG, "Updating bluetooth stats: " + info);
        }
+19 −6
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.server.power.stats;

import android.annotation.Nullable;
import android.bluetooth.BluetoothActivityEnergyInfo;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.UidTraffic;
@@ -41,7 +42,10 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector {

    private static final long BLUETOOTH_ACTIVITY_REQUEST_TIMEOUT = 20000;

    private static final long ENERGY_UNSPECIFIED = -1;
    interface Observer {
        void onBluetoothPowerStatsRetrieved(@Nullable BluetoothActivityEnergyInfo info,
                long elapsedRealtimeMs, long uptimeMs);
    }

    public interface BluetoothStatsRetriever {
        interface Callback {
@@ -65,6 +69,7 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector {
    }

    private final Injector mInjector;
    private final Observer mObserver;

    private com.android.server.power.stats.format.BluetoothPowerStatsLayout mLayout;
    private boolean mIsInitialized;
@@ -89,13 +94,14 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector {

    private final SparseArray<UidStats> mUidStats = new SparseArray<>();

    public BluetoothPowerStatsCollector(Injector injector) {
    public BluetoothPowerStatsCollector(Injector injector, @Nullable Observer observer) {
        super(injector.getHandler(),  injector.getPowerStatsCollectionThrottlePeriod(
                        BatteryConsumer.powerComponentIdToString(
                                BatteryConsumer.POWER_COMPONENT_BLUETOOTH)),
                injector.getUidResolver(),
                injector.getClock());
        mInjector = injector;
        mObserver = observer;
    }

    @Override
@@ -146,15 +152,20 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector {
        Arrays.fill(mDeviceStats, 0);
        mPowerStats.uidStats.clear();

        collectBluetoothActivityInfo();
        BluetoothActivityEnergyInfo activityInfo = collectBluetoothActivityInfo();
        collectBluetoothScanStats();

        mConsumedEnergyHelper.collectConsumedEnergy(mPowerStats, mLayout);

        if (mObserver != null) {
            mObserver.onBluetoothPowerStatsRetrieved(activityInfo, mClock.elapsedRealtime(),
                    mClock.uptimeMillis());
        }

        return mPowerStats;
    }

    private void collectBluetoothActivityInfo() {
    private BluetoothActivityEnergyInfo collectBluetoothActivityInfo() {
        CompletableFuture<BluetoothActivityEnergyInfo> immediateFuture = new CompletableFuture<>();
        boolean success = mBluetoothStatsRetriever.requestControllerActivityEnergyInfo(
                Runnable::run,
@@ -173,7 +184,7 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector {
                });

        if (!success) {
            return;
            return null;
        }

        BluetoothActivityEnergyInfo activityInfo;
@@ -186,7 +197,7 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector {
        }

        if (activityInfo == null) {
            return;
            return null;
        }

        long rxTime = activityInfo.getControllerRxTimeMillis();
@@ -241,6 +252,8 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector {
                mLayout.setUidTxBytes(stats, txDelta);
            }
        }

        return activityInfo;
    }

    private void collectBluetoothScanStats() {
+10 −2
Original line number Diff line number Diff line
@@ -225,7 +225,10 @@ public class BluetoothPowerStatsCollectorTest {
    }

    private PowerStats collectPowerStats() {
        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector);
        List<BluetoothActivityEnergyInfo> expected = new ArrayList<>();
        List<BluetoothActivityEnergyInfo> observed = new ArrayList<>();
        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector,
                (info, elapsedRealtimeMs, uptimeMs) -> observed.add(info));
        collector.setEnabled(true);

        when(mConsumedEnergyRetriever.getVoltageMv()).thenReturn(3500);
@@ -236,6 +239,7 @@ public class BluetoothPowerStatsCollectorTest {
                mockUidTraffic(APP_UID1, 100, 200),
                mockUidTraffic(APP_UID2, 300, 400),
                mockUidTraffic(ISOLATED_UID, 500, 600));
        expected.add(mBluetoothActivityEnergyInfo);

        mUidScanTimes.put(APP_UID1, 100);

@@ -248,6 +252,7 @@ public class BluetoothPowerStatsCollectorTest {
                mockUidTraffic(APP_UID1, 1100, 2200),
                mockUidTraffic(APP_UID2, 3300, 4400),
                mockUidTraffic(ISOLATED_UID, 5500, 6600));
        expected.add(mBluetoothActivityEnergyInfo);

        mUidScanTimes.clear();
        mUidScanTimes.put(APP_UID1, 200);
@@ -257,7 +262,10 @@ public class BluetoothPowerStatsCollectorTest {
        mockConsumedEnergy(777, 64321);

        mStatsRule.setTime(20000, 20000);
        return collector.collectStats();
        PowerStats powerStats = collector.collectStats();

        assertThat(observed).isEqualTo(expected);
        return powerStats;
    }

    private void mockConsumedEnergy(int consumerId, long energyUWs) {
+3 −3
Original line number Diff line number Diff line
@@ -170,7 +170,7 @@ public class BluetoothPowerStatsProcessorTest {
        PowerComponentAggregatedPowerStats aggregatedStats = createAggregatedPowerStats(
                () -> new BluetoothPowerStatsProcessor(mStatsRule.getPowerProfile()));

        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector);
        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector, null);
        collector.setEnabled(true);
        mBluetoothActivityEnergyInfo = mockBluetoothActivityEnergyInfo(1000, 600, 100, 200,
                mockUidTraffic(APP_UID1, 100, 200),
@@ -271,7 +271,7 @@ public class BluetoothPowerStatsProcessorTest {
        PowerComponentAggregatedPowerStats aggregatedStats = createAggregatedPowerStats(
                () -> new BluetoothPowerStatsProcessor(mStatsRule.getPowerProfile()));

        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector);
        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector, null);
        collector.setEnabled(true);
        mBluetoothActivityEnergyInfo = mockBluetoothActivityEnergyInfo(1000, 600, 100, 200,
                mockUidTraffic(APP_UID1, 100, 200),
@@ -371,7 +371,7 @@ public class BluetoothPowerStatsProcessorTest {
        PowerComponentAggregatedPowerStats aggregatedStats = createAggregatedPowerStats(
                () -> new BluetoothPowerStatsProcessor(mStatsRule.getPowerProfile()));

        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector);
        BluetoothPowerStatsCollector collector = new BluetoothPowerStatsCollector(mInjector, null);
        collector.setEnabled(true);
        mBluetoothActivityEnergyInfo = mockBluetoothActivityEnergyInfo(1000, 600, 100, 200,
                mockUidTraffic(APP_UID1, 100, 200),