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

Commit 35b0ec9c authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge changes I488715c8,I2333568d,Iea68a8d3 into main

* changes:
  Share UID mapping logic between BatteryStats and PowerStatCollectors
  Export aggregated PowerStats as BatteryUsageStats
  Untangle circular dependency between BatteryStats and BatteryUsageStatsProvider
parents fde7a7db 36de7b51
Loading
Loading
Loading
Loading
+23 −2
Original line number Diff line number Diff line
@@ -682,6 +682,7 @@ public abstract class BatteryConsumer {

    static class BatteryConsumerDataLayout {
        private static final Key[] KEY_ARRAY = new Key[0];
        public static final int POWER_MODEL_NOT_INCLUDED = -1;
        public final String[] customPowerComponentNames;
        public final int customPowerComponentCount;
        public final boolean powerModelsIncluded;
@@ -713,7 +714,9 @@ public abstract class BatteryConsumer {
                // Declare the Key for the power component, ignoring other dimensions.
                perComponentKeys.add(
                        new Key(componentId, PROCESS_STATE_ANY,
                                powerModelsIncluded ? columnIndex++ : -1,  // power model
                                powerModelsIncluded
                                        ? columnIndex++
                                        : POWER_MODEL_NOT_INCLUDED,  // power model
                                columnIndex++,      // power
                                columnIndex++       // usage duration
                        ));
@@ -736,7 +739,9 @@ public abstract class BatteryConsumer {

                            perComponentKeys.add(
                                    new Key(componentId, processState,
                                            powerModelsIncluded ? columnIndex++ : -1, // power model
                                            powerModelsIncluded
                                                    ? columnIndex++
                                                    : POWER_MODEL_NOT_INCLUDED, // power model
                                            columnIndex++,      // power
                                            columnIndex++       // usage duration
                                    ));
@@ -841,6 +846,15 @@ public abstract class BatteryConsumer {
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        @NonNull
        public T addConsumedPower(@PowerComponent int componentId, double componentPower,
                @PowerModel int powerModel) {
            mPowerComponentsBuilder.addConsumedPower(getKey(componentId, PROCESS_STATE_UNSPECIFIED),
                    componentPower, powerModel);
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        @NonNull
        public T setConsumedPower(Key key, double componentPower, @PowerModel int powerModel) {
@@ -848,6 +862,13 @@ public abstract class BatteryConsumer {
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        @NonNull
        public T addConsumedPower(Key key, double componentPower, @PowerModel int powerModel) {
            mPowerComponentsBuilder.addConsumedPower(key, componentPower, powerModel);
            return (T) this;
        }

        /**
         * Sets the amount of drain attributed to the specified custom drain type.
         *
+29 −18
Original line number Diff line number Diff line
@@ -4028,6 +4028,17 @@ public abstract class BatteryStats {
        return false;
    }

    /**
     * A helper object passed to various dump... methods to integrate with such objects
     * as BatteryUsageStatsProvider.
     */
    public interface BatteryStatsDumpHelper {
        /**
         * Generates BatteryUsageStats based on the specified BatteryStats.
         */
        BatteryUsageStats getBatteryUsageStats(BatteryStats batteryStats, boolean detailed);
    }

    /**
     * Dumps the ControllerActivityCounter if it has any data worth dumping.
     * The order of the arguments in the final check in line is:
@@ -4390,7 +4401,7 @@ public abstract class BatteryStats {
     * NOTE: all times are expressed in microseconds, unless specified otherwise.
     */
    public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
            boolean wifiOnly) {
            boolean wifiOnly, BatteryStatsDumpHelper dumpHelper) {

        if (which != BatteryStats.STATS_SINCE_CHARGED) {
            dumpLine(pw, 0, STAT_NAMES[which], "err",
@@ -4652,7 +4663,7 @@ public abstract class BatteryStats {
            }
        }

        final BatteryUsageStats stats = getBatteryUsageStats(context, true /* detailed */);
        final BatteryUsageStats stats = dumpHelper.getBatteryUsageStats(this, true /* detailed */);
        dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
                formatCharge(stats.getBatteryCapacity()),
                formatCharge(stats.getConsumedPower()),
@@ -5127,7 +5138,7 @@ public abstract class BatteryStats {

    @SuppressWarnings("unused")
    public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
            int reqUid, boolean wifiOnly) {
            int reqUid, boolean wifiOnly, BatteryStatsDumpHelper dumpHelper) {

        if (which != BatteryStats.STATS_SINCE_CHARGED) {
            pw.println("ERROR: BatteryStats.dump called for which type " + which
@@ -5854,7 +5865,7 @@ public abstract class BatteryStats {
        pw.println();


        BatteryUsageStats stats = getBatteryUsageStats(context, true /* detailed */);
        BatteryUsageStats stats = dumpHelper.getBatteryUsageStats(this, true /* detailed */);
        stats.dump(pw, prefix);

        List<UidMobileRadioStats> uidMobileRadioStats =
@@ -7645,7 +7656,8 @@ public abstract class BatteryStats {
     * @param pw         a Printer to receive the dump output.
     */
    @SuppressWarnings("unused")
    public void dump(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
    public void dump(Context context, PrintWriter pw, int flags, int reqUid, long histStart,
            BatteryStatsDumpHelper dumpHelper) {
        synchronized (this) {
            prepareForDumpLocked();
        }
@@ -7663,12 +7675,12 @@ public abstract class BatteryStats {
        }

        synchronized (this) {
            dumpLocked(context, pw, flags, reqUid, filtering);
            dumpLocked(context, pw, flags, reqUid, filtering, dumpHelper);
        }
    }

    private void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid,
            boolean filtering) {
            boolean filtering, BatteryStatsDumpHelper dumpHelper) {
        if (!filtering) {
            SparseArray<? extends Uid> uidStats = getUidStats();
            final int NU = uidStats.size();
@@ -7803,15 +7815,15 @@ public abstract class BatteryStats {
            pw.println("  System starts: " + getStartCount()
                    + ", currently on battery: " + getIsOnBattery());
            dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
                    (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
                    (flags & DUMP_DEVICE_WIFI_ONLY) != 0, dumpHelper);
            pw.println();
        }
    }

    // This is called from BatteryStatsService.
    @SuppressWarnings("unused")
    public void dumpCheckin(Context context, PrintWriter pw,
            List<ApplicationInfo> apps, int flags, long histStart) {
    public void dumpCheckin(Context context, PrintWriter pw, List<ApplicationInfo> apps, int flags,
            long histStart, BatteryStatsDumpHelper dumpHelper) {
        synchronized (this) {
            prepareForDumpLocked();

@@ -7829,12 +7841,12 @@ public abstract class BatteryStats {
        }

        synchronized (this) {
            dumpCheckinLocked(context, pw, apps, flags);
            dumpCheckinLocked(context, pw, apps, flags, dumpHelper);
        }
    }

    private void dumpCheckinLocked(Context context, PrintWriter pw, List<ApplicationInfo> apps,
            int flags) {
            int flags, BatteryStatsDumpHelper dumpHelper) {
        if (apps != null) {
            SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
            for (int i=0; i<apps.size(); i++) {
@@ -7881,7 +7893,7 @@ public abstract class BatteryStats {
                        (Object[])lineArgs);
            }
            dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
                    (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
                    (flags & DUMP_DEVICE_WIFI_ONLY) != 0, dumpHelper);
        }
    }

@@ -7891,7 +7903,7 @@ public abstract class BatteryStats {
     * @hide
     */
    public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
            int flags, long histStart) {
            int flags, long histStart, BatteryStatsDumpHelper dumpHelper) {
        final ProtoOutputStream proto = new ProtoOutputStream(fd);
        prepareForDumpLocked();

@@ -7909,7 +7921,8 @@ public abstract class BatteryStats {
        proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());

        if ((flags & DUMP_DAILY_ONLY) == 0) {
            final BatteryUsageStats stats = getBatteryUsageStats(context, false /* detailed */);
            final BatteryUsageStats stats =
                    dumpHelper.getBatteryUsageStats(this, false /* detailed */);
            ProportionalAttributionCalculator proportionalAttributionCalculator =
                    new ProportionalAttributionCalculator(context, stats);
            dumpProtoAppsLocked(proto, stats, apps, proportionalAttributionCalculator);
@@ -8856,8 +8869,6 @@ public abstract class BatteryStats {
        return !tm.isDataCapable();
    }

    protected abstract BatteryUsageStats getBatteryUsageStats(Context context, boolean detailed);

    private boolean shouldHidePowerComponent(int powerComponent) {
        return powerComponent == BatteryConsumer.POWER_COMPONENT_IDLE
                || powerComponent == BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO
+12 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
@@ -586,7 +587,8 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
                            + "(" + BatteryConsumer.processStateToString(key.processState) + ")";
                }
                printPowerComponent(pw, prefix, label, devicePowerMah, appsPowerMah,
                        deviceConsumer.getPowerModel(key),
                        mIncludesPowerModels ? deviceConsumer.getPowerModel(key)
                                : BatteryConsumer.POWER_MODEL_UNDEFINED,
                        deviceConsumer.getUsageDurationMillis(key));
            }
        }
@@ -774,6 +776,15 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
        super.finalize();
    }

    @Override
    public String toString() {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        dump(pw, "");
        pw.flush();
        return sw.toString();
    }

    /**
     * Builder for BatteryUsageStats.
     */
+27 −14
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package android.os;

import static android.os.BatteryConsumer.BatteryConsumerDataLayout.POWER_MODEL_NOT_INCLUDED;
import static android.os.BatteryConsumer.POWER_COMPONENT_ANY;
import static android.os.BatteryConsumer.PROCESS_STATE_ANY;
import static android.os.BatteryConsumer.PROCESS_STATE_UNSPECIFIED;
@@ -118,7 +119,7 @@ class PowerComponents {

    @BatteryConsumer.PowerModel
    int getPowerModel(BatteryConsumer.Key key) {
        if (key.mPowerModelColumnIndex == -1) {
        if (key.mPowerModelColumnIndex == POWER_MODEL_NOT_INCLUDED) {
            throw new IllegalStateException(
                    "Power model IDs were not requested in the BatteryUsageStatsQuery");
        }
@@ -468,7 +469,7 @@ class PowerComponents {
            mMinConsumedPowerThreshold = minConsumedPowerThreshold;
            for (BatteryConsumer.Key[] keys : mData.layout.keys) {
                for (BatteryConsumer.Key key : keys) {
                    if (key.mPowerModelColumnIndex != -1) {
                    if (key.mPowerModelColumnIndex != POWER_MODEL_NOT_INCLUDED) {
                        mData.putInt(key.mPowerModelColumnIndex, POWER_MODEL_UNINITIALIZED);
                    }
                }
@@ -478,11 +479,19 @@ class PowerComponents {
        @NonNull
        public Builder setConsumedPower(BatteryConsumer.Key key, double componentPower,
                int powerModel) {
            if (Math.abs(componentPower) < mMinConsumedPowerThreshold) {
                componentPower = 0;
            }
            mData.putDouble(key.mPowerColumnIndex, componentPower);
            if (key.mPowerModelColumnIndex != -1) {
            if (key.mPowerModelColumnIndex != POWER_MODEL_NOT_INCLUDED) {
                mData.putInt(key.mPowerModelColumnIndex, powerModel);
            }
            return this;
        }

        @NonNull
        public Builder addConsumedPower(BatteryConsumer.Key key, double componentPower,
                int powerModel) {
            mData.putDouble(key.mPowerColumnIndex,
                    mData.getDouble(key.mPowerColumnIndex) + componentPower);
            if (key.mPowerModelColumnIndex != POWER_MODEL_NOT_INCLUDED) {
                mData.putInt(key.mPowerModelColumnIndex, powerModel);
            }
            return this;
@@ -496,9 +505,6 @@ class PowerComponents {
         */
        @NonNull
        public Builder setConsumedPowerForCustomComponent(int componentId, double componentPower) {
            if (Math.abs(componentPower) < mMinConsumedPowerThreshold) {
                componentPower = 0;
            }
            final int index = componentId - BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID;
            if (index < 0 || index >= mData.layout.customPowerComponentCount) {
                throw new IllegalArgumentException(
@@ -575,12 +581,12 @@ class PowerComponents {
                            mData.getLong(key.mDurationColumnIndex)
                                    + otherData.getLong(otherKey.mDurationColumnIndex));

                    if (key.mPowerModelColumnIndex == -1) {
                    if (key.mPowerModelColumnIndex == POWER_MODEL_NOT_INCLUDED) {
                        continue;
                    }

                    boolean undefined = false;
                    if (otherKey.mPowerModelColumnIndex == -1) {
                    if (otherKey.mPowerModelColumnIndex == POWER_MODEL_NOT_INCLUDED) {
                        undefined = true;
                    } else {
                        final int powerModel = mData.getInt(key.mPowerModelColumnIndex);
@@ -641,19 +647,26 @@ class PowerComponents {
         */
        @NonNull
        public PowerComponents build() {
            mData.putDouble(mData.layout.totalConsumedPowerColumnIndex, getTotalPower());

            for (BatteryConsumer.Key[] keys : mData.layout.keys) {
                for (BatteryConsumer.Key key : keys) {
                    if (key.mPowerModelColumnIndex != -1) {
                    if (key.mPowerModelColumnIndex != POWER_MODEL_NOT_INCLUDED) {
                        if (mData.getInt(key.mPowerModelColumnIndex) == POWER_MODEL_UNINITIALIZED) {
                            mData.putInt(key.mPowerModelColumnIndex,
                                    BatteryConsumer.POWER_MODEL_UNDEFINED);
                        }
                    }

                    if (mMinConsumedPowerThreshold != 0) {
                        if (mData.getDouble(key.mPowerColumnIndex) < mMinConsumedPowerThreshold) {
                            mData.putDouble(key.mPowerColumnIndex, 0);
                        }
                    }
                }
            }

            if (mData.getDouble(mData.layout.totalConsumedPowerColumnIndex) == 0) {
                mData.putDouble(mData.layout.totalConsumedPowerColumnIndex, getTotalPower());
            }
            return new PowerComponents(this);
        }
    }
+2 −0
Original line number Diff line number Diff line
@@ -50,6 +50,8 @@ public class MonotonicClock {
    private final Clock mClock;
    private long mTimeshift;

    public static final long UNDEFINED = -1;

    public MonotonicClock(File file) {
        mFile = new AtomicFile(file);
        mClock = Clock.SYSTEM_CLOCK;
Loading