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

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

Merge "Track BLE scans in BatteryStats"

parents ea2a9ee0 9f55cc71
Loading
Loading
Loading
Loading
+41 −3
Original line number Diff line number Diff line
@@ -146,6 +146,11 @@ public abstract class BatteryStats implements Parcelable {
     */
    public static final int WAKE_TYPE_DRAW = 18;

    /**
     * A constant indicating a bluetooth scan timer.
     */
    public static final int BLUETOOTH_SCAN_ON = 19;

    /**
     * Include all of the data in the stats, including previously saved data.
     */
@@ -438,6 +443,7 @@ public abstract class BatteryStats implements Parcelable {
        public abstract Timer getFlashlightTurnedOnTimer();
        public abstract Timer getCameraTurnedOnTimer();
        public abstract Timer getForegroundActivityTimer();
        public abstract Timer getBluetoothScanTimer();

        // Time this uid has any processes in the top state.
        public static final int PROCESS_STATE_TOP = 0;
@@ -1179,6 +1185,7 @@ public abstract class BatteryStats implements Parcelable {
        public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
        public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
        public static final int STATE2_CAMERA_FLAG = 1<<21;
        public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;

        public static final int MOST_INTERESTING_STATES2 =
            STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
@@ -1922,6 +1929,7 @@ public abstract class BatteryStats implements Parcelable {
                HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
                WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
        new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
        new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
    };

    public static final String[] HISTORY_EVENT_NAMES = new String[] {
@@ -2041,6 +2049,13 @@ public abstract class BatteryStats implements Parcelable {
     */
    public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);

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

    public static final int NETWORK_MOBILE_RX_DATA = 0;
    public static final int NETWORK_MOBILE_TX_DATA = 1;
@@ -2797,9 +2812,12 @@ 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);
        final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
        final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
        dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
                mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
                mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
                mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
                btRxTotalBytes, btTxTotalBytes);

        // Dump Modem controller stats
        dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
@@ -3017,14 +3035,18 @@ 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 btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
            final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
            if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
                    || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
                    || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
                    || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
                    || btBytesRx > 0 || btBytesTx > 0) {
                dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
                        wifiBytesRx, wifiBytesTx,
                        mobilePacketsRx, mobilePacketsTx,
                        wifiPacketsRx, wifiPacketsTx,
                        mobileActiveTime, mobileActiveCount);
                        mobileActiveTime, mobileActiveCount,
                        btBytesRx, btBytesTx);
            }

            // Dump modem controller data, per UID.
@@ -3046,6 +3068,9 @@ public abstract class BatteryStats implements Parcelable {
            dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
                    u.getWifiControllerActivity(), which);

            dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
                    u.getBluetoothControllerActivity(), which);

            if (u.hasUserActivity()) {
                args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
                boolean hasData = false;
@@ -3668,6 +3693,12 @@ public abstract class BatteryStats implements Parcelable {
        pw.print("  Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
        pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));

        final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
        sb.setLength(0);
        sb.append(prefix);
        sb.append("  Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
        pw.println(sb.toString());

        printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
                which);

@@ -3793,6 +3824,10 @@ public abstract class BatteryStats implements Parcelable {
                        pw.print(" wifi=");
                        printmAh(pw, bs.wifiPowerMah);
                    }
                    if (bs.bluetoothPowerMah != 0) {
                        pw.print(" bt=");
                        printmAh(pw, bs.bluetoothPowerMah);
                    }
                    if (bs.gpsPowerMah != 0) {
                        pw.print(" gps=");
                        printmAh(pw, bs.gpsPowerMah);
@@ -4035,6 +4070,9 @@ public abstract class BatteryStats implements Parcelable {
                pw.println(" sent");
            }

            uidActivity |= printTimer(pw, sb, u.getBluetoothScanTimer(), rawRealtime, which, prefix,
                    "Bluetooth Scan");

            if (u.hasUserActivity()) {
                boolean hasData = false;
                for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
+1 −0
Original line number Diff line number Diff line
@@ -124,4 +124,5 @@ interface IBatteryStats {

    void noteBleScanStarted(in WorkSource ws);
    void noteBleScanStopped(in WorkSource ws);
    void noteResetBleScan();
}
+9 −1
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ public class BatterySipper implements Comparable<BatterySipper> {
    public long wakeLockTimeMs;
    public long cameraTimeMs;
    public long flashlightTimeMs;
    public long bluetoothRunningTimeMs;

    public long mobileRxPackets;
    public long mobileTxPackets;
@@ -56,6 +57,8 @@ public class BatterySipper implements Comparable<BatterySipper> {
    public long mobileTxBytes;
    public long wifiRxBytes;
    public long wifiTxBytes;
    public long btRxBytes;
    public long btTxBytes;
    public double percent;
    public double noCoveragePercent;
    public String[] mPackages;
@@ -71,6 +74,7 @@ public class BatterySipper implements Comparable<BatterySipper> {
    public double sensorPowerMah;
    public double cameraPowerMah;
    public double flashlightPowerMah;
    public double bluetoothPowerMah;

    public enum DrainType {
        IDLE,
@@ -142,6 +146,7 @@ public class BatterySipper implements Comparable<BatterySipper> {
        wakeLockTimeMs += other.wakeLockTimeMs;
        cameraTimeMs += other.cameraTimeMs;
        flashlightTimeMs += other.flashlightTimeMs;
        bluetoothRunningTimeMs += other.bluetoothRunningTimeMs;
        mobileRxPackets += other.mobileRxPackets;
        mobileTxPackets += other.mobileTxPackets;
        mobileActive += other.mobileActive;
@@ -152,6 +157,8 @@ public class BatterySipper implements Comparable<BatterySipper> {
        mobileTxBytes += other.mobileTxBytes;
        wifiRxBytes += other.wifiRxBytes;
        wifiTxBytes += other.wifiTxBytes;
        btRxBytes += other.btRxBytes;
        btTxBytes += other.btTxBytes;
        wifiPowerMah += other.wifiPowerMah;
        gpsPowerMah += other.gpsPowerMah;
        cpuPowerMah += other.cpuPowerMah;
@@ -160,6 +167,7 @@ public class BatterySipper implements Comparable<BatterySipper> {
        wakeLockPowerMah += other.wakeLockPowerMah;
        cameraPowerMah += other.cameraPowerMah;
        flashlightPowerMah += other.flashlightPowerMah;
        bluetoothPowerMah += other.bluetoothPowerMah;
    }

    /**
@@ -169,6 +177,6 @@ public class BatterySipper implements Comparable<BatterySipper> {
    public double sumPower() {
        return totalPowerMah = usagePowerMah + wifiPowerMah + gpsPowerMah + cpuPowerMah +
                sensorPowerMah + mobileRadioPowerMah + wakeLockPowerMah + cameraPowerMah +
                flashlightPowerMah;
                flashlightPowerMah + bluetoothPowerMah;
    }
}
+303 −45

File changed.

Preview size limit exceeded, changes collapsed.

+38 −3
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ public class BluetoothPowerCalculator extends PowerCalculator {
    private final double mIdleMa;
    private final double mRxMa;
    private final double mTxMa;
    private double mAppTotalPowerMah = 0;
    private long mAppTotalTimeMs = 0;

    public BluetoothPowerCalculator(PowerProfile profile) {
        mIdleMa = profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_IDLE);
@@ -34,7 +36,31 @@ public class BluetoothPowerCalculator extends PowerCalculator {
    @Override
    public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
                             long rawUptimeUs, int statsType) {
        // No per-app distribution yet.

        final BatteryStats.ControllerActivityCounter counter = u.getBluetoothControllerActivity();
        if (counter == null) {
            return;
        }

        final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(statsType);
        final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(statsType);
        final long txTimeMs = counter.getTxTimeCounters()[0].getCountLocked(statsType);
        final long totalTimeMs = idleTimeMs + txTimeMs + rxTimeMs;
        double powerMah = counter.getPowerCounter().getCountLocked(statsType)
                / (double)(1000*60*60);

        if (powerMah == 0) {
            powerMah = ((idleTimeMs * mIdleMa) + (rxTimeMs * mRxMa) + (txTimeMs * mTxMa))
                    / (1000*60*60);
        }

        app.bluetoothPowerMah = powerMah;
        app.bluetoothRunningTimeMs = totalTimeMs;
        app.btRxBytes = u.getNetworkActivityBytes(BatteryStats.NETWORK_BT_RX_DATA, statsType);
        app.btTxBytes = u.getNetworkActivityBytes(BatteryStats.NETWORK_BT_TX_DATA, statsType);

        mAppTotalPowerMah += powerMah;
        mAppTotalTimeMs += totalTimeMs;
    }

    @Override
@@ -56,12 +82,21 @@ public class BluetoothPowerCalculator extends PowerCalculator {
                    / (1000*60*60);
        }

        // Subtract what the apps used, but clamp to 0.
        powerMah = Math.max(0, powerMah - mAppTotalPowerMah);

        if (DEBUG && powerMah != 0) {
            Log.d(TAG, "Bluetooth active: time=" + (totalTimeMs)
                    + " power=" + BatteryStatsHelper.makemAh(powerMah));
        }

        app.usagePowerMah = powerMah;
        app.usageTimeMs = totalTimeMs;
        app.bluetoothPowerMah = powerMah;
        app.bluetoothRunningTimeMs = Math.max(0, totalTimeMs - mAppTotalTimeMs);
    }

    @Override
    public void reset() {
        mAppTotalPowerMah = 0;
        mAppTotalTimeMs = 0;
    }
}
Loading