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

Commit 5be91008 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Add power stats collector and processor for Camera and GNSS

Bug: 333941740
Test: atest PowerStatsTestsRavenwood; atest PowerStatsTests
Flag: com.android.server.power.optimization.streamlined_misc_battery_stats
Change-Id: I37dfece1cbbe6d663be5d232cb684858542d8035
parent 717f00be
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -200,6 +200,8 @@ public abstract class BatteryConsumer {
            POWER_COMPONENT_AUDIO,
            POWER_COMPONENT_AUDIO,
            POWER_COMPONENT_VIDEO,
            POWER_COMPONENT_VIDEO,
            POWER_COMPONENT_FLASHLIGHT,
            POWER_COMPONENT_FLASHLIGHT,
            POWER_COMPONENT_CAMERA,
            POWER_COMPONENT_GNSS,
    };
    };


    static final int COLUMN_INDEX_BATTERY_CONSUMER_TYPE = 0;
    static final int COLUMN_INDEX_BATTERY_CONSUMER_TYPE = 0;
+2 −1
Original line number Original line Diff line number Diff line
@@ -1994,7 +1994,8 @@ public abstract class BatteryStats {


        // STATES2 bits that are used for Power Stats tracking
        // STATES2 bits that are used for Power Stats tracking
        public static final int IMPORTANT_FOR_POWER_STATS_STATES2 =
        public static final int IMPORTANT_FOR_POWER_STATS_STATES2 =
                STATE2_VIDEO_ON_FLAG | STATE2_FLASHLIGHT_FLAG | STATE2_CAMERA_FLAG;
                STATE2_VIDEO_ON_FLAG | STATE2_FLASHLIGHT_FLAG | STATE2_CAMERA_FLAG
                | STATE2_GPS_SIGNAL_QUALITY_MASK;


        @UnsupportedAppUsage
        @UnsupportedAppUsage
        public int states2;
        public int states2;
+33 −7
Original line number Original line Diff line number Diff line
@@ -128,8 +128,10 @@ import com.android.server.power.stats.BatteryStatsDumpHelperImpl;
import com.android.server.power.stats.BatteryStatsImpl;
import com.android.server.power.stats.BatteryStatsImpl;
import com.android.server.power.stats.BatteryUsageStatsProvider;
import com.android.server.power.stats.BatteryUsageStatsProvider;
import com.android.server.power.stats.BluetoothPowerStatsProcessor;
import com.android.server.power.stats.BluetoothPowerStatsProcessor;
import com.android.server.power.stats.CameraPowerStatsProcessor;
import com.android.server.power.stats.CpuPowerStatsProcessor;
import com.android.server.power.stats.CpuPowerStatsProcessor;
import com.android.server.power.stats.FlashlightPowerStatsProcessor;
import com.android.server.power.stats.FlashlightPowerStatsProcessor;
import com.android.server.power.stats.GnssPowerStatsProcessor;
import com.android.server.power.stats.MobileRadioPowerStatsProcessor;
import com.android.server.power.stats.MobileRadioPowerStatsProcessor;
import com.android.server.power.stats.PhoneCallPowerStatsProcessor;
import com.android.server.power.stats.PhoneCallPowerStatsProcessor;
import com.android.server.power.stats.PowerStatsAggregator;
import com.android.server.power.stats.PowerStatsAggregator;
@@ -528,8 +530,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                .setProcessor(
                .setProcessor(
                        new AudioPowerStatsProcessor(mPowerProfile,
                        new AudioPowerStatsProcessor(mPowerProfile, mPowerStatsUidResolver));
                                mPowerStatsUidResolver));


        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_VIDEO)
        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_VIDEO)
                .trackDeviceStates(
                .trackDeviceStates(
@@ -539,9 +540,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                .setProcessor(
                .setProcessor(new VideoPowerStatsProcessor(mPowerProfile, mPowerStatsUidResolver));
                        new VideoPowerStatsProcessor(mPowerProfile,
                                mPowerStatsUidResolver));


        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT)
        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT)
                .trackDeviceStates(
                .trackDeviceStates(
@@ -552,8 +551,29 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                .setProcessor(
                .setProcessor(
                        new FlashlightPowerStatsProcessor(mPowerProfile,
                        new FlashlightPowerStatsProcessor(mPowerProfile, mPowerStatsUidResolver));
                                mPowerStatsUidResolver));

        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_CAMERA)
                .trackDeviceStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_SCREEN)
                .trackUidStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                .setProcessor(
                        new CameraPowerStatsProcessor(mPowerProfile, mPowerStatsUidResolver));

        config.trackPowerComponent(BatteryConsumer.POWER_COMPONENT_GNSS)
                .trackDeviceStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_SCREEN)
                .trackUidStates(
                        AggregatedPowerStatsConfig.STATE_POWER,
                        AggregatedPowerStatsConfig.STATE_SCREEN,
                        AggregatedPowerStatsConfig.STATE_PROCESS_STATE)
                .setProcessor(
                        new GnssPowerStatsProcessor(mPowerProfile, mPowerStatsUidResolver));
        return config;
        return config;
    }
    }


@@ -639,6 +659,12 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                BatteryConsumer.POWER_COMPONENT_FLASHLIGHT,
                BatteryConsumer.POWER_COMPONENT_FLASHLIGHT,
                Flags.streamlinedMiscBatteryStats());
                Flags.streamlinedMiscBatteryStats());


        mStats.setPowerStatsCollectorEnabled(BatteryConsumer.POWER_COMPONENT_CAMERA,
                Flags.streamlinedMiscBatteryStats());
        mBatteryUsageStatsProvider.setPowerStatsExporterEnabled(
                BatteryConsumer.POWER_COMPONENT_CAMERA,
                Flags.streamlinedMiscBatteryStats());

        mWorker.systemServicesReady();
        mWorker.systemServicesReady();
        mStats.systemServicesReady(mContext);
        mStats.systemServicesReady(mContext);
        mCpuWakeupStats.systemServicesReady();
        mCpuWakeupStats.systemServicesReady();
+45 −7
Original line number Original line Diff line number Diff line
@@ -298,6 +298,8 @@ public class BatteryStatsImpl extends BatteryStats {
    private final MobileRadioPowerStatsCollector mMobileRadioPowerStatsCollector;
    private final MobileRadioPowerStatsCollector mMobileRadioPowerStatsCollector;
    private final WifiPowerStatsCollector mWifiPowerStatsCollector;
    private final WifiPowerStatsCollector mWifiPowerStatsCollector;
    private final BluetoothPowerStatsCollector mBluetoothPowerStatsCollector;
    private final BluetoothPowerStatsCollector mBluetoothPowerStatsCollector;
    private final CameraPowerStatsCollector mCameraPowerStatsCollector;
    private final GnssPowerStatsCollector mGnssPowerStatsCollector;
    private final SparseBooleanArray mPowerStatsCollectorEnabled = new SparseBooleanArray();
    private final SparseBooleanArray mPowerStatsCollectorEnabled = new SparseBooleanArray();
    private final WifiPowerStatsCollector.WifiStatsRetriever mWifiStatsRetriever =
    private final WifiPowerStatsCollector.WifiStatsRetriever mWifiStatsRetriever =
            new WifiPowerStatsCollector.WifiStatsRetriever() {
            new WifiPowerStatsCollector.WifiStatsRetriever() {
@@ -1963,7 +1965,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private class PowerStatsCollectorInjector implements CpuPowerStatsCollector.Injector,
    private class PowerStatsCollectorInjector implements CpuPowerStatsCollector.Injector,
            MobileRadioPowerStatsCollector.Injector, WifiPowerStatsCollector.Injector,
            MobileRadioPowerStatsCollector.Injector, WifiPowerStatsCollector.Injector,
            BluetoothPowerStatsCollector.Injector {
            BluetoothPowerStatsCollector.Injector, EnergyConsumerPowerStatsCollector.Injector {
        private PackageManager mPackageManager;
        private PackageManager mPackageManager;
        private PowerStatsCollector.ConsumedEnergyRetriever mConsumedEnergyRetriever;
        private PowerStatsCollector.ConsumedEnergyRetriever mConsumedEnergyRetriever;
        private NetworkStatsManager mNetworkStatsManager;
        private NetworkStatsManager mNetworkStatsManager;
@@ -5446,7 +5448,10 @@ public class BatteryStatsImpl extends BatteryStats {
        final int mappedUid = mapUid(uid);
        final int mappedUid = mapUid(uid);
        if (mGpsNesting == 0) {
        if (mGpsNesting == 0) {
            mHistory.recordStateStartEvent(elapsedRealtimeMs, uptimeMs,
            mHistory.recordStateStartEvent(elapsedRealtimeMs, uptimeMs,
                    HistoryItem.STATE_GPS_ON_FLAG);
                    HistoryItem.STATE_GPS_ON_FLAG, uid, "gnss");
            if (mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_GNSS)) {
                mGnssPowerStatsCollector.schedule();
            }
        }
        }
        mGpsNesting++;
        mGpsNesting++;
@@ -5465,11 +5470,14 @@ public class BatteryStatsImpl extends BatteryStats {
        mGpsNesting--;
        mGpsNesting--;
        if (mGpsNesting == 0) {
        if (mGpsNesting == 0) {
            mHistory.recordStateStopEvent(elapsedRealtimeMs, uptimeMs,
            mHistory.recordStateStopEvent(elapsedRealtimeMs, uptimeMs,
                    HistoryItem.STATE_GPS_ON_FLAG);
                    HistoryItem.STATE_GPS_ON_FLAG, uid, "gnss");
            mHistory.recordGpsSignalQualityEvent(elapsedRealtimeMs, uptimeMs,
            mHistory.recordGpsSignalQualityEvent(elapsedRealtimeMs, uptimeMs,
                    GPS_SIGNAL_QUALITY_NONE);
                    GPS_SIGNAL_QUALITY_NONE);
            stopAllGpsSignalQualityTimersLocked(-1, elapsedRealtimeMs);
            stopAllGpsSignalQualityTimersLocked(-1, elapsedRealtimeMs);
            mGpsSignalQualityBin = -1;
            mGpsSignalQualityBin = -1;
            if (mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_GNSS)) {
                mGnssPowerStatsCollector.schedule();
            }
        }
        }
        mFrameworkStatsLogger.gpsScanStateChanged(mapIsolatedUid(uid), workChain, /* on */ false);
        mFrameworkStatsLogger.gpsScanStateChanged(mapIsolatedUid(uid), workChain, /* on */ false);
@@ -6652,14 +6660,18 @@ public class BatteryStatsImpl extends BatteryStats {
        uid = mapUid(uid);
        uid = mapUid(uid);
        if (mCameraOnNesting++ == 0) {
        if (mCameraOnNesting++ == 0) {
            mHistory.recordState2StartEvent(elapsedRealtimeMs, uptimeMs,
            mHistory.recordState2StartEvent(elapsedRealtimeMs, uptimeMs,
                    HistoryItem.STATE2_CAMERA_FLAG);
                    HistoryItem.STATE2_CAMERA_FLAG, uid, "camera");
            mCameraOnTimer.startRunningLocked(elapsedRealtimeMs);
            mCameraOnTimer.startRunningLocked(elapsedRealtimeMs);
        }
        }
        getUidStatsLocked(uid, elapsedRealtimeMs, uptimeMs)
        getUidStatsLocked(uid, elapsedRealtimeMs, uptimeMs)
                .noteCameraTurnedOnLocked(elapsedRealtimeMs);
                .noteCameraTurnedOnLocked(elapsedRealtimeMs);
        if (mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_CAMERA)) {
            mCameraPowerStatsCollector.schedule();
        } else {
            scheduleSyncExternalStatsLocked("camera-on", ExternalStatsSync.UPDATE_CAMERA);
            scheduleSyncExternalStatsLocked("camera-on", ExternalStatsSync.UPDATE_CAMERA);
        }
        }
    }
    @GuardedBy("this")
    @GuardedBy("this")
    public void noteCameraOffLocked(int uid, long elapsedRealtimeMs, long uptimeMs) {
    public void noteCameraOffLocked(int uid, long elapsedRealtimeMs, long uptimeMs) {
@@ -6669,14 +6681,18 @@ public class BatteryStatsImpl extends BatteryStats {
        uid = mapUid(uid);
        uid = mapUid(uid);
        if (--mCameraOnNesting == 0) {
        if (--mCameraOnNesting == 0) {
            mHistory.recordState2StopEvent(elapsedRealtimeMs, uptimeMs,
            mHistory.recordState2StopEvent(elapsedRealtimeMs, uptimeMs,
                    HistoryItem.STATE2_CAMERA_FLAG);
                    HistoryItem.STATE2_CAMERA_FLAG, uid, "camera");
            mCameraOnTimer.stopRunningLocked(elapsedRealtimeMs);
            mCameraOnTimer.stopRunningLocked(elapsedRealtimeMs);
        }
        }
        getUidStatsLocked(uid, elapsedRealtimeMs, uptimeMs)
        getUidStatsLocked(uid, elapsedRealtimeMs, uptimeMs)
                .noteCameraTurnedOffLocked(elapsedRealtimeMs);
                .noteCameraTurnedOffLocked(elapsedRealtimeMs);
        if (mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_CAMERA)) {
            mCameraPowerStatsCollector.schedule();
        } else {
            scheduleSyncExternalStatsLocked("camera-off", ExternalStatsSync.UPDATE_CAMERA);
            scheduleSyncExternalStatsLocked("camera-off", ExternalStatsSync.UPDATE_CAMERA);
        }
        }
    }
    @GuardedBy("this")
    @GuardedBy("this")
    public void noteResetCameraLocked(long elapsedRealtimeMs, long uptimeMs) {
    public void noteResetCameraLocked(long elapsedRealtimeMs, long uptimeMs) {
@@ -11281,6 +11297,12 @@ public class BatteryStatsImpl extends BatteryStats {
                mPowerStatsCollectorInjector);
                mPowerStatsCollectorInjector);
        mBluetoothPowerStatsCollector.addConsumer(this::recordPowerStats);
        mBluetoothPowerStatsCollector.addConsumer(this::recordPowerStats);
        mCameraPowerStatsCollector = new CameraPowerStatsCollector(mPowerStatsCollectorInjector);
        mCameraPowerStatsCollector.addConsumer(this::recordPowerStats);
        mGnssPowerStatsCollector = new GnssPowerStatsCollector(mPowerStatsCollectorInjector);
        mGnssPowerStatsCollector.addConsumer(this::recordPowerStats);
        mStartCount++;
        mStartCount++;
        initTimersAndCounters();
        initTimersAndCounters();
        mOnBattery = mOnBatteryInternal = false;
        mOnBattery = mOnBatteryInternal = false;
@@ -14703,6 +14725,14 @@ public class BatteryStatsImpl extends BatteryStats {
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_BLUETOOTH));
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_BLUETOOTH));
        mBluetoothPowerStatsCollector.schedule();
        mBluetoothPowerStatsCollector.schedule();
        mCameraPowerStatsCollector.setEnabled(
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_CAMERA));
        mCameraPowerStatsCollector.schedule();
        mGnssPowerStatsCollector.setEnabled(
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_GNSS));
        mGnssPowerStatsCollector.schedule();
        mSystemReady = true;
        mSystemReady = true;
    }
    }
@@ -14721,6 +14751,10 @@ public class BatteryStatsImpl extends BatteryStats {
                return mWifiPowerStatsCollector;
                return mWifiPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_BLUETOOTH:
            case BatteryConsumer.POWER_COMPONENT_BLUETOOTH:
                return mBluetoothPowerStatsCollector;
                return mBluetoothPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_CAMERA:
                return mCameraPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_GNSS:
                return mGnssPowerStatsCollector;
        }
        }
        return null;
        return null;
    }
    }
@@ -16258,6 +16292,8 @@ public class BatteryStatsImpl extends BatteryStats {
        mMobileRadioPowerStatsCollector.forceSchedule();
        mMobileRadioPowerStatsCollector.forceSchedule();
        mWifiPowerStatsCollector.forceSchedule();
        mWifiPowerStatsCollector.forceSchedule();
        mBluetoothPowerStatsCollector.forceSchedule();
        mBluetoothPowerStatsCollector.forceSchedule();
        mCameraPowerStatsCollector.forceSchedule();
        mGnssPowerStatsCollector.forceSchedule();
    }
    }
    /**
    /**
@@ -16278,6 +16314,8 @@ public class BatteryStatsImpl extends BatteryStats {
        mMobileRadioPowerStatsCollector.collectAndDump(pw);
        mMobileRadioPowerStatsCollector.collectAndDump(pw);
        mWifiPowerStatsCollector.collectAndDump(pw);
        mWifiPowerStatsCollector.collectAndDump(pw);
        mBluetoothPowerStatsCollector.collectAndDump(pw);
        mBluetoothPowerStatsCollector.collectAndDump(pw);
        mCameraPowerStatsCollector.collectAndDump(pw);
        mGnssPowerStatsCollector.collectAndDump(pw);
    }
    }
    private final Runnable mWriteAsyncRunnable = () -> {
    private final Runnable mWriteAsyncRunnable = () -> {
+6 −2
Original line number Original line Diff line number Diff line
@@ -95,8 +95,12 @@ public class BatteryUsageStatsProvider {
                }
                }
                mPowerCalculators.add(new SensorPowerCalculator(
                mPowerCalculators.add(new SensorPowerCalculator(
                        mContext.getSystemService(SensorManager.class)));
                        mContext.getSystemService(SensorManager.class)));
                if (!mPowerStatsExporterEnabled.get(BatteryConsumer.POWER_COMPONENT_GNSS)) {
                    mPowerCalculators.add(new GnssPowerCalculator(mPowerProfile));
                    mPowerCalculators.add(new GnssPowerCalculator(mPowerProfile));
                }
                if (!mPowerStatsExporterEnabled.get(BatteryConsumer.POWER_COMPONENT_CAMERA)) {
                    mPowerCalculators.add(new CameraPowerCalculator(mPowerProfile));
                    mPowerCalculators.add(new CameraPowerCalculator(mPowerProfile));
                }
                if (!mPowerStatsExporterEnabled.get(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT)) {
                if (!mPowerStatsExporterEnabled.get(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT)) {
                    mPowerCalculators.add(new FlashlightPowerCalculator(mPowerProfile));
                    mPowerCalculators.add(new FlashlightPowerCalculator(mPowerProfile));
                }
                }
Loading