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

Commit 58e0eefe authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Improve power tracking of WIFI use.

We now distribute "wifi started" time across all apps that are
holding WIFI locks that cause it to be started.  But only when
WIFI would not normally be running.  Also have a mechanism to
distribute other WIFI work that has happened across those processes
based on their use.

Also fixed a bug where we were not retaining the CPU speed step
stats across boots...!

Change-Id: I00e3153b98429166273750512cc37e7975211ab9
parent 0741a5ed
Loading
Loading
Loading
Loading
+16 −16
Original line number Original line Diff line number Diff line
@@ -56,9 +56,9 @@ public abstract class BatteryStats implements Parcelable {
    public static final int SENSOR = 3;
    public static final int SENSOR = 3;
    
    
    /**
    /**
     * A constant indicating a a wifi turn on timer
     * A constant indicating a a wifi running timer
     */
     */
    public static final int WIFI_TURNED_ON = 4;
    public static final int WIFI_RUNNING = 4;
    
    
    /**
    /**
     * A constant indicating a full wifi lock timer
     * A constant indicating a full wifi lock timer
@@ -249,8 +249,8 @@ public abstract class BatteryStats implements Parcelable {
         */
         */
        public abstract long getTcpBytesSent(int which);
        public abstract long getTcpBytesSent(int which);
        
        
        public abstract void noteWifiTurnedOnLocked();
        public abstract void noteWifiRunningLocked();
        public abstract void noteWifiTurnedOffLocked();
        public abstract void noteWifiStoppedLocked();
        public abstract void noteFullWifiLockAcquiredLocked();
        public abstract void noteFullWifiLockAcquiredLocked();
        public abstract void noteFullWifiLockReleasedLocked();
        public abstract void noteFullWifiLockReleasedLocked();
        public abstract void noteScanWifiLockAcquiredLocked();
        public abstract void noteScanWifiLockAcquiredLocked();
@@ -261,7 +261,7 @@ public abstract class BatteryStats implements Parcelable {
        public abstract void noteAudioTurnedOffLocked();
        public abstract void noteAudioTurnedOffLocked();
        public abstract void noteVideoTurnedOnLocked();
        public abstract void noteVideoTurnedOnLocked();
        public abstract void noteVideoTurnedOffLocked();
        public abstract void noteVideoTurnedOffLocked();
        public abstract long getWifiTurnedOnTime(long batteryRealtime, int which);
        public abstract long getWifiRunningTime(long batteryRealtime, int which);
        public abstract long getFullWifiLockTime(long batteryRealtime, int which);
        public abstract long getFullWifiLockTime(long batteryRealtime, int which);
        public abstract long getScanWifiLockTime(long batteryRealtime, int which);
        public abstract long getScanWifiLockTime(long batteryRealtime, int which);
        public abstract long getWifiMulticastTime(long batteryRealtime,
        public abstract long getWifiMulticastTime(long batteryRealtime,
@@ -701,7 +701,7 @@ public abstract class BatteryStats implements Parcelable {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getWifiRunningTime(long batteryRealtime, int which);
    public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);


    /**
    /**
     * Returns the time in microseconds that bluetooth has been on while the device was
     * Returns the time in microseconds that bluetooth has been on while the device was
@@ -977,7 +977,7 @@ public abstract class BatteryStats implements Parcelable {
        final long screenOnTime = getScreenOnTime(batteryRealtime, which);
        final long screenOnTime = getScreenOnTime(batteryRealtime, which);
        final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
        final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
        final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
        final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
        final long wifiRunningTime = getWifiRunningTime(batteryRealtime, which);
        final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
        final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
        final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
       
       
        StringBuilder sb = new StringBuilder(128);
        StringBuilder sb = new StringBuilder(128);
@@ -1091,14 +1091,14 @@ public abstract class BatteryStats implements Parcelable {
            long tx = u.getTcpBytesSent(which);
            long tx = u.getTcpBytesSent(which);
            long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
            long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
            long scanWifiLockOnTime = u.getScanWifiLockTime(batteryRealtime, which);
            long scanWifiLockOnTime = u.getScanWifiLockTime(batteryRealtime, which);
            long wifiTurnedOnTime = u.getWifiTurnedOnTime(batteryRealtime, which);
            long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
            
            
            if (rx > 0 || tx > 0) dumpLine(pw, uid, category, NETWORK_DATA, rx, tx);
            if (rx > 0 || tx > 0) dumpLine(pw, uid, category, NETWORK_DATA, rx, tx);
            
            
            if (fullWifiLockOnTime != 0 || scanWifiLockOnTime != 0
            if (fullWifiLockOnTime != 0 || scanWifiLockOnTime != 0
                    || wifiTurnedOnTime != 0) {
                    || uidWifiRunningTime != 0) {
                dumpLine(pw, uid, category, WIFI_LOCK_DATA, 
                dumpLine(pw, uid, category, WIFI_LOCK_DATA, 
                        fullWifiLockOnTime, scanWifiLockOnTime, wifiTurnedOnTime);
                        fullWifiLockOnTime, scanWifiLockOnTime, uidWifiRunningTime);
            }
            }


            if (u.hasUserActivity()) {
            if (u.hasUserActivity()) {
@@ -1240,7 +1240,7 @@ public abstract class BatteryStats implements Parcelable {
        
        
        final long screenOnTime = getScreenOnTime(batteryRealtime, which);
        final long screenOnTime = getScreenOnTime(batteryRealtime, which);
        final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
        final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
        final long wifiRunningTime = getWifiRunningTime(batteryRealtime, which);
        final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
        final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
        final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
        final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
        final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
        sb.setLength(0);
        sb.setLength(0);
@@ -1449,7 +1449,7 @@ public abstract class BatteryStats implements Parcelable {
            long tcpSent = u.getTcpBytesSent(which);
            long tcpSent = u.getTcpBytesSent(which);
            long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
            long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
            long scanWifiLockOnTime = u.getScanWifiLockTime(batteryRealtime, which);
            long scanWifiLockOnTime = u.getScanWifiLockTime(batteryRealtime, which);
            long wifiTurnedOnTime = u.getWifiTurnedOnTime(batteryRealtime, which);
            long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
            
            
            if (tcpReceived != 0 || tcpSent != 0) {
            if (tcpReceived != 0 || tcpSent != 0) {
                pw.print(prefix); pw.print("    Network: ");
                pw.print(prefix); pw.print("    Network: ");
@@ -1480,11 +1480,11 @@ public abstract class BatteryStats implements Parcelable {
            }
            }
            
            
            if (fullWifiLockOnTime != 0 || scanWifiLockOnTime != 0
            if (fullWifiLockOnTime != 0 || scanWifiLockOnTime != 0
                    || wifiTurnedOnTime != 0) {
                    || uidWifiRunningTime != 0) {
                sb.setLength(0);
                sb.setLength(0);
                sb.append(prefix); sb.append("    Turned Wifi On: "); 
                sb.append(prefix); sb.append("    Wifi Running: ");
                        formatTimeMs(sb, wifiTurnedOnTime / 1000); 
                        formatTimeMs(sb, uidWifiRunningTime / 1000);
                        sb.append("("); sb.append(formatRatioLocked(wifiTurnedOnTime, 
                        sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
                                whichBatteryRealtime)); sb.append(")\n");
                                whichBatteryRealtime)); sb.append(")\n");
                sb.append(prefix); sb.append("    Full Wifi Lock: "); 
                sb.append(prefix); sb.append("    Full Wifi Lock: "); 
                        formatTimeMs(sb, fullWifiLockOnTime / 1000); 
                        formatTimeMs(sb, fullWifiLockOnTime / 1000); 
+5 −4
Original line number Original line Diff line number Diff line
@@ -49,10 +49,11 @@ interface IBatteryStats {
    void notePhoneSignalStrength(in SignalStrength signalStrength);
    void notePhoneSignalStrength(in SignalStrength signalStrength);
    void notePhoneDataConnectionState(int dataType, boolean hasData);
    void notePhoneDataConnectionState(int dataType, boolean hasData);
    void notePhoneState(int phoneState);
    void notePhoneState(int phoneState);
    void noteWifiOn(int uid);
    void noteWifiOn();
    void noteWifiOff(int uid);
    void noteWifiOff();
    void noteWifiRunning();
    void noteWifiRunning(in WorkSource ws);
    void noteWifiStopped();
    void noteWifiRunningChanged(in WorkSource oldWs, in WorkSource newWs);
    void noteWifiStopped(in WorkSource ws);
    void noteBluetoothOn();
    void noteBluetoothOn();
    void noteBluetoothOff();
    void noteBluetoothOff();
    void noteFullWifiLockAcquired(int uid);
    void noteFullWifiLockAcquired(int uid);
+171 −74

File changed.

Preview size limit exceeded, changes collapsed.

+37 −5
Original line number Original line Diff line number Diff line
@@ -189,6 +189,12 @@ public class WifiService extends IWifiManager.Stub {
    private static final int SCAN_RESULT_BUFFER_SIZE = 512;
    private static final int SCAN_RESULT_BUFFER_SIZE = 512;
    private boolean mNeedReconfig;
    private boolean mNeedReconfig;


    /**
     * Temporary for computing UIDS that are responsible for starting WIFI.
     * Protected by mWifiStateTracker lock.
     */
    private final WorkSource mTmpWorkSource = new WorkSource();

    /*
    /*
     * Last UID that asked to enable WIFI.
     * Last UID that asked to enable WIFI.
     */
     */
@@ -529,9 +535,9 @@ public class WifiService extends IWifiManager.Stub {
        long ident = Binder.clearCallingIdentity();
        long ident = Binder.clearCallingIdentity();
        try {
        try {
            if (wifiState == WIFI_STATE_ENABLED) {
            if (wifiState == WIFI_STATE_ENABLED) {
                mBatteryStats.noteWifiOn(uid);
                mBatteryStats.noteWifiOn();
            } else if (wifiState == WIFI_STATE_DISABLED) {
            } else if (wifiState == WIFI_STATE_DISABLED) {
                mBatteryStats.noteWifiOff(uid);
                mBatteryStats.noteWifiOff();
            }
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
        } finally {
        } finally {
@@ -788,9 +794,9 @@ public class WifiService extends IWifiManager.Stub {
        long ident = Binder.clearCallingIdentity();
        long ident = Binder.clearCallingIdentity();
        try {
        try {
            if (wifiAPState == WIFI_AP_STATE_ENABLED) {
            if (wifiAPState == WIFI_AP_STATE_ENABLED) {
                mBatteryStats.noteWifiOn(uid);
                mBatteryStats.noteWifiOn();
            } else if (wifiAPState == WIFI_AP_STATE_DISABLED) {
            } else if (wifiAPState == WIFI_AP_STATE_DISABLED) {
                mBatteryStats.noteWifiOff(uid);
                mBatteryStats.noteWifiOff();
            }
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
        } finally {
        } finally {
@@ -1664,6 +1670,9 @@ public class WifiService extends IWifiManager.Stub {
                mAlarmManager.cancel(mIdleIntent);
                mAlarmManager.cancel(mIdleIntent);
                mDeviceIdle = false;
                mDeviceIdle = false;
                mScreenOff = false;
                mScreenOff = false;
                // Once the screen is on, we are not keeping WIFI running
                // because of any locks so clear that tracking immediately.
                reportStartWorkSource();
                mWifiStateTracker.enableRssiPolling(true);
                mWifiStateTracker.enableRssiPolling(true);
                /* DHCP or other temporary failures in the past can prevent
                /* DHCP or other temporary failures in the past can prevent
                 * a disabled network from being connected to, enable on screen on
                 * a disabled network from being connected to, enable on screen on
@@ -1705,6 +1714,7 @@ public class WifiService extends IWifiManager.Stub {
            } else if (action.equals(ACTION_DEVICE_IDLE)) {
            } else if (action.equals(ACTION_DEVICE_IDLE)) {
                Slog.d(TAG, "got ACTION_DEVICE_IDLE");
                Slog.d(TAG, "got ACTION_DEVICE_IDLE");
                mDeviceIdle = true;
                mDeviceIdle = true;
                reportStartWorkSource();
            } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
            } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                /*
                /*
                 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
                 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
@@ -1806,6 +1816,18 @@ public class WifiService extends IWifiManager.Stub {
        Message.obtain(mWifiHandler, MESSAGE_ENABLE_NETWORKS).sendToTarget();
        Message.obtain(mWifiHandler, MESSAGE_ENABLE_NETWORKS).sendToTarget();
    }
    }


    private void reportStartWorkSource() {
        synchronized (mWifiStateTracker) {
            mTmpWorkSource.clear();
            if (mDeviceIdle) {
                for (int i=0; i<mLocks.mList.size(); i++) {
                    mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
                }
            }
            mWifiStateTracker.updateBatteryWorkSourceLocked(mTmpWorkSource);
        }
    }

    private void updateWifiState() {
    private void updateWifiState() {
        // send a message so it's all serialized
        // send a message so it's all serialized
        Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget();
        Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget();
@@ -1814,7 +1836,12 @@ public class WifiService extends IWifiManager.Stub {
    private void doUpdateWifiState() {
    private void doUpdateWifiState() {
        boolean wifiEnabled = getPersistedWifiEnabled();
        boolean wifiEnabled = getPersistedWifiEnabled();
        boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden;
        boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden;
        boolean lockHeld = mLocks.hasLocks();

        boolean lockHeld;
        synchronized (mLocks) {
            lockHeld = mLocks.hasLocks();
        }

        int strongestLockMode = WifiManager.WIFI_MODE_FULL;
        int strongestLockMode = WifiManager.WIFI_MODE_FULL;
        boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
        boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
        boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
        boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
@@ -1922,6 +1949,7 @@ public class WifiService extends IWifiManager.Stub {
                    break;
                    break;


                case MESSAGE_START_WIFI:
                case MESSAGE_START_WIFI:
                    reportStartWorkSource();
                    mWifiStateTracker.setScanOnlyMode(msg.arg1 == WifiManager.WIFI_MODE_SCAN_ONLY);
                    mWifiStateTracker.setScanOnlyMode(msg.arg1 == WifiManager.WIFI_MODE_SCAN_ONLY);
                    mWifiStateTracker.restart();
                    mWifiStateTracker.restart();
                    mWifiStateTracker.setHighPerfMode(msg.arg1 ==
                    mWifiStateTracker.setHighPerfMode(msg.arg1 ==
@@ -2198,6 +2226,10 @@ public class WifiService extends IWifiManager.Stub {
            Binder.restoreCallingIdentity(ident);
            Binder.restoreCallingIdentity(ident);
        }
        }


        // Be aggressive about adding new locks into the accounted state...
        // we want to over-report rather than under-report.
        reportStartWorkSource();

        updateWifiState();
        updateWifiState();
        return true;
        return true;
    }
    }
+15 −8
Original line number Original line Diff line number Diff line
@@ -218,17 +218,17 @@ public final class BatteryStatsService extends IBatteryStats.Stub {
        }
        }
    }
    }


    public void noteWifiOn(int uid) {
    public void noteWifiOn() {
        enforceCallingPermission();
        enforceCallingPermission();
        synchronized (mStats) {
        synchronized (mStats) {
            mStats.noteWifiOnLocked(uid);
            mStats.noteWifiOnLocked();
        }
        }
    }
    }
    
    
    public void noteWifiOff(int uid) {
    public void noteWifiOff() {
        enforceCallingPermission();
        enforceCallingPermission();
        synchronized (mStats) {
        synchronized (mStats) {
            mStats.noteWifiOffLocked(uid);
            mStats.noteWifiOffLocked();
        }
        }
    }
    }


@@ -260,17 +260,24 @@ public final class BatteryStatsService extends IBatteryStats.Stub {
        }
        }
    }
    }


    public void noteWifiRunning() {
    public void noteWifiRunning(WorkSource ws) {
        enforceCallingPermission();
        enforceCallingPermission();
        synchronized (mStats) {
        synchronized (mStats) {
            mStats.noteWifiRunningLocked();
            mStats.noteWifiRunningLocked(ws);
        }
        }
    }
    }


    public void noteWifiStopped() {
    public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
        enforceCallingPermission();
        enforceCallingPermission();
        synchronized (mStats) {
        synchronized (mStats) {
            mStats.noteWifiStoppedLocked();
            mStats.noteWifiRunningChangedLocked(oldWs, newWs);
        }
    }

    public void noteWifiStopped(WorkSource ws) {
        enforceCallingPermission();
        synchronized (mStats) {
            mStats.noteWifiStoppedLocked(ws);
        }
        }
    }
    }


Loading