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

Commit a4766050 authored by Adam Lesinski's avatar Adam Lesinski Committed by Android (Google) Code Review
Browse files

Merge "Report WiFi and Bluetooth energy collection in checkin stats"

parents e4e13b4a e283d33f
Loading
Loading
Loading
Loading
+46 −100
Original line number Diff line number Diff line
@@ -183,7 +183,9 @@ public abstract class BatteryStats implements Parcelable {
    private static final String BATTERY_DATA = "bt";
    private static final String BATTERY_DISCHARGE_DATA = "dc";
    private static final String BATTERY_LEVEL_DATA = "lv";
    private static final String GLOBAL_WIFI_DATA = "gwfl";
    private static final String WIFI_DATA = "wfl";
    private static final String GLOBAL_BLUETOOTH_DATA = "gble";
    private static final String MISC_DATA = "m";
    private static final String GLOBAL_NETWORK_DATA = "gn";
    private static final String HISTORY_STRING_POOL = "hsp";
@@ -200,8 +202,6 @@ public abstract class BatteryStats implements Parcelable {
    private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
    private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
    private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
    private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
    private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
    private static final String POWER_USE_SUMMARY_DATA = "pws";
    private static final String POWER_USE_ITEM_DATA = "pwi";
    private static final String DISCHARGE_STEP_DATA = "dsd";
@@ -1897,43 +1897,6 @@ public abstract class BatteryStats implements Parcelable {
     */
    public abstract int getWifiSignalStrengthCount(int strengthBin, int which);

    /**
     * Returns the time in microseconds that bluetooth has been on while the device was
     * running on battery.
     * 
     * {@hide}
     */
    public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
    
    public abstract int getBluetoothPingCount();

    public static final int BLUETOOTH_STATE_INACTIVE = 0;
    public static final int BLUETOOTH_STATE_LOW = 1;
    public static final int BLUETOOTH_STATE_MEDIUM = 2;
    public static final int BLUETOOTH_STATE_HIGH = 3;

    static final String[] BLUETOOTH_STATE_NAMES = {
        "inactive", "low", "med", "high"
    };

    public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;

    /**
     * Returns the time in microseconds that Bluetooth has been running in the
     * given active state.
     *
     * {@hide}
     */
    public abstract long getBluetoothStateTime(int bluetoothState,
            long elapsedRealtimeUs, int which);

    /**
     * Returns the number of times that Bluetooth has entered the given active state.
     *
     * {@hide}
     */
    public abstract int getBluetoothStateCount(int bluetoothState, int which);

    /**
     * Returns the time in microseconds that the flashlight has been on while the device was
     * running on battery.
@@ -2446,9 +2409,6 @@ public abstract class BatteryStats implements Parcelable {
        final long deviceIdlingTime = getDeviceIdlingTime(rawRealtime, which);
        final int connChanges = getNumConnectivityChange(which);
        final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
        final long wifiOnTime = getWifiOnTime(rawRealtime, which);
        final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
        final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);

        final StringBuilder sb = new StringBuilder(128);
        
@@ -2491,6 +2451,7 @@ public abstract class BatteryStats implements Parcelable {
            }
        }

        // Dump network stats
        final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
        final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
        final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
@@ -2499,19 +2460,34 @@ public abstract class BatteryStats implements Parcelable {
        final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
        final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
        final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);

        // Dump network stats
        dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
                mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
                mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);

        // Dump Wifi controller stats
        final long wifiOnTime = getWifiOnTime(rawRealtime, which);
        final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
        final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
        final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
        final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
        final long wifiPowerMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
        dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA,
                wifiOnTime / 1000, wifiRunningTime / 1000,
                wifiIdleTimeMs, wifiRxTimeMs, wifiTxTimeMs, wifiPowerMaMs / (1000*60*60));

        // Dump Bluetooth controller stats
        final long btIdleTimeMs = getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
        final long btRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
        final long btTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
        final long btPowerMaMs = getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which);
        dumpLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_DATA,
                btIdleTimeMs, btRxTimeMs, btTxTimeMs, btPowerMaMs / (1000*60*60));

        // Dump misc stats
        dumpLine(pw, 0 /* uid */, category, MISC_DATA,
                screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
                wifiRunningTime / 1000, bluetoothOnTime / 1000,
                mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
                screenOnTime / 1000, phoneOnTime / 1000,
                fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
                0 /*legacy input event count*/, getMobileRadioActiveTime(rawRealtime, which) / 1000,
                getMobileRadioActiveTime(rawRealtime, which) / 1000,
                getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
                powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeEnabledTime / 1000,
                getDeviceIdleModeEnabledCount(which), deviceIdlingTime / 1000,
@@ -2581,17 +2557,6 @@ public abstract class BatteryStats implements Parcelable {
        }
        dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);

        // Dump bluetooth state stats
        args = new Object[NUM_BLUETOOTH_STATES];
        for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
            args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
        }
        dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
        for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
            args[i] = getBluetoothStateCount(i, which);
        }
        dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);

        if (which == STATS_SINCE_UNPLUGGED) {
            dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
                    getDischargeCurrentLevel());
@@ -2696,6 +2661,7 @@ public abstract class BatteryStats implements Parcelable {
                continue;
            }
            final Uid u = uidStats.valueAt(iu);

            // Dump Network stats per uid, if any
            final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
            final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
@@ -2707,11 +2673,6 @@ public abstract class BatteryStats implements Parcelable {
            final int mobileActiveCount = u.getMobileRadioActiveCount(which);
            final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
            final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
            final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
            final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
            final int wifiScanCount = u.getWifiScanCount(which);
            final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);

            if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
                    || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
                    || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
@@ -2722,10 +2683,19 @@ public abstract class BatteryStats implements Parcelable {
                        mobileActiveTime, mobileActiveCount);
            }

            final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
            final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
            final int wifiScanCount = u.getWifiScanCount(which);
            final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
            final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
            final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
            final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
            if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
                    || uidWifiRunningTime != 0) {
                    || uidWifiRunningTime != 0 || uidWifiIdleTimeMs != 0 || uidWifiRxTimeMs != 0
                    || uidWifiTxTimeMs != 0) {
                dumpLine(pw, uid, category, WIFI_DATA,
                        fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime, wifiScanCount);
                        fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime, wifiScanCount,
                        uidWifiIdleTimeMs, uidWifiRxTimeMs, uidWifiTxTimeMs);
            }

            if (u.hasUserActivity()) {
@@ -2983,7 +2953,6 @@ public abstract class BatteryStats implements Parcelable {
        final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
        final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
        final long wifiOnTime = getWifiOnTime(rawRealtime, which);
        final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
        sb.setLength(0);
        sb.append(prefix);
                sb.append("  Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
@@ -3332,42 +3301,11 @@ public abstract class BatteryStats implements Parcelable {

        sb.setLength(0);
        sb.append(prefix);
        sb.append("  WiFi Energy use: ").append(BatteryStatsHelper.makemAh(
        sb.append("  WiFi Power drain: ").append(BatteryStatsHelper.makemAh(
                getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which) / (double)(1000*60*60)));
        sb.append(" mAh");
        pw.println(sb.toString());

        sb.setLength(0);
        sb.append(prefix);
                sb.append("  Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
                sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
                sb.append(")");
        pw.println(sb.toString());

        sb.setLength(0);
        sb.append(prefix);
        sb.append("  Bluetooth states:");
        didOne = false;
        for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
            final long time = getBluetoothStateTime(i, rawRealtime, which);
            if (time == 0) {
                continue;
            }
            sb.append("\n    ");
            didOne = true;
            sb.append(BLUETOOTH_STATE_NAMES[i]);
            sb.append(" ");
            formatTimeMs(sb, time/1000);
            sb.append("(");
            sb.append(formatRatioLocked(time, whichBatteryRealtime));
            sb.append(") ");
            sb.append(getPhoneDataConnectionCount(i, which));
            sb.append("x");
        }

        if (!didOne) sb.append(" (no activity)");
        pw.println(sb.toString());

        final long bluetoothIdleTimeMs =
                getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
        final long bluetoothRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
@@ -3399,6 +3337,14 @@ public abstract class BatteryStats implements Parcelable {
        sb.append(")");
        pw.println(sb.toString());

        sb.setLength(0);
        sb.append(prefix);
        sb.append("  Bluetooth Power drain: ").append(BatteryStatsHelper.makemAh(
                getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which) /
                        (double)(1000*60*60)));
        sb.append(" mAh");
        pw.println(sb.toString());

        pw.println();

        if (which == STATS_SINCE_UNPLUGGED) {
+0 −3
Original line number Diff line number Diff line
@@ -95,9 +95,6 @@ interface IBatteryStats {
    void noteWifiState(int wifiState, String accessPoint);
    void noteWifiSupplicantStateChanged(int supplState, boolean failedAuth);
    void noteWifiRssiChanged(int newRssi);
    void noteBluetoothOn();
    void noteBluetoothOff();
    void noteBluetoothState(int bluetoothState);
    void noteFullWifiLockAcquired(int uid);
    void noteFullWifiLockReleased(int uid);
    void noteWifiScanStarted(int uid);
+15 −2
Original line number Diff line number Diff line
@@ -136,6 +136,14 @@ public final class BatteryStatsHelper {
                profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_TX) != 0;
    }

    public static boolean checkHasBluetoothPowerReporting(BatteryStats stats,
                                                          PowerProfile profile) {
        return stats.hasBluetoothActivityReporting() &&
                profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_IDLE) != 0 &&
                profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_RX) != 0 &&
                profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_TX) != 0;
    }

    public BatteryStatsHelper(Context context) {
        this(context, true);
    }
@@ -256,7 +264,8 @@ public final class BatteryStatsHelper {
    }

    public static String makemAh(double power) {
        if (power < .00001) return String.format("%.8f", power);
        if (power == 0) return "0";
        else if (power < .00001) return String.format("%.8f", power);
        else if (power < .0001) return String.format("%.7f", power);
        else if (power < .001) return String.format("%.6f", power);
        else if (power < .01) return String.format("%.5f", power);
@@ -342,7 +351,11 @@ public final class BatteryStatsHelper {
        mWifiPowerCalculator.reset();

        if (mBluetoothPowerCalculator == null) {
            if (checkHasBluetoothPowerReporting(mStats, mPowerProfile)) {
                mBluetoothPowerCalculator = new BluetoothPowerCalculator();
            } else {
                mBluetoothPowerCalculator = new BluetoothPowerCalculator();
            }
        }
        mBluetoothPowerCalculator.reset();

+14 −160

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ public class WifiPowerCalculator extends PowerCalculator {
        app.wifiRunningTimeMs = idleTimeMs + rxTimeMs + txTimeMs;

        double powerDrain = stats.getWifiControllerActivity(BatteryStats.CONTROLLER_POWER_DRAIN,
                statsType) / (1000*60*60);
                statsType) / (double)(1000*60*60);
        if (powerDrain == 0) {
            // Some controllers do not report power drain, so we can calculate it here.
            powerDrain = ((idleTimeMs * mIdleCurrentMa) + (txTimeMs * mTxCurrentMa)
Loading