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

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

Introduce BinaryStatePowerStatsProcessor

Bug: 333941740
Test: atest PowerStatsTestsRavenwood; atest PowerStatsTests
Flag: com.android.server.power.optimization.streamlined_misc_battery_stats
Change-Id: I4c9c94b67e957e95938389ad5293bd0de1cfd74b
parent 8cf3a71f
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -1948,6 +1948,10 @@ public abstract class BatteryStats {

        public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;

        // STATES bits that are used for Power Stats tracking
        public static final int IMPORTANT_FOR_POWER_STATS_STATES =
                STATE_GPS_ON_FLAG | STATE_SENSOR_ON_FLAG | STATE_AUDIO_ON_FLAG;

        @UnsupportedAppUsage
        public int states;

@@ -1988,6 +1992,10 @@ public abstract class BatteryStats {

        public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;

        // STATES2 bits that are used for Power Stats tracking
        public static final int IMPORTANT_FOR_POWER_STATS_STATES2 =
                STATE2_VIDEO_ON_FLAG | STATE2_FLASHLIGHT_FLAG | STATE2_CAMERA_FLAG;

        @UnsupportedAppUsage
        public int states2;

@@ -2053,6 +2061,8 @@ public abstract class BatteryStats {
        public static final int EVENT_WAKEUP_AP = 0x0013;
        // Event for reporting that a specific partial wake lock has been held for a long duration.
        public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
        // Event for reporting change of some device states, triggered by a specific UID
        public static final int EVENT_STATE_CHANGE = 0x0015;

        // Number of event types.
        public static final int EVENT_COUNT = 0x0016;
@@ -3066,13 +3076,13 @@ public abstract class BatteryStats {
    public static final String[] HISTORY_EVENT_NAMES = new String[] {
            "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
            "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
            "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
            "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity", "state"
    };

    public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
            "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
            "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
            "Esw", "Ewa", "Elw", "Eec"
            "Esw", "Ewa", "Elw", "Eec", "Esc"
    };

    @FunctionalInterface
@@ -3087,7 +3097,7 @@ public abstract class BatteryStats {
            sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
            sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sIntToString,
            sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
            sUidToString, sUidToString, sUidToString, sIntToString
            sUidToString, sUidToString, sUidToString, sIntToString, sUidToString
    };

    /**
+34 −0
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

package com.android.internal.os;

import static android.os.BatteryStats.HistoryItem.EVENT_FLAG_FINISH;
import static android.os.BatteryStats.HistoryItem.EVENT_FLAG_START;
import static android.os.BatteryStats.HistoryItem.EVENT_STATE_CHANGE;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.BatteryConsumer;
@@ -1448,6 +1452,21 @@ public class BatteryStatsHistory {
        }
    }

    /**
     * Records an event when some state flag changes to true.
     */
    public void recordStateStartEvent(long elapsedRealtimeMs, long uptimeMs, int stateFlags,
            int uid, String name) {
        synchronized (this) {
            mHistoryCur.states |= stateFlags;
            mHistoryCur.eventCode = EVENT_STATE_CHANGE | EVENT_FLAG_START;
            mHistoryCur.eventTag = mHistoryCur.localEventTag;
            mHistoryCur.eventTag.uid = uid;
            mHistoryCur.eventTag.string = name;
            writeHistoryItem(elapsedRealtimeMs, uptimeMs);
        }
    }

    /**
     * Records an event when some state flag changes to false.
     */
@@ -1458,6 +1477,21 @@ public class BatteryStatsHistory {
        }
    }

    /**
     * Records an event when some state flag changes to false.
     */
    public void recordStateStopEvent(long elapsedRealtimeMs, long uptimeMs, int stateFlags,
            int uid, String name) {
        synchronized (this) {
            mHistoryCur.states &= ~stateFlags;
            mHistoryCur.eventCode = EVENT_STATE_CHANGE | EVENT_FLAG_FINISH;
            mHistoryCur.eventTag = mHistoryCur.localEventTag;
            mHistoryCur.eventTag.uid = uid;
            mHistoryCur.eventTag.string = name;
            writeHistoryItem(elapsedRealtimeMs, uptimeMs);
        }
    }

    /**
     * Records an event when some state flags change to true and some to false.
     */
+8 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.power.stats;
import android.annotation.CurrentTimeMillisLong;
import android.annotation.DurationMillisLong;
import android.annotation.NonNull;
import android.os.BatteryStats;
import android.os.UserHandle;
import android.text.format.DateFormat;
import android.util.IndentingPrintWriter;
@@ -155,11 +156,17 @@ class AggregatedPowerStats {
        int powerComponentId = powerStats.descriptor.powerComponentId;
        for (PowerComponentAggregatedPowerStats stats : mPowerComponentStats) {
            if (stats.powerComponentId == powerComponentId) {
                stats.addPowerStats(powerStats, time);
                stats.getConfig().getProcessor().addPowerStats(stats, powerStats, time);
            }
        }
    }

    public void noteStateChange(BatteryStats.HistoryItem item) {
        for (PowerComponentAggregatedPowerStats stats : mPowerComponentStats) {
            stats.getConfig().getProcessor().noteStateChange(stats, item);
        }
    }

    void reset() {
        mClockUpdates.clear();
        mDurationMs = 0;
+1 −1
Original line number Diff line number Diff line
@@ -205,7 +205,7 @@ public class AggregatedPowerStatsConfig {

    private static final PowerStatsProcessor NO_OP_PROCESSOR = new PowerStatsProcessor() {
        @Override
        void finish(PowerComponentAggregatedPowerStats stats) {
        void finish(PowerComponentAggregatedPowerStats stats, long timestampMs) {
        }
    };
}
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 com.android.server.power.stats;

class BinaryStatePowerStatsLayout extends PowerStatsLayout {
    BinaryStatePowerStatsLayout() {
        addDeviceSectionUsageDuration();
        // Add a section for consumed energy, even if the specific device does not
        // have support EnergyConsumers.  This is done to guarantee format compatibility between
        // PowerStats created by a PowerStatsCollector and those produced by a PowerStatsProcessor.
        addDeviceSectionEnergyConsumers(1);
        addDeviceSectionPowerEstimate();

        addUidSectionUsageDuration();
        addUidSectionPowerEstimate();
    }
}
Loading