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

Commit 31d1a07e authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Disable unnecessary operation in the streamlined BatteryStats mode

Bug: 285646152
Test: atest PowerStatsTests
Change-Id: I7012be1709f5ed8adf3d23386588c5f0e6ab9a6a
parent 16ed0294
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -480,6 +480,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
    }

    public void systemServicesReady() {
        mStats.setPowerStatsCollectorEnabled(Flags.streamlinedBatteryStats());
        mBatteryUsageStatsProvider.setPowerStatsExporterEnabled(Flags.streamlinedBatteryStats());
        mWorker.systemServicesReady();
        mStats.systemServicesReady(mContext);
@@ -2648,7 +2649,9 @@ public final class BatteryStatsService extends IBatteryStats.Stub
        pw.println("     --proto: output as a binary protobuffer");
        pw.println("     --model power-profile: use the power profile model"
                + " even if measured energy is available");
        if (Flags.streamlinedBatteryStats()) {
            pw.println("  --sample: collect and dump a sample of stats for debugging purpose");
        }
        pw.println("  <package.name>: optional name of package to filter output by.");
        pw.println("  -h: print this help text.");
        pw.println("Battery stats (batterystats) commands:");
@@ -2938,10 +2941,10 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                    mCpuWakeupStats.dump(new IndentingPrintWriter(pw, "  "),
                            SystemClock.elapsedRealtime());
                    return;
                } else if ("--sample".equals(arg)) {
                } else if (Flags.streamlinedBatteryStats() && "--sample".equals(arg)) {
                    dumpStatsSample(pw);
                    return;
                } else if ("--aggregated".equals(arg)) {
                } else if (Flags.streamlinedBatteryStats() && "--aggregated".equals(arg)) {
                    dumpAggregatedStats(pw);
                    return;
                } else if ("--store".equals(arg)) {
+23 −4
Original line number Diff line number Diff line
@@ -286,6 +286,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
    private int[] mCpuPowerBracketMap;
    private final CpuPowerStatsCollector mCpuPowerStatsCollector;
    private boolean mPowerStatsCollectorEnabled;
    public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
        return mKernelMemoryStats;
@@ -601,6 +602,10 @@ public class BatteryStatsImpl extends BatteryStats {
    @SuppressWarnings("GuardedBy")    // errorprone false positive on getProcStateTimeCounter
    @VisibleForTesting
    public void updateProcStateCpuTimesLocked(int uid, long elapsedRealtimeMs, long uptimeMs) {
        if (mPowerStatsCollectorEnabled) {
            return;
        }
        ensureKernelSingleUidTimeReaderLocked();
        final Uid u = getUidStatsLocked(uid);
@@ -653,8 +658,9 @@ public class BatteryStatsImpl extends BatteryStats {
     */
    @SuppressWarnings("GuardedBy")    // errorprone false positive on getProcStateTimeCounter
    public void updateCpuTimesForAllUids() {
        if (mCpuPowerStatsCollector != null) {
        if (mPowerStatsCollectorEnabled && mCpuPowerStatsCollector != null) {
            mCpuPowerStatsCollector.schedule();
            return;
        }
        synchronized (BatteryStatsImpl.this) {
@@ -712,7 +718,7 @@ public class BatteryStatsImpl extends BatteryStats {
    @GuardedBy("this")
    private void ensureKernelSingleUidTimeReaderLocked() {
        if (mKernelSingleUidTimeReader != null) {
        if (mPowerStatsCollectorEnabled || mKernelSingleUidTimeReader != null) {
            return;
        }
@@ -8213,6 +8219,10 @@ public class BatteryStatsImpl extends BatteryStats {
        @GuardedBy("mBsi")
        private void ensureMultiStateCounters(long timestampMs) {
            if (mBsi.mPowerStatsCollectorEnabled) {
                throw new IllegalStateException("Multi-state counters used in streamlined mode");
            }
            if (mProcStateTimeMs == null) {
                mProcStateTimeMs =
                        new TimeInFreqMultiStateCounter(mBsi.mOnBatteryTimeBase,
@@ -10511,7 +10521,7 @@ public class BatteryStatsImpl extends BatteryStats {
                    mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
                }
                if (mBsi.trackPerProcStateCpuTimes()) {
                if (!mBsi.mPowerStatsCollectorEnabled && mBsi.trackPerProcStateCpuTimes()) {
                    mBsi.updateProcStateCpuTimesLocked(mUid, elapsedRealtimeMs, uptimeMs);
                    LongArrayMultiStateCounter onBatteryCounter =
@@ -14211,7 +14221,7 @@ public class BatteryStatsImpl extends BatteryStats {
            mCpuUidFreqTimeReader.onSystemReady();
        }
        if (mCpuPowerStatsCollector != null) {
            mCpuPowerStatsCollector.onSystemReady();
            mCpuPowerStatsCollector.setEnabled(mPowerStatsCollectorEnabled);
        }
        mSystemReady = true;
    }
@@ -15232,6 +15242,15 @@ public class BatteryStatsImpl extends BatteryStats {
        return mCpuUidFreqTimeReader.isFastCpuTimesReader();
    }
    /**
     * Enables or disables the PowerStatsCollector mode.
     */
    public void setPowerStatsCollectorEnabled(boolean enabled) {
        synchronized (this) {
            mPowerStatsCollectorEnabled = enabled;
        }
    }
    @GuardedBy("this")
    public void systemServicesReady(Context context) {
        mConstants.startObserving(context.getContentResolver());
+0 −8
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import com.android.internal.os.CpuScalingPolicies;
import com.android.internal.os.PowerProfile;
import com.android.internal.os.PowerStats;
import com.android.server.LocalServices;
import com.android.server.power.optimization.Flags;

import java.io.PrintWriter;
import java.util.ArrayList;
@@ -273,13 +272,6 @@ public class CpuPowerStatsCollector extends PowerStatsCollector {
        mDefaultCpuPowerBracketsPerEnergyConsumer = defaultCpuPowerBracketsPerEnergyConsumer;
    }

    /**
     * Initializes the collector during the boot sequence.
     */
    public void onSystemReady() {
        setEnabled(Flags.streamlinedBatteryStats());
    }

    private boolean ensureInitialized() {
        if (mIsInitialized) {
            return true;