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

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

Add "modeled" power components

Modeled power components are computed without regard
for power measured with ODPM/rails.

The purpose of this is mostly to support testing
of power models themselves.

Bug: 175644968
Test: mp :BatteryStatsViewer && adb shell am start -n com.android.frameworks.core.batterystatsviewer/.BatteryStatsViewerActivity

Change-Id: Icca6fe6954776baed48b593017abd8c9bab4dfb7
parent a3c423c3
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -50,6 +50,17 @@ public abstract class BatteryConsumer {
    public static final int FIRST_CUSTOM_POWER_COMPONENT_ID = 1000;
    public static final int LAST_CUSTOM_POWER_COMPONENT_ID = 9999;

    /**
     * Modeled power components are used for testing only.  They are returned if the
     * {@link BatteryUsageStatsQuery#FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED} is set.
     * The modeled power components are retrieved with {@link #getConsumedPowerForCustomComponent}.
     * The ID of a modeled power component is calculated as
     * (FIRST_MODELED_POWER_COMPONENT_ID + powerComponentId), e.g.
     * FIRST_MODELED_POWER_COMPONENT_ID + POWER_COMPONENT_CPU.
     */
    public static final int FIRST_MODELED_POWER_COMPONENT_ID = 10000;
    public static final int LAST_MODELED_POWER_COMPONENT_ID = 19999;

    /**
     * Time usage component, describing the particular part of the system
     * that was used for the corresponding amount of time.
@@ -137,9 +148,10 @@ public abstract class BatteryConsumer {
    protected abstract static class BaseBuilder<T extends BaseBuilder<?>> {
        final PowerComponents.Builder mPowerComponentsBuilder;

        public BaseBuilder(int customPowerComponentCount, int customTimeComponentCount) {
        public BaseBuilder(int customPowerComponentCount, int customTimeComponentCount,
                boolean includeModeledComponents) {
            mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentCount,
                    customTimeComponentCount);
                    customTimeComponentCount, includeModeledComponents);
        }

        /**
+14 −1
Original line number Diff line number Diff line
@@ -160,6 +160,7 @@ public final class BatteryStatsManager {
        mBatteryStats = batteryStats;
    }


    /**
     * Returns BatteryUsageStats, which contains power attribution data on a per-subsystem
     * and per-UID basis.
@@ -169,8 +170,20 @@ public final class BatteryStatsManager {
    @RequiresPermission(android.Manifest.permission.BATTERY_STATS)
    @NonNull
    public BatteryUsageStats getBatteryUsageStats() {
        return getBatteryUsageStats(BatteryUsageStatsQuery.DEFAULT);
    }

    /**
     * Returns BatteryUsageStats, which contains power attribution data on a per-subsystem
     * and per-UID basis.
     *
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.BATTERY_STATS)
    @NonNull
    public BatteryUsageStats getBatteryUsageStats(BatteryUsageStatsQuery query) {
        try {
            return mBatteryStats.getBatteryUsageStats();
            return mBatteryStats.getBatteryUsageStats(query);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+6 −3
Original line number Diff line number Diff line
@@ -114,6 +114,7 @@ public final class BatteryUsageStats implements Parcelable {
    public static final class Builder {
        private final int mCustomPowerComponentCount;
        private final int mCustomTimeComponentCount;
        private final boolean mIncludeModeledComponents;
        private double mConsumedPower;
        private int mDischargePercentage;
        private final SparseArray<UidBatteryConsumer.Builder> mUidBatteryConsumerBuilders =
@@ -121,9 +122,11 @@ public final class BatteryUsageStats implements Parcelable {
        private final SparseArray<SystemBatteryConsumer.Builder> mSystemBatteryConsumerBuilders =
                new SparseArray<>();

        public Builder(int customPowerComponentCount, int customTimeComponentCount) {
        public Builder(int customPowerComponentCount, int customTimeComponentCount,
                boolean includeModeledComponents) {
            mCustomPowerComponentCount = customPowerComponentCount;
            mCustomTimeComponentCount = customTimeComponentCount;
            mIncludeModeledComponents = includeModeledComponents;
        }

        /**
@@ -166,7 +169,7 @@ public final class BatteryUsageStats implements Parcelable {
            UidBatteryConsumer.Builder builder = mUidBatteryConsumerBuilders.get(uid);
            if (builder == null) {
                builder = new UidBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, batteryStatsUid);
                        mCustomTimeComponentCount, mIncludeModeledComponents, batteryStatsUid);
                mUidBatteryConsumerBuilders.put(uid, builder);
            }
            return builder;
@@ -182,7 +185,7 @@ public final class BatteryUsageStats implements Parcelable {
            SystemBatteryConsumer.Builder builder = mSystemBatteryConsumerBuilders.get(drainType);
            if (builder == null) {
                builder = new SystemBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, drainType);
                        mCustomTimeComponentCount, mIncludeModeledComponents, drainType);
                mSystemBatteryConsumerBuilders.put(drainType, builder);
            }
            return builder;
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;

parcelable BatteryUsageStatsQuery;
+123 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;

import android.annotation.IntDef;
import android.annotation.NonNull;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Query parameters for the {@link BatteryStatsManager#getBatteryUsageStats()} call.
 *
 * @hide
 */
public final class BatteryUsageStatsQuery implements Parcelable {

    @NonNull
    public static final BatteryUsageStatsQuery DEFAULT =
            new BatteryUsageStatsQuery.Builder().build();

    /**
     * Flags for the {@link BatteryStatsManager#getBatteryUsageStats()} method.
     * @hide
     */
    @IntDef(flag = true, prefix = { "FLAG_BATTERY_USAGE_STATS_" }, value = {
            FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BatteryUsageStatsFlags {}

    /**
     * Indicates that modeled battery usage stats should be returned along with
     * measured ones.
     *
     * @hide
     */
    public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED = 1;

    private final int mFlags;

    private BatteryUsageStatsQuery(@NonNull Builder builder) {
        mFlags = builder.mFlags;
    }

    @BatteryUsageStatsFlags
    public int getFlags() {
        return mFlags;
    }

    private BatteryUsageStatsQuery(Parcel in) {
        mFlags = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mFlags);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @NonNull
    public static final Creator<BatteryUsageStatsQuery> CREATOR =
            new Creator<BatteryUsageStatsQuery>() {
                @Override
                public BatteryUsageStatsQuery createFromParcel(Parcel in) {
                    return new BatteryUsageStatsQuery(in);
                }

                @Override
                public BatteryUsageStatsQuery[] newArray(int size) {
                    return new BatteryUsageStatsQuery[size];
                }
            };

    /**
     * Builder for BatteryUsageStatsQuery.
     */
    public static final class Builder {
        private int mFlags;

        /**
         * Builds a read-only BatteryUsageStatsQuery object.
         */
        public BatteryUsageStatsQuery build() {
            return new BatteryUsageStatsQuery(this);
        }

        /**
         * Sets flags to modify the behavior of {@link BatteryStatsManager#getBatteryUsageStats}.
         */
        public Builder setFlags(@BatteryUsageStatsFlags int flags) {
            mFlags = flags;
            return this;
        }

        /**
         * Requests to include modeled battery usage stats along with measured ones.
         * Should only be used for testing and debugging.
         */
        public Builder includeModeled() {
            mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED;
            return this;
        }
    }
}
Loading