From 1beb4fcdbd1e82ba934ebcfacee626d7c16133ec Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Mon, 21 Jun 2021 17:18:38 -0700 Subject: [PATCH] Fix Bluetooth power accounting after a BT crash Bug: 191488536 Test: Use BatteryStatsViewer to take a note of BT usage time $ adb shell killall com.android.bluetooth Use BatteryStatsViewer to verify that BT usage time is going up Change-Id: I8438f19bb4bd6365da6d582fb7347f0a2cbddc1c --- .../android/internal/os/BatteryStatsImpl.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 945a6ab118565..dab3e9fa15fa8 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -12141,6 +12141,15 @@ public class BatteryStatsImpl extends BatteryStats { } } } + + void reset() { + idleTimeMs = 0; + rxTimeMs = 0; + txTimeMs = 0; + energy = 0; + uidRxBytes.clear(); + uidTxBytes.clear(); + } } private final BluetoothActivityInfoCache mLastBluetoothActivityInfo @@ -12167,6 +12176,15 @@ public class BatteryStatsImpl extends BatteryStats { mHasBluetoothReporting = true; + if (info.getControllerRxTimeMillis() < mLastBluetoothActivityInfo.rxTimeMs + || info.getControllerTxTimeMillis() < mLastBluetoothActivityInfo.txTimeMs + || info.getControllerIdleTimeMillis() < mLastBluetoothActivityInfo.idleTimeMs + || info.getControllerEnergyUsed() < mLastBluetoothActivityInfo.energy) { + // A drop in accumulated Bluetooth stats is a sign of a Bluetooth crash. + // Reset the preserved previous snapshot in order to restart accumulating deltas. + mLastBluetoothActivityInfo.reset(); + } + final long rxTimeMs = info.getControllerRxTimeMillis() - mLastBluetoothActivityInfo.rxTimeMs; final long txTimeMs =