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

Commit 9f89d4bd authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add shell commands to set flex policy." into main

parents bf782e30 e6b9f110
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
@@ -304,6 +304,8 @@ public class JobSchedulerService extends com.android.server.SystemService
    private final ConnectivityController mConnectivityController;
    /** Need directly for sending uid state changes */
    private final DeviceIdleJobsController mDeviceIdleJobsController;
    /** Need direct access to this for testing. */
    private final FlexibilityController mFlexibilityController;
    /** Needed to get next estimated launch time. */
    private final PrefetchController mPrefetchController;
    /** Needed to get remaining quota time. */
@@ -2701,17 +2703,16 @@ public class JobSchedulerService extends com.android.server.SystemService
        mControllers = new ArrayList<StateController>();
        mPrefetchController = new PrefetchController(this);
        mControllers.add(mPrefetchController);
        final FlexibilityController flexibilityController =
                new FlexibilityController(this, mPrefetchController);
        mControllers.add(flexibilityController);
        mFlexibilityController = new FlexibilityController(this, mPrefetchController);
        mControllers.add(mFlexibilityController);
        mConnectivityController =
                new ConnectivityController(this, flexibilityController);
                new ConnectivityController(this, mFlexibilityController);
        mControllers.add(mConnectivityController);
        mControllers.add(new TimeController(this));
        final IdleController idleController = new IdleController(this, flexibilityController);
        final IdleController idleController = new IdleController(this, mFlexibilityController);
        mControllers.add(idleController);
        final BatteryController batteryController =
                new BatteryController(this, flexibilityController);
                new BatteryController(this, mFlexibilityController);
        mControllers.add(batteryController);
        mStorageController = new StorageController(this);
        mControllers.add(mStorageController);
@@ -5561,6 +5562,15 @@ public class JobSchedulerService extends com.android.server.SystemService
        return 0;
    }

    // Shell command infrastructure: set flex policy
    void setFlexPolicy(boolean override, int appliedConstraints) {
        if (DEBUG) {
            Slog.v(TAG, "setFlexPolicy(): " + override + "/" + appliedConstraints);
        }

        mFlexibilityController.setLocalPolicyForTesting(override, appliedConstraints);
    }

    void setMonitorBattery(boolean enabled) {
        synchronized (mLock) {
            mBatteryStateTracker.setMonitorBatteryLocked(enabled);
+88 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.os.Binder;
import android.os.UserHandle;

import com.android.modules.utils.BasicShellCommandHandler;
import com.android.server.job.controllers.JobStatus;

import java.io.PrintWriter;

@@ -59,6 +60,10 @@ public final class JobSchedulerShellCommand extends BasicShellCommandHandler {
                    return cancelJob(pw);
                case "monitor-battery":
                    return monitorBattery(pw);
                case "disable-flex-policy":
                    return disableFlexPolicy(pw);
                case "enable-flex-policy":
                    return enableFlexPolicy(pw);
                case "get-aconfig-flag-state":
                    return getAconfigFlagState(pw);
                case "get-battery-seq":
@@ -91,6 +96,8 @@ public final class JobSchedulerShellCommand extends BasicShellCommandHandler {
                    return resetExecutionQuota(pw);
                case "reset-schedule-quota":
                    return resetScheduleQuota(pw);
                case "reset-flex-policy":
                    return resetFlexPolicy(pw);
                case "stop":
                    return stop(pw);
                case "trigger-dock-state":
@@ -346,6 +353,65 @@ public final class JobSchedulerShellCommand extends BasicShellCommandHandler {
        return 0;
    }

    private int disableFlexPolicy(PrintWriter pw) throws Exception {
        checkPermission("disable flex policy");

        final long ident = Binder.clearCallingIdentity();
        try {
            mInternal.setFlexPolicy(true, 0);
            pw.println("Set flex policy to 0");
            return 0;
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

    private int enableFlexPolicy(PrintWriter pw) throws Exception {
        checkPermission("enable flex policy");

        int enabled = 0;

        String opt;
        while ((opt = getNextOption()) != null) {
            switch (opt) {
                case "-o":
                case "--option":
                    final String constraint = getNextArgRequired();
                    switch (constraint) {
                        case "battery-not-low":
                            enabled |= JobStatus.CONSTRAINT_BATTERY_NOT_LOW;
                            break;
                        case "charging":
                            enabled |= JobStatus.CONSTRAINT_CHARGING;
                            break;
                        case "connectivity":
                            enabled |= JobStatus.CONSTRAINT_CONNECTIVITY;
                            break;
                        case "idle":
                            enabled |= JobStatus.CONSTRAINT_IDLE;
                            break;
                        default:
                            pw.println("Unsupported option: " + constraint);
                            return -1;
                    }
                    break;

                default:
                    pw.println("Error: unknown option '" + opt + "'");
                    return -1;
            }
        }

        final long ident = Binder.clearCallingIdentity();
        try {
            mInternal.setFlexPolicy(true, enabled);
            pw.println("Set flex policy to " + enabled);
            return 0;
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

    private int getAconfigFlagState(PrintWriter pw) throws Exception {
        checkPermission("get aconfig flag state", Manifest.permission.DUMP);

@@ -581,6 +647,19 @@ public final class JobSchedulerShellCommand extends BasicShellCommandHandler {
        return 0;
    }

    private int resetFlexPolicy(PrintWriter pw) throws Exception {
        checkPermission("reset flex policy");

        final long ident = Binder.clearCallingIdentity();
        try {
            mInternal.setFlexPolicy(false, 0);
            pw.println("Reset flex policy to its default state");
            return 0;
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

    private int resetExecutionQuota(PrintWriter pw) throws Exception {
        checkPermission("reset execution quota");

@@ -773,6 +852,15 @@ public final class JobSchedulerShellCommand extends BasicShellCommandHandler {
        pw.println("  monitor-battery [on|off]");
        pw.println("    Control monitoring of all battery changes.  Off by default.  Turning");
        pw.println("    on makes get-battery-seq useful.");
        pw.println("  enable-flex-policy --option <option>");
        pw.println("    Enable flex policy with the specified options. Supported options are");
        pw.println("    battery-not-low, charging, connectivity, idle.");
        pw.println("    Multiple enable options can be specified (e.g.");
        pw.println("    enable-flex-policy --option battery-not-low --option charging");
        pw.println("  disable-flex-policy");
        pw.println("    Turn off flex policy so that it does not affect job execution.");
        pw.println("  reset-flex-policy");
        pw.println("    Resets the flex policy to its default state.");
        pw.println("  get-aconfig-flag-state FULL_FLAG_NAME");
        pw.println("    Return the state of the specified aconfig flag, if known. The flag name");
        pw.println("         must be fully qualified.");
+30 −0
Original line number Diff line number Diff line
@@ -328,6 +328,9 @@ public final class FlexibilityController extends StateController {
    @GuardedBy("mLock")
    private final ArraySet<String> mPackagesToCheck = new ArraySet<>();

    @GuardedBy("mLock")
    private boolean mLocalOverride;

    public FlexibilityController(
            JobSchedulerService service, PrefetchController prefetchController) {
        super(service);
@@ -1923,6 +1926,27 @@ public final class FlexibilityController extends StateController {
        }
    }

    /**
     * If {@code override} is true, uses {@code appliedConstraints} for flex policy evaluation,
     * overriding anything else that was set. If {@code override} is false, any previous calls
     * will be discarded and the policy will be reset to the normal default policy.
     */
    public void setLocalPolicyForTesting(boolean override, int appliedConstraints) {
        synchronized (mLock) {
            final boolean recheckJobs = mLocalOverride != override
                    || mAppliedConstraints != appliedConstraints;
            mLocalOverride = override;
            if (mLocalOverride) {
                mAppliedConstraints = appliedConstraints;
            } else {
                mAppliedConstraints = mFcConfig.APPLIED_CONSTRAINTS;
            }
            if (recheckJobs) {
                mHandler.obtainMessage(MSG_CHECK_ALL_JOBS).sendToTarget();
            }
        }
    }

    @Override
    @GuardedBy("mLock")
    public void dumpConstants(IndentingPrintWriter pw) {
@@ -1932,6 +1956,12 @@ public final class FlexibilityController extends StateController {
    @Override
    @GuardedBy("mLock")
    public void dumpControllerStateLocked(IndentingPrintWriter pw, Predicate<JobStatus> predicate) {
        if (mLocalOverride) {
            pw.println("Local override active");
        }
        pw.print("Applied Flexible Constraints:");
        JobStatus.dumpConstraints(pw, mAppliedConstraints);
        pw.println();
        pw.print("Satisfied Flexible Constraints:");
        JobStatus.dumpConstraints(pw, mSatisfiedFlexibleConstraints);
        pw.println();