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

Commit c5750b5e authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Add PowerStats collector and processor for Wakelocks

Bug: 345022340
Test: atest PowerStatsTests; atest PowerStatsTestsRavenwood
Flag: com.android.server.power.optimization.streamlined_misc_battery_stats

Change-Id: I1532ee40adfdbb7935b0a23c4c3733b99d905ee1
parent 6581acd7
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -225,7 +225,9 @@ public abstract class BatteryConsumer {
                POWER_COMPONENT_FLASHLIGHT,
                POWER_COMPONENT_CAMERA,
                POWER_COMPONENT_GNSS,
                POWER_COMPONENT_SENSORS};
                POWER_COMPONENT_SENSORS,
                POWER_COMPONENT_WAKELOCK,
        };
        Arrays.sort(supportedPowerComponents);
        SUPPORTED_POWER_COMPONENTS_PER_PROCESS_STATE = IntArray.wrap(supportedPowerComponents);
    };
+2 −1
Original line number Diff line number Diff line
@@ -1950,7 +1950,8 @@ public abstract class BatteryStats {

        // STATES bits that are used for Power Stats tracking
        public static final int IMPORTANT_FOR_POWER_STATS_STATES =
                STATE_GPS_ON_FLAG | STATE_SENSOR_ON_FLAG | STATE_AUDIO_ON_FLAG;
                STATE_GPS_ON_FLAG | STATE_SENSOR_ON_FLAG | STATE_AUDIO_ON_FLAG
                        | STATE_WAKE_LOCK_FLAG;

        @UnsupportedAppUsage
        public int states;
+18 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import com.android.internal.annotations.VisibleForTesting;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ConcurrentModificationException;
@@ -1745,9 +1746,12 @@ public class BatteryStatsHistory {
                    mHistoryAddTmp.setTo(mHistoryLastWritten);
                    mHistoryAddTmp.wakelockTag = null;
                    mHistoryAddTmp.wakeReasonTag = null;
                    mHistoryAddTmp.powerStats = null;
                    mHistoryAddTmp.processStateChange = null;
                    mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
                    mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
                    writeHistoryItem(wakeElapsedTimeMs, uptimeMs, mHistoryAddTmp);

                }
            }
            mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
@@ -2452,6 +2456,20 @@ public class BatteryStatsHistory {
        }
    }

    /**
     * Prints battery stats history for debugging.
     */
    public void dump(PrintWriter pw, long startTimeMs, long endTimeMs) {
        BatteryStats.HistoryPrinter printer = new BatteryStats.HistoryPrinter();
        try (BatteryStatsHistoryIterator iterate = iterate(startTimeMs, endTimeMs)) {
            while (iterate.hasNext()) {
                HistoryItem next = iterate.next();
                printer.printNextItem(pw, next, 0, false, true);
            }
        }
        pw.flush();
    }

    /**
     * Writes/reads an array of longs into Parcel using a compact format, where small integers use
     * fewer bytes.  It is a bit more expensive than just writing the long into the parcel,
+6 −0
Original line number Diff line number Diff line
@@ -514,6 +514,12 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                BatteryConsumer.POWER_COMPONENT_CPU,
                Flags.streamlinedBatteryStats());

        mStats.setPowerStatsCollectorEnabled(BatteryConsumer.POWER_COMPONENT_WAKELOCK,
                Flags.streamlinedMiscBatteryStats());
        attributor.setPowerComponentSupported(
                BatteryConsumer.POWER_COMPONENT_WAKELOCK,
                Flags.streamlinedMiscBatteryStats());

        mStats.setPowerStatsCollectorEnabled(BatteryConsumer.POWER_COMPONENT_SCREEN,
                Flags.streamlinedMiscBatteryStats());
        attributor.setPowerComponentSupported(
+62 −1
Original line number Diff line number Diff line
@@ -294,6 +294,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
    private int[] mCpuPowerBracketMap;
    private final CpuPowerStatsCollector mCpuPowerStatsCollector;
    private final WakelockPowerStatsCollector mWakelockPowerStatsCollector;
    private final ScreenPowerStatsCollector mScreenPowerStatsCollector;
    private final MobileRadioPowerStatsCollector mMobileRadioPowerStatsCollector;
    private final WifiPowerStatsCollector mWifiPowerStatsCollector;
@@ -400,6 +401,48 @@ public class BatteryStatsImpl extends BatteryStats {
        }
    }
    private final WakelockPowerStatsCollector.WakelockDurationRetriever mWakelockDurationRetriever =
            new WakelockPowerStatsCollector.WakelockDurationRetriever() {
                @Override
                public long getWakelockDurationMillis() {
                    synchronized (BatteryStatsImpl.this) {
                        long rawRealtimeUs = mClock.uptimeMillis() * 1000;
                        long batteryUptimeUs = getBatteryUptime(rawRealtimeUs);
                        long screenOnTimeUs = getScreenOnTime(rawRealtimeUs,
                                BatteryStats.STATS_SINCE_CHARGED);
                        return (batteryUptimeUs - screenOnTimeUs) / 1000;
                    }
                }
                @Override
                public void retrieveUidWakelockDuration(Callback callback) {
                    synchronized (BatteryStatsImpl.this) {
                        long rawRealtimeUs = mClock.elapsedRealtime() * 1000;
                        for (int i = mUidStats.size() - 1; i >= 0; i--) {
                            Uid u = mUidStats.valueAt(i);
                            long wakeLockTimeUs = 0;
                            ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelockStats =
                                    u.getWakelockStats();
                            final int wakelockStatsCount = wakelockStats.size();
                            for (int j = 0; j < wakelockStatsCount; j++) {
                                final BatteryStats.Uid.Wakelock wakelock = wakelockStats.valueAt(j);
                                BatteryStats.Timer timer = wakelock.getWakeTime(
                                        BatteryStats.WAKE_TYPE_PARTIAL);
                                if (timer != null) {
                                    wakeLockTimeUs += timer.getTotalTimeLocked(rawRealtimeUs,
                                            BatteryStats.STATS_SINCE_CHARGED);
                                }
                            }
                            if (wakeLockTimeUs != 0) {
                                callback.onUidWakelockDuration(u.getUid(), wakeLockTimeUs / 1000);
                            }
                        }
                    }
                }
            };
    public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
        return mKernelMemoryStats;
    }
@@ -2015,7 +2058,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private class PowerStatsCollectorInjector implements CpuPowerStatsCollector.Injector,
            ScreenPowerStatsCollector.Injector, MobileRadioPowerStatsCollector.Injector,
            WifiPowerStatsCollector.Injector, BluetoothPowerStatsCollector.Injector,
            EnergyConsumerPowerStatsCollector.Injector {
            EnergyConsumerPowerStatsCollector.Injector, WakelockPowerStatsCollector.Injector {
        private PackageManager mPackageManager;
        private PowerStatsCollector.ConsumedEnergyRetriever mConsumedEnergyRetriever;
        private NetworkStatsManager mNetworkStatsManager;
@@ -2132,6 +2175,12 @@ public class BatteryStatsImpl extends BatteryStats {
            return () -> mPhoneSignalScanningTimer.getTotalTimeLocked(
                    mClock.elapsedRealtime() * 1000, STATS_SINCE_CHARGED) / 1000;
        }
        @Override
        public WakelockPowerStatsCollector.WakelockDurationRetriever
                getWakelockDurationRetriever() {
            return mWakelockDurationRetriever;
        }
    }
    private final PowerStatsCollectorInjector mPowerStatsCollectorInjector =
@@ -11349,6 +11398,10 @@ public class BatteryStatsImpl extends BatteryStats {
        mCpuPowerStatsCollector = new CpuPowerStatsCollector(mPowerStatsCollectorInjector);
        mCpuPowerStatsCollector.addConsumer(this::recordPowerStats);
        mWakelockPowerStatsCollector = new WakelockPowerStatsCollector(
                mPowerStatsCollectorInjector);
        mWakelockPowerStatsCollector.addConsumer(this::recordPowerStats);
        mScreenPowerStatsCollector = new ScreenPowerStatsCollector(mPowerStatsCollectorInjector);
        mScreenPowerStatsCollector.addConsumer(this::recordPowerStats);
@@ -14819,6 +14872,10 @@ public class BatteryStatsImpl extends BatteryStats {
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_CPU));
        mCpuPowerStatsCollector.schedule();
        mWakelockPowerStatsCollector.setEnabled(
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_WAKELOCK));
        mWakelockPowerStatsCollector.schedule();
        mScreenPowerStatsCollector.setEnabled(
                mPowerStatsCollectorEnabled.get(BatteryConsumer.POWER_COMPONENT_SCREEN));
        mScreenPowerStatsCollector.schedule();
@@ -14859,6 +14916,8 @@ public class BatteryStatsImpl extends BatteryStats {
        switch (powerComponent) {
            case BatteryConsumer.POWER_COMPONENT_CPU:
                return mCpuPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_WAKELOCK:
                return mWakelockPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_SCREEN:
                return mScreenPowerStatsCollector;
            case BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO:
@@ -16400,6 +16459,7 @@ public class BatteryStatsImpl extends BatteryStats {
        }
        mCpuPowerStatsCollector.forceSchedule();
        mWakelockPowerStatsCollector.forceSchedule();
        mScreenPowerStatsCollector.forceSchedule();
        mMobileRadioPowerStatsCollector.forceSchedule();
        mWifiPowerStatsCollector.forceSchedule();
@@ -16424,6 +16484,7 @@ public class BatteryStatsImpl extends BatteryStats {
     */
    public void dumpStatsSample(PrintWriter pw) {
        mCpuPowerStatsCollector.collectAndDump(pw);
        mWakelockPowerStatsCollector.collectAndDump(pw);
        mScreenPowerStatsCollector.collectAndDump(pw);
        mMobileRadioPowerStatsCollector.collectAndDump(pw);
        mWifiPowerStatsCollector.collectAndDump(pw);
Loading