summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2024-12-11 19:16:15 -0800
committer Android (Google) Code Review <android-gerrit@google.com> 2024-12-11 19:16:15 -0800
commit98ecee975bd70bef541677b45ecb2a9fc3af2e56 (patch)
tree1f3cbbcc18065ca61e0add41689d129e84d9b6da
parent33798902cc9bbb813456c096af000d2b6c99780c (diff)
parentb65fb2149f68e1a22c2aa0db8693369705b8357d (diff)
Merge "Re-enable Bluetooth stats reporting in dumpsys" into main
-rw-r--r--services/core/java/com/android/server/power/stats/BatteryStatsImpl.java13
-rw-r--r--services/core/java/com/android/server/power/stats/BluetoothPowerStatsCollector.java25
-rw-r--r--services/tests/powerstatstests/src/com/android/server/power/stats/BluetoothPowerStatsCollectorTest.java12
-rw-r--r--services/tests/powerstatstests/src/com/android/server/power/stats/processor/BluetoothPowerStatsProcessorTest.java6
4 files changed, 40 insertions, 16 deletions
diff --git a/services/core/java/com/android/server/power/stats/BatteryStatsImpl.java b/services/core/java/com/android/server/power/stats/BatteryStatsImpl.java
index c26eeed6be35..fe14f6b172f1 100644
--- a/services/core/java/com/android/server/power/stats/BatteryStatsImpl.java
+++ b/services/core/java/com/android/server/power/stats/BatteryStatsImpl.java
@@ -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);
}
diff --git a/services/core/java/com/android/server/power/stats/BluetoothPowerStatsCollector.java b/services/core/java/com/android/server/power/stats/BluetoothPowerStatsCollector.java
index d7aa9876fe0d..c12ae63140c9 100644
--- a/services/core/java/com/android/server/power/stats/BluetoothPowerStatsCollector.java
+++ b/services/core/java/com/android/server/power/stats/BluetoothPowerStatsCollector.java
@@ -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() {
diff --git a/services/tests/powerstatstests/src/com/android/server/power/stats/BluetoothPowerStatsCollectorTest.java b/services/tests/powerstatstests/src/com/android/server/power/stats/BluetoothPowerStatsCollectorTest.java
index e392c5d190f7..3895cb480847 100644
--- a/services/tests/powerstatstests/src/com/android/server/power/stats/BluetoothPowerStatsCollectorTest.java
+++ b/services/tests/powerstatstests/src/com/android/server/power/stats/BluetoothPowerStatsCollectorTest.java
@@ -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) {
diff --git a/services/tests/powerstatstests/src/com/android/server/power/stats/processor/BluetoothPowerStatsProcessorTest.java b/services/tests/powerstatstests/src/com/android/server/power/stats/processor/BluetoothPowerStatsProcessorTest.java
index 2c580e5ab0d2..60131861ce89 100644
--- a/services/tests/powerstatstests/src/com/android/server/power/stats/processor/BluetoothPowerStatsProcessorTest.java
+++ b/services/tests/powerstatstests/src/com/android/server/power/stats/processor/BluetoothPowerStatsProcessorTest.java
@@ -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),