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

Commit d0ce1fbb authored by Adam Lesinski's avatar Adam Lesinski Committed by Android Git Automerger
Browse files

am 69e17f59: am da43a3aa: Merge "BatteryStats: Start using cpu power from kernel" into mnc-dr-dev

* commit '69e17f59':
  BatteryStats: Start using cpu power from kernel
parents d1c6dca6 69e17f59
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -468,6 +468,7 @@ public abstract class BatteryStats implements Parcelable {
         * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
         * @see BatteryStats#getCpuSpeedSteps()
         */
        @Deprecated
        public abstract long getTimeAtCpuSpeed(int step, int which);

        public static abstract class Sensor {
+1 −1
Original line number Diff line number Diff line
@@ -338,7 +338,7 @@ public final class BatteryStatsHelper {
        }

        if (mCpuPowerCalculator == null) {
            mCpuPowerCalculator = new CpuPowerCalculator(mPowerProfile);
            mCpuPowerCalculator = new CpuPowerCalculator();
        }
        mCpuPowerCalculator.reset();

+4 −47
Original line number Diff line number Diff line
@@ -23,54 +23,14 @@ public class CpuPowerCalculator extends PowerCalculator {
    private static final String TAG = "CpuPowerCalculator";
    private static final boolean DEBUG = BatteryStatsHelper.DEBUG;

    private final double[] mPowerCpuNormal;

    /**
     * Reusable array for calculations.
     */
    private final long[] mSpeedStepTimes;

    public CpuPowerCalculator(PowerProfile profile) {
        final int speedSteps = profile.getNumSpeedSteps();
        mPowerCpuNormal = new double[speedSteps];
        mSpeedStepTimes = new long[speedSteps];
        for (int p = 0; p < speedSteps; p++) {
            mPowerCpuNormal[p] = profile.getAveragePower(PowerProfile.POWER_CPU_ACTIVE, p);
        }
    }

    @Override
    public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
                             long rawUptimeUs, int statsType) {
        final int speedSteps = mSpeedStepTimes.length;

        long totalTimeAtSpeeds = 0;
        for (int step = 0; step < speedSteps; step++) {
            mSpeedStepTimes[step] = u.getTimeAtCpuSpeed(step, statsType);
            totalTimeAtSpeeds += mSpeedStepTimes[step];
        }
        totalTimeAtSpeeds = Math.max(totalTimeAtSpeeds, 1);

        app.cpuTimeMs = (u.getUserCpuTimeUs(statsType) + u.getSystemCpuTimeUs(statsType)) / 1000;
        if (DEBUG && app.cpuTimeMs != 0) {
            Log.d(TAG, "UID " + u.getUid() + ": CPU time " + app.cpuTimeMs + " ms");
        }

        double cpuPowerMaMs = 0;
        for (int step = 0; step < speedSteps; step++) {
            final double ratio = (double) mSpeedStepTimes[step] / totalTimeAtSpeeds;
            final double cpuSpeedStepPower = ratio * app.cpuTimeMs * mPowerCpuNormal[step];
            if (DEBUG && ratio != 0) {
                Log.d(TAG, "UID " + u.getUid() + ": CPU step #"
                        + step + " ratio=" + BatteryStatsHelper.makemAh(ratio) + " power="
                        + BatteryStatsHelper.makemAh(cpuSpeedStepPower / (60 * 60 * 1000)));
            }
            cpuPowerMaMs += cpuSpeedStepPower;
        }

        if (DEBUG && cpuPowerMaMs != 0) {
            Log.d(TAG, "UID " + u.getUid() + ": cpu total power="
                    + BatteryStatsHelper.makemAh(cpuPowerMaMs / (60 * 60 * 1000)));
        app.cpuPowerMah = (double) u.getCpuPowerMaUs(statsType) / (60.0 * 60.0 * 1000.0 * 1000.0);
        if (DEBUG && (app.cpuTimeMs != 0 || app.cpuPowerMah != 0)) {
            Log.d(TAG, "UID " + u.getUid() + ": CPU time=" + app.cpuTimeMs + " ms power="
                    + BatteryStatsHelper.makemAh(app.cpuPowerMah));
        }

        // Keep track of the package with highest drain.
@@ -108,8 +68,5 @@ public class CpuPowerCalculator extends PowerCalculator {
            // Statistics may not have been gathered yet.
            app.cpuTimeMs = app.cpuFgTimeMs;
        }

        // Convert the CPU power to mAh
        app.cpuPowerMah = cpuPowerMaMs / (60 * 60 * 1000);
    }
}