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

Commit 627bbedd authored by Jing Ji's avatar Jing Ji
Browse files

Add a tracker on background current drains for each uid

On detecting excessive battery usage of a background uid, we may
move the background restriction of the apps running in this uid to
more restrictive levels.

Bug: 200326767
Test: atest FrameworksMockingServicesTests:BackgroundRestrictionTest
Change-Id: I6c2d41e44367a283d8aa9491be683018a80a810c
parent 1507931e
Loading
Loading
Loading
Loading
+23 −0
Original line number Original line Diff line number Diff line
@@ -123,6 +123,15 @@ public class SparseDoubleArray implements Cloneable {
        return mValues.size();
        return mValues.size();
    }
    }


    /**
     * Returns the index for which {@link #keyAt} would return the
     * specified key, or a negative number if the specified
     * key is not mapped.
     */
    public int indexOfKey(int key) {
        return mValues.indexOfKey(key);
    }

    /**
    /**
     * Given an index in the range <code>0...size()-1</code>, returns
     * Given an index in the range <code>0...size()-1</code>, returns
     * the key from the <code>index</code>th key-value mapping that this
     * the key from the <code>index</code>th key-value mapping that this
@@ -145,6 +154,20 @@ public class SparseDoubleArray implements Cloneable {
        return Double.longBitsToDouble(mValues.valueAt(index));
        return Double.longBitsToDouble(mValues.valueAt(index));
    }
    }


    /**
     * Given an index in the range <code>0...size()-1</code>, sets a new
     * value for the <code>index</code>th key-value mapping that this
     * SparseDoubleArray stores.
     *
     * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for
     * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an
     * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting
     * {@link android.os.Build.VERSION_CODES#Q} and later.</p>
     */
    public void setValueAt(int index, double value) {
        mValues.setValueAt(index, Double.doubleToRawLongBits(value));
    }

    /**
    /**
     * Removes all key-value mappings from this SparseDoubleArray.
     * Removes all key-value mappings from this SparseDoubleArray.
     */
     */
+22 −0
Original line number Original line Diff line number Diff line
@@ -244,6 +244,28 @@ public class SparseLongArray implements Cloneable {
        return mValues[index];
        return mValues[index];
    }
    }


    /**
     * Given an index in the range <code>0...size()-1</code>, sets a new
     * value for the <code>index</code>th key-value mapping that this
     * SparseLongArray stores.
     *
     * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for
     * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an
     * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting
     * {@link android.os.Build.VERSION_CODES#Q} and later.</p>
     *
     * @hide
     */
    public void setValueAt(int index, long value) {
        if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) {
            // The array might be slightly bigger than mSize, in which case, indexing won't fail.
            // Check if exception should be thrown outside of the critical path.
            throw new ArrayIndexOutOfBoundsException(index);
        }

        mValues[index] = value;
    }

    /**
    /**
     * Returns the index for which {@link #keyAt} would return the
     * Returns the index for which {@link #keyAt} would return the
     * specified key, or a negative number if the specified
     * specified key, or a negative number if the specified
+12 −0
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import com.android.internal.os.BinderCallsStats;
import com.android.internal.os.SystemServerCpuThreadReader.SystemServiceCpuThreadTimes;
import com.android.internal.os.SystemServerCpuThreadReader.SystemServiceCpuThreadTimes;


import java.util.Collection;
import java.util.Collection;
import java.util.List;


/**
/**
 * Battery stats local system service interface. This is used to pass internal data out of
 * Battery stats local system service interface. This is used to pass internal data out of
@@ -41,6 +42,17 @@ public abstract class BatteryStatsInternal {
    /** Returns CPU times for system server thread groups. */
    /** Returns CPU times for system server thread groups. */
    public abstract SystemServiceCpuThreadTimes getSystemServiceCpuThreadTimes();
    public abstract SystemServiceCpuThreadTimes getSystemServiceCpuThreadTimes();


    /**
     * Returns BatteryUsageStats, which contains power attribution data on a per-subsystem
     * and per-UID basis.
     *
     * <p>
     * Note: This is a slow running method and should be called from non-blocking threads only.
     * </p>
     */
    public abstract List<BatteryUsageStats> getBatteryUsageStats(
            List<BatteryUsageStatsQuery> queries);

    /**
    /**
     * Inform battery stats how many deferred jobs existed when the app got launched and how
     * Inform battery stats how many deferred jobs existed when the app got launched and how
     * long ago was the last job execution for the app.
     * long ago was the last job execution for the app.
+589 −0

File added.

Preview size limit exceeded, changes collapsed.

+6 −0
Original line number Original line Diff line number Diff line
@@ -817,6 +817,11 @@ public final class AppRestrictionController {
        return mLock;
        return mLock;
    }
    }


    @VisibleForTesting
    void addAppStateTracker(@NonNull BaseAppStateTracker tracker) {
        mAppStateTrackers.add(tracker);
    }

    static class BgHandler extends Handler {
    static class BgHandler extends Handler {
        static final int MSG_BACKGROUND_RESTRICTION_CHANGED = 0;
        static final int MSG_BACKGROUND_RESTRICTION_CHANGED = 0;
        static final int MSG_APP_RESTRICTION_LEVEL_CHANGED = 1;
        static final int MSG_APP_RESTRICTION_LEVEL_CHANGED = 1;
@@ -884,6 +889,7 @@ public final class AppRestrictionController {


        void initAppStateTrackers(AppRestrictionController controller) {
        void initAppStateTrackers(AppRestrictionController controller) {
            mAppRestrictionController = controller;
            mAppRestrictionController = controller;
            controller.mAppStateTrackers.add(new AppBatteryTracker(mContext, controller));
        }
        }


        AppRestrictionController getAppRestrictionController() {
        AppRestrictionController getAppRestrictionController() {
Loading