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

Commit f4013aa4 authored by Adam Lesinski's avatar Adam Lesinski
Browse files

BatteryStats: Wifi energy data is sometimes wrong

Wifi energy data sometimes reports negative values. The bug
is filed, but in the meanwhile, batterystats calculations
are way off due to strange values.

Now we ignore invalid data, and sample again later.
Bug:21613534

Change-Id: I3c5c1385e15d85346e6fd3a2737cb58ca706ab74
parent 6cb8e30b
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -1113,23 +1113,32 @@ public final class BatteryStatsService extends IBatteryStats.Stub
            // correct delta from when we should start reading (aka when we are on battery).
            WifiActivityEnergyInfo info = mWifiManager.reportActivityInfo();
            if (info != null && info.isValid()) {
                if (info.mControllerEnergyUsed < 0 || info.mControllerIdleTimeMs < 0 ||
                        info.mControllerRxTimeMs < 0 || info.mControllerTxTimeMs < 0) {
                    Slog.wtf(TAG, "Reported WiFi energy data is invalid: " + info);
                    return null;
                }

                // We will modify the last info object to be the delta, and store the new
                // WifiActivityEnergyInfo object as our last one.
                final WifiActivityEnergyInfo result = mLastInfo;
                result.mTimestamp = info.getTimeStamp();
                result.mStackState = info.getStackState();

                // These times seem to be the most reliable.
                result.mControllerTxTimeMs =
                        info.mControllerTxTimeMs - mLastInfo.mControllerTxTimeMs;
                result.mControllerRxTimeMs =
                        info.mControllerRxTimeMs - mLastInfo.mControllerRxTimeMs;
                result.mControllerEnergyUsed =
                        info.mControllerEnergyUsed - mLastInfo.mControllerEnergyUsed;

                // WiFi calculates the idle time as a difference from the on time and the various
                // Rx + Tx times. There seems to be some missing time there because this sometimes
                // becomes negative. Just cap it at 0 and move on.
                // b/21613534
                result.mControllerIdleTimeMs =
                        Math.max(0, info.mControllerIdleTimeMs - mLastInfo.mControllerIdleTimeMs);
                result.mControllerEnergyUsed =
                        Math.max(0, info.mControllerEnergyUsed - mLastInfo.mControllerEnergyUsed);

                if (result.mControllerTxTimeMs < 0 ||
                        result.mControllerRxTimeMs < 0) {