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

Commit 545e8b38 authored by Vinay Kalia's avatar Vinay Kalia
Browse files

Revert "Improve BLE scan attribution to facilitate debugging power issues"

This reverts commit 30803361.

Change-Id: I9f08c85f7c140b2075ded97764e4ff3eb9ec6fa4
parent 30803361
Loading
Loading
Loading
Loading
+66 −98
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;

import com.android.bluetooth.btservice.BluetoothProto;
/**
@@ -56,17 +55,15 @@ import com.android.bluetooth.btservice.BluetoothProto;
        boolean background;
        boolean filtered;
        int results;
        int scannerId;

        public LastScan(long timestamp, long duration, boolean opportunistic, boolean background,
                boolean filtered, int scannerId) {
                boolean filtered) {
            this.duration = duration;
            this.timestamp = timestamp;
            this.opportunistic = opportunistic;
            this.background = background;
            this.filtered = filtered;
            this.results = 0;
            this.scannerId = scannerId;
        }
    }

@@ -85,13 +82,12 @@ import com.android.bluetooth.btservice.BluetoothProto;
    WorkSource workSource; // Used for BatteryStats
    int scansStarted = 0;
    int scansStopped = 0;
    boolean isScanning = false;
    boolean isRegistered = false;
    long minScanTime = Long.MAX_VALUE;
    long maxScanTime = 0;
    long mScanStartTime = 0;
    long mTotalScanTime = 0;
    List<LastScan> lastScans = new ArrayList<LastScan>(NUM_SCAN_DURATIONS_KEPT);
    HashMap<Integer, LastScan> ongoingScans = new HashMap<Integer, LastScan>();
    long totalScanTime = 0;
    List<LastScan> lastScans = new ArrayList<LastScan>(NUM_SCAN_DURATIONS_KEPT + 1);
    long startTime = 0;
    long stopTime = 0;
    int results = 0;
@@ -109,10 +105,9 @@ import com.android.bluetooth.btservice.BluetoothProto;
        workSource = source;
    }

    synchronized void addResult(int scannerId) {
        LastScan scan = getScanFromScannerId(scannerId);
        if (scan != null) {
            int batteryStatsResults = ++scan.results;
    synchronized void addResult() {
        if (!lastScans.isEmpty()) {
            int batteryStatsResults = ++lastScans.get(lastScans.size() - 1).results;

            // Only update battery stats after receiving 100 new results in order
            // to lower the cost of the binder transaction
@@ -128,27 +123,20 @@ import com.android.bluetooth.btservice.BluetoothProto;
        results++;
    }

    boolean isScanning() {
        return !ongoingScans.isEmpty();
    }

    LastScan getScanFromScannerId(int scannerId) {
        return ongoingScans.get(scannerId);
    }

    synchronized void recordScanStart(ScanSettings settings, boolean filtered, int scannerId) {
        LastScan existingScan = getScanFromScannerId(scannerId);
        if (existingScan != null) {
    synchronized void recordScanStart(ScanSettings settings, boolean filtered) {
        if (isScanning)
            return;
        }

        this.scansStarted++;
        isScanning = true;
        startTime = SystemClock.elapsedRealtime();

        LastScan scan = new LastScan(startTime, 0, false, false, filtered, scannerId);
        LastScan scan = new LastScan(startTime, 0, false, false, filtered);
        if (settings != null) {
          scan.opportunistic = settings.getScanMode() == ScanSettings.SCAN_MODE_OPPORTUNISTIC;
          scan.background = (settings.getCallbackType() & ScanSettings.CALLBACK_TYPE_FIRST_MATCH) != 0;
        }
        lastScans.add(scan);

        BluetoothProto.ScanEvent scanEvent = new BluetoothProto.ScanEvent();
        scanEvent.setScanEventType(BluetoothProto.ScanEvent.SCAN_EVENT_START);
@@ -157,33 +145,33 @@ import com.android.bluetooth.btservice.BluetoothProto;
        scanEvent.setInitiator(truncateAppName(appName));
        gattService.addScanEvent(scanEvent);

        if (!isScanning()) {
        try {
            boolean isUnoptimized = !(scan.filtered || scan.background || scan.opportunistic);
                mScanStartTime = startTime;
            batteryStats.noteBleScanStarted(workSource, isUnoptimized);
        } catch (RemoteException e) {
            /* ignore */
        }
    }

        ongoingScans.put(scannerId, scan);
    }

    synchronized void recordScanStop(int scannerId) {
        LastScan scan = getScanFromScannerId(scannerId);
        if (scan == null) {
    synchronized void recordScanStop() {
        if (!isScanning)
          return;
        }

        this.scansStopped++;
        isScanning = false;
        stopTime = SystemClock.elapsedRealtime();
        long scanDuration = stopTime - scan.timestamp;
        scan.duration = scanDuration;
        ongoingScans.remove(scannerId);
        if (lastScans.size() >= NUM_SCAN_DURATIONS_KEPT) {
        long scanDuration = stopTime - startTime;

        minScanTime = Math.min(scanDuration, minScanTime);
        maxScanTime = Math.max(scanDuration, maxScanTime);
        totalScanTime += scanDuration;

        LastScan curr = lastScans.get(lastScans.size() - 1);
        curr.duration = scanDuration;

        if (lastScans.size() > NUM_SCAN_DURATIONS_KEPT) {
            lastScans.remove(0);
        }
        lastScans.add(scan);

        BluetoothProto.ScanEvent scanEvent = new BluetoothProto.ScanEvent();
        scanEvent.setScanEventType(BluetoothProto.ScanEvent.SCAN_EVENT_STOP);
@@ -192,28 +180,23 @@ import com.android.bluetooth.btservice.BluetoothProto;
        scanEvent.setInitiator(truncateAppName(appName));
        gattService.addScanEvent(scanEvent);

        if (!isScanning()) {
        try {
                long totalDuration = stopTime - mScanStartTime;
                mTotalScanTime += totalDuration;
                minScanTime = Math.min(totalDuration, minScanTime);
                maxScanTime = Math.max(totalDuration, maxScanTime);
            // Inform battery stats of any results it might be missing on
            // scan stop
                batteryStats.noteBleScanResults(workSource, scan.results % 100);
            batteryStats.noteBleScanResults(workSource, curr.results % 100);
            batteryStats.noteBleScanStopped(workSource);
        } catch (RemoteException e) {
            /* ignore */
        }
    }
    }

    synchronized void setScanTimeout(int scannerId) {
        if (!isScanning()) return;
    synchronized void setScanTimeout() {
        if (!isScanning)
          return;

        LastScan scan = getScanFromScannerId(scannerId);
        if (scan != null) {
            scan.timeout = true;
        if (!lastScans.isEmpty()) {
            LastScan curr = lastScans.get(lastScans.size() - 1);
            curr.timeout = true;
        }
    }

@@ -227,10 +210,11 @@ import com.android.bluetooth.btservice.BluetoothProto;
    }

    synchronized boolean isScanningTooLong() {
        if (!isScanning()) {
        if (lastScans.isEmpty() || !isScanning) {
            return false;
        }
        return (SystemClock.elapsedRealtime() - mScanStartTime) > SCAN_TIMEOUT_MS;

        return (SystemClock.elapsedRealtime() - startTime) > SCAN_TIMEOUT_MS;
    }

    // This function truncates the app name for privacy reasons. Apps with
@@ -259,24 +243,19 @@ import com.android.bluetooth.btservice.BluetoothProto;
        long minScan = minScanTime;
        long scanDuration = 0;

        if (isScanning()) {
            scanDuration = currTime - mScanStartTime;
        }
        if (isScanning) {
            scanDuration = currTime - startTime;
            minScan = Math.min(scanDuration, minScan);
            maxScan = Math.max(scanDuration, maxScan);
        }

        if (minScan == Long.MAX_VALUE) {
            minScan = 0;
        }

        /*TODO: Average scan time can be skewed for
         * multiple scan clients. It will show less than
         * actual value.
         * */
        long avgScan = 0;
        long totalScanTime = mTotalScanTime + scanDuration;
        if (scansStarted > 0) {
            avgScan = totalScanTime / scansStarted;
            avgScan = (totalScanTime + scanDuration) / scansStarted;
        }

        sb.append("  " + appName);
@@ -302,14 +281,16 @@ import com.android.bluetooth.btservice.BluetoothProto;
        sb.append("  Total number of results            : " +
                  results + "\n");

        long currentTime = System.currentTimeMillis();
        long elapsedRt = SystemClock.elapsedRealtime();
        if (!lastScans.isEmpty()) {
            sb.append("  Last " + lastScans.size() + " scans                       :\n");
            int lastScansSize = scansStopped < NUM_SCAN_DURATIONS_KEPT ?
                                scansStopped : NUM_SCAN_DURATIONS_KEPT;
            sb.append("  Last " + lastScansSize +
                      " scans                       :\n");

            for (int i = 0; i < lastScans.size(); i++) {
            for (int i = 0; i < lastScansSize; i++) {
                LastScan scan = lastScans.get(i);
                Date timestamp = new Date(currentTime - elapsedRt + scan.timestamp);
                Date timestamp = new Date(System.currentTimeMillis() - SystemClock.elapsedRealtime()
                        + scan.timestamp);
                sb.append("    " + dateFormat.format(timestamp) + " - ");
                sb.append(scan.duration + "ms ");
                if (scan.opportunistic) sb.append("Opp ");
@@ -317,24 +298,6 @@ import com.android.bluetooth.btservice.BluetoothProto;
                if (scan.timeout) sb.append("Forced ");
                if (scan.filtered) sb.append("Filter ");
                sb.append(scan.results + " results");
                sb.append(" (" + scan.scannerId + ")");
                sb.append("\n");
            }
        }

        if (!ongoingScans.isEmpty()) {
            sb.append("  Ongoing scans                      :\n");
            for (Integer key : ongoingScans.keySet()) {
                LastScan scan = ongoingScans.get(key);
                Date timestamp = new Date(currentTime - elapsedRt + scan.timestamp);
                sb.append("    " + dateFormat.format(timestamp) + " - ");
                sb.append((elapsedRt - scan.timestamp) + "ms ");
                if (scan.opportunistic) sb.append("Opp ");
                if (scan.background) sb.append("Back ");
                if (scan.timeout) sb.append("Forced ");
                if (scan.filtered) sb.append("Filter ");
                sb.append(scan.results + " results");
                sb.append(" (" + scan.scannerId + ")");
                sb.append("\n");
            }
        }
@@ -346,6 +309,11 @@ import com.android.bluetooth.btservice.BluetoothProto;
            sb.append("  UUID                               : " +
                      appEntry.uuid + "\n");

            if (isScanning) {
                sb.append("  Current scan duration in ms        : " +
                          scanDuration + "\n");
            }

            List<ContextMap.Connection> connections =
              contextMap.getConnectionByApp(appEntry.id);

+4 −4
Original line number Diff line number Diff line
@@ -781,7 +781,7 @@ public class GattService extends ProfileService {
            }

            try {
                app.appScanStats.addResult(client.scannerId);
                app.appScanStats.addResult();
                if (app.callback != null) {
                    app.callback.onScanResult(result);
                } else {
@@ -1596,7 +1596,7 @@ public class GattService extends ProfileService {
            scanClient.stats = app;

            boolean isFilteredScan = (filters != null) && !filters.isEmpty();
            app.recordScanStart(settings, isFilteredScan, scannerId);
            app.recordScanStart(settings, isFilteredScan);
        }

        mScanManager.startScan(scanClient);
@@ -1644,7 +1644,7 @@ public class GattService extends ProfileService {
            scanClient.stats = app;

            boolean isFilteredScan = (piInfo.filters != null) && !piInfo.filters.isEmpty();
            app.recordScanStart(piInfo.settings, isFilteredScan, scannerId);
            app.recordScanStart(piInfo.settings, isFilteredScan);
        }

        mScanManager.startScan(scanClient);
@@ -1663,7 +1663,7 @@ public class GattService extends ProfileService {

        AppScanStats app = null;
        app = mScannerMap.getAppScanStatsById(client.scannerId);
        if (app != null) app.recordScanStop(client.scannerId);
        if (app != null) app.recordScanStop();

        mScanManager.stopScan(client);
    }
+1 −1
Original line number Diff line number Diff line
@@ -686,7 +686,7 @@ public class ScanManager {
                        "Moving scan client to opportunistic (scannerId " + client.scannerId + ")");
                setOpportunisticScanClient(client);
                removeScanFilters(client.scannerId);
                client.stats.setScanTimeout(client.scannerId);
                client.stats.setScanTimeout();
            }

            // The scan should continue for background scans