Loading services/core/java/com/android/server/power/stats/BatteryStatsImpl.java +8 −5 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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. * Loading @@ -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); } services/core/java/com/android/server/power/stats/BluetoothPowerStatsCollector.java +19 −6 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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 { Loading @@ -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; Loading @@ -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 Loading Loading @@ -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, Loading @@ -173,7 +184,7 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector { }); if (!success) { return; return null; } BluetoothActivityEnergyInfo activityInfo; Loading @@ -186,7 +197,7 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector { } if (activityInfo == null) { return; return null; } long rxTime = activityInfo.getControllerRxTimeMillis(); Loading Loading @@ -241,6 +252,8 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector { mLayout.setUidTxBytes(stats, txDelta); } } return activityInfo; } private void collectBluetoothScanStats() { Loading services/tests/powerstatstests/src/com/android/server/power/stats/BluetoothPowerStatsCollectorTest.java +10 −2 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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); Loading @@ -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); Loading @@ -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) { Loading services/tests/powerstatstests/src/com/android/server/power/stats/processor/BluetoothPowerStatsProcessorTest.java +3 −3 Original line number Diff line number Diff line Loading @@ -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), Loading Loading @@ -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), Loading Loading @@ -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), Loading Loading
services/core/java/com/android/server/power/stats/BatteryStatsImpl.java +8 −5 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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. * Loading @@ -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); }
services/core/java/com/android/server/power/stats/BluetoothPowerStatsCollector.java +19 −6 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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 { Loading @@ -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; Loading @@ -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 Loading Loading @@ -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, Loading @@ -173,7 +184,7 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector { }); if (!success) { return; return null; } BluetoothActivityEnergyInfo activityInfo; Loading @@ -186,7 +197,7 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector { } if (activityInfo == null) { return; return null; } long rxTime = activityInfo.getControllerRxTimeMillis(); Loading Loading @@ -241,6 +252,8 @@ public class BluetoothPowerStatsCollector extends PowerStatsCollector { mLayout.setUidTxBytes(stats, txDelta); } } return activityInfo; } private void collectBluetoothScanStats() { Loading
services/tests/powerstatstests/src/com/android/server/power/stats/BluetoothPowerStatsCollectorTest.java +10 −2 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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); Loading @@ -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); Loading @@ -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) { Loading
services/tests/powerstatstests/src/com/android/server/power/stats/processor/BluetoothPowerStatsProcessorTest.java +3 −3 Original line number Diff line number Diff line Loading @@ -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), Loading Loading @@ -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), Loading Loading @@ -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), Loading