diff options
| author | Dmitri Plotnikov <dplotnikov@google.com> | 2021-04-23 15:08:29 -0700 |
|---|---|---|
| committer | Dmitri Plotnikov <dplotnikov@google.com> | 2021-04-30 13:09:32 -0700 |
| commit | d3545200232e988bf994ea32b0acc6ba97f26484 (patch) | |
| tree | 9b9ac1ea05588197766d2e63fa557cea581fdd74 /core/java/android | |
| parent | 7fc1313d39666e52d0b48ce55e8639372e4f7c01 (diff) | |
Aggregate power data in AggregateBatteryConsumers
as opposed to SystemBatteryConsumers, which will be removed
in a follow-up CL
Bug: 186006876
Test: atest FrameworksCoreTests:com.android.internal.os.BatteryStatsTests
Change-Id: Ie4166217be211d92293c2a107762801a7d3c206f
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/os/AggregateBatteryConsumer.java | 74 | ||||
| -rw-r--r-- | core/java/android/os/BatteryConsumer.java | 11 | ||||
| -rw-r--r-- | core/java/android/os/BatteryUsageStats.java | 86 |
3 files changed, 163 insertions, 8 deletions
diff --git a/core/java/android/os/AggregateBatteryConsumer.java b/core/java/android/os/AggregateBatteryConsumer.java new file mode 100644 index 000000000000..bcf41cce7e7c --- /dev/null +++ b/core/java/android/os/AggregateBatteryConsumer.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2021 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.NonNull; + +/** + * Contains power consumption data across the entire device. + * + * {@hide} + */ +public final class AggregateBatteryConsumer extends BatteryConsumer implements Parcelable { + + public AggregateBatteryConsumer(@NonNull Builder builder) { + super(builder.mPowerComponentsBuilder.build()); + } + + private AggregateBatteryConsumer(@NonNull Parcel source) { + super(new PowerComponents(source)); + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + super.writeToParcel(dest, flags); + } + + @Override + public int describeContents() { + return 0; + } + + @NonNull + public static final Creator<AggregateBatteryConsumer> CREATOR = + new Creator<AggregateBatteryConsumer>() { + public AggregateBatteryConsumer createFromParcel(@NonNull Parcel source) { + return new AggregateBatteryConsumer(source); + } + + public AggregateBatteryConsumer[] newArray(int size) { + return new AggregateBatteryConsumer[size]; + } + }; + + /** + * Builder for DeviceBatteryConsumer. + */ + public static final class Builder extends BaseBuilder<AggregateBatteryConsumer.Builder> { + public Builder(@NonNull String[] customPowerComponentNames, boolean includePowerModels) { + super(customPowerComponentNames, includePowerModels); + } + + /** + * Creates a read-only object out of the Builder values. + */ + @NonNull + public AggregateBatteryConsumer build() { + return new AggregateBatteryConsumer(this); + } + } +} diff --git a/core/java/android/os/BatteryConsumer.java b/core/java/android/os/BatteryConsumer.java index 32ca92cc97b0..6b628b0140e6 100644 --- a/core/java/android/os/BatteryConsumer.java +++ b/core/java/android/os/BatteryConsumer.java @@ -27,7 +27,7 @@ import java.lang.annotation.RetentionPolicy; * * @hide */ -public abstract class BatteryConsumer { +public class BatteryConsumer { /** * Power usage component, describing the particular part of the system @@ -72,14 +72,15 @@ public abstract class BatteryConsumer { public static final int POWER_COMPONENT_WIFI = 11; public static final int POWER_COMPONENT_WAKELOCK = 12; public static final int POWER_COMPONENT_MEMORY = 13; - public static final int POWER_COMPONENT_PHONE = 13; - public static final int POWER_COMPONENT_IDLE = 15; + public static final int POWER_COMPONENT_PHONE = 14; + public static final int POWER_COMPONENT_AMBIENT_DISPLAY = 15; + public static final int POWER_COMPONENT_IDLE = 16; // Power that is re-attributed to other battery consumers. For example, for System Server // this represents the power attributed to apps requesting system services. // The value should be negative or zero. - public static final int POWER_COMPONENT_REATTRIBUTED_TO_OTHER_CONSUMERS = 16; + public static final int POWER_COMPONENT_REATTRIBUTED_TO_OTHER_CONSUMERS = 17; - public static final int POWER_COMPONENT_COUNT = 17; + public static final int POWER_COMPONENT_COUNT = 18; public static final int FIRST_CUSTOM_POWER_COMPONENT_ID = 1000; public static final int LAST_CUSTOM_POWER_COMPONENT_ID = 9999; diff --git a/core/java/android/os/BatteryUsageStats.java b/core/java/android/os/BatteryUsageStats.java index efdef62d98ff..e3af4834c341 100644 --- a/core/java/android/os/BatteryUsageStats.java +++ b/core/java/android/os/BatteryUsageStats.java @@ -16,6 +16,7 @@ package android.os; +import android.annotation.IntDef; import android.annotation.NonNull; import android.util.Range; import android.util.SparseArray; @@ -23,15 +24,53 @@ import android.util.SparseArray; import com.android.internal.os.BatteryStatsHistory; import com.android.internal.os.BatteryStatsHistoryIterator; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; /** * Contains a snapshot of battery attribution data, on a per-subsystem and per-UID basis. + * <p> + * The totals for the entire device are returned as AggregateBatteryConsumers, which can be + * obtained by calling {@link #getAggregateBatteryConsumer(int)}. + * <p> + * Power attributed to individual apps is returned as UidBatteryConsumers, see + * {@link #getUidBatteryConsumers()}. * * @hide */ public final class BatteryUsageStats implements Parcelable { + + /** + * Scope of battery stats included in a BatteryConsumer: the entire device, just + * the apps, etc. + * + * @hide + */ + @IntDef(prefix = {"AGGREGATE_BATTERY_CONSUMER_SCOPE_"}, value = { + AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE, + AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS, + }) + @Retention(RetentionPolicy.SOURCE) + public static @interface AggregateBatteryConsumerScope { + } + + /** + * Power consumption by the entire device, since last charge. The power usage in this + * scope includes both the power attributed to apps and the power unattributed to any + * apps. + */ + public static final int AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE = 0; + + /** + * Aggregated power consumed by all applications, combined, since last charge. This is + * the sum of power reported in UidBatteryConsumers. + */ + public static final int AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS = 1; + + public static final int AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT = 2; + private final double mConsumedPower; private final int mDischargePercentage; private final long mStatsStartTimestampMs; @@ -43,6 +82,7 @@ public final class BatteryUsageStats implements Parcelable { private final List<UidBatteryConsumer> mUidBatteryConsumers; private final List<SystemBatteryConsumer> mSystemBatteryConsumers; private final List<UserBatteryConsumer> mUserBatteryConsumers; + private final AggregateBatteryConsumer[] mAggregateBatteryConsumers; private final Parcel mHistoryBuffer; private final List<BatteryStats.HistoryTag> mHistoryTagPool; @@ -57,6 +97,12 @@ public final class BatteryUsageStats implements Parcelable { mChargeTimeRemainingMs = builder.mChargeTimeRemainingMs; mCustomPowerComponentNames = builder.mCustomPowerComponentNames; + mAggregateBatteryConsumers = + new AggregateBatteryConsumer[AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT]; + for (int i = 0; i < AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT; i++) { + mAggregateBatteryConsumers[i] = builder.mAggregateBatteryConsumersBuilders[i].build(); + } + double totalPower = 0; final int uidBatteryConsumerCount = builder.mUidBatteryConsumerBuilders.size(); @@ -143,6 +189,14 @@ public final class BatteryUsageStats implements Parcelable { return mConsumedPower; } + /** + * Returns a battery consumer for the specified battery consumer type. + */ + public BatteryConsumer getAggregateBatteryConsumer( + @AggregateBatteryConsumerScope int scope) { + return mAggregateBatteryConsumers[scope]; + } + @NonNull public List<UidBatteryConsumer> getUidBatteryConsumers() { return mUidBatteryConsumers; @@ -185,6 +239,13 @@ public final class BatteryUsageStats implements Parcelable { mBatteryTimeRemainingMs = source.readLong(); mChargeTimeRemainingMs = source.readLong(); mCustomPowerComponentNames = source.readStringArray(); + mAggregateBatteryConsumers = + new AggregateBatteryConsumer[AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT]; + for (int i = 0; i < AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT; i++) { + mAggregateBatteryConsumers[i] = + AggregateBatteryConsumer.CREATOR.createFromParcel(source); + mAggregateBatteryConsumers[i].setCustomPowerComponentNames(mCustomPowerComponentNames); + } int uidCount = source.readInt(); mUidBatteryConsumers = new ArrayList<>(uidCount); for (int i = 0; i < uidCount; i++) { @@ -244,6 +305,9 @@ public final class BatteryUsageStats implements Parcelable { dest.writeLong(mBatteryTimeRemainingMs); dest.writeLong(mChargeTimeRemainingMs); dest.writeStringArray(mCustomPowerComponentNames); + for (int i = 0; i < AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT; i++) { + mAggregateBatteryConsumers[i].writeToParcel(dest, flags); + } dest.writeInt(mUidBatteryConsumers.size()); for (int i = mUidBatteryConsumers.size() - 1; i >= 0; i--) { mUidBatteryConsumers.get(i).writeToParcel(dest, flags); @@ -299,6 +363,8 @@ public final class BatteryUsageStats implements Parcelable { private double mDischargedPowerUpperBoundMah; private long mBatteryTimeRemainingMs = -1; private long mChargeTimeRemainingMs = -1; + private final AggregateBatteryConsumer.Builder[] mAggregateBatteryConsumersBuilders = + new AggregateBatteryConsumer.Builder[AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT]; private final SparseArray<UidBatteryConsumer.Builder> mUidBatteryConsumerBuilders = new SparseArray<>(); private final SparseArray<SystemBatteryConsumer.Builder> mSystemBatteryConsumerBuilders = @@ -315,6 +381,10 @@ public final class BatteryUsageStats implements Parcelable { public Builder(@NonNull String[] customPowerComponentNames, boolean includePowerModels) { mCustomPowerComponentNames = customPowerComponentNames; mIncludePowerModels = includePowerModels; + for (int i = 0; i < AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT; i++) { + mAggregateBatteryConsumersBuilders[i] = new AggregateBatteryConsumer.Builder( + customPowerComponentNames, includePowerModels); + } } /** @@ -386,7 +456,17 @@ public final class BatteryUsageStats implements Parcelable { } /** - * Creates or returns a exiting UidBatteryConsumer, which represents battery attribution + * Creates or returns an AggregateBatteryConsumer builder, which represents aggregate + * battery consumption data for the specified scope. + */ + @NonNull + public AggregateBatteryConsumer.Builder getAggregateBatteryConsumerBuilder( + @AggregateBatteryConsumerScope int scope) { + return mAggregateBatteryConsumersBuilders[scope]; + } + + /** + * Creates or returns a UidBatteryConsumer, which represents battery attribution * data for an individual UID. */ @NonNull @@ -403,7 +483,7 @@ public final class BatteryUsageStats implements Parcelable { } /** - * Creates or returns a exiting SystemBatteryConsumer, which represents battery attribution + * Creates or returns a SystemBatteryConsumer, which represents battery attribution * data for a specific drain type. */ @NonNull @@ -419,7 +499,7 @@ public final class BatteryUsageStats implements Parcelable { } /** - * Creates or returns a exiting UserBatteryConsumer, which represents battery attribution + * Creates or returns a UserBatteryConsumer, which represents battery attribution * data for an individual {@link UserHandle}. */ @NonNull |
